Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,32 @@ use Akaunting\Money\Money;

class CustomMoney
{
public function absolute(): Money
public function absolute(): callable
{
return $this->isPositive() ? $this : $this->multiply(-1);
return function(): Money {
/** @var Money $this */
return $this->isPositive() ? $this : $this->multiply(-1);
};
}

public static function zero(?string $currency = null): Money
public function zero(): callable
{
return new Money(0, new Currency($currency ?? 'GBP'));
return fn(?string $currency = null): Money => new Money(0, new Currency($currency ?? 'GBP'));
}

public function sum(): callable
{
return function (iterable $items, null|string|Currency $currency = null): Money {
$items = collect($items)->ensure(Money::class);
return $items->reduce(
fn(Money $sum, Money $item) => $sum->add($item),
new Money(0, $currency instanceof Currency ? $currency : (
$items->isEmpty() || is_string($currency) ? new Currency($currency ?? 'GBP') : $items->first()->getCurrency()
))
);
};
}

}
```

Expand Down