brick / money

A money and currency library for PHP
MIT License
1.65k stars 102 forks source link

Conditional chaining #20

Closed khairahmanplus closed 5 years ago

khairahmanplus commented 5 years ago

I have use case where i need to chain a lot of numbers based on true/false conditional. Idea is from method when in Laravel Framework.

https://github.com/laravel/framework/blob/5.8/src/Illuminate/Database/Concerns/BuildsQueries.php#L88

$salary = '3000.70';
$employerContributeTax = true;
$employerContributeInsurance = true;

$gross = Money::for($salary)
    ->when($employerContributeTax, function ($salary) {
        return $salary->minus($someNumber);
    })
    ->when($employerContributeInsurance, function ($salary) {
        return $salary->minus($someNumberAgain);
    });
BenMorel commented 5 years ago

I'm sorry but I fail to see the value in this proposal.

Is the above equivalent to the following code?

$salary = '3000.70';
$employerContributeTax = true;
$employerContributeInsurance = true;

$gross = Money::of($salary);

if ($employerContributeTax) {
    $salary = $salary->minus($someNumber);
}

if ($employerContributeInsurance) {
    $salary = $salary->minus($someNumberAgain);
}

If so, what's wrong with this approach?

BenMorel commented 5 years ago

Closing due to absence of feedback. Please feel free to comment if you think this should be reopened.