From 3a343d6bc9c8b75515266113fa4bd1c4d4e52fbc Mon Sep 17 00:00:00 2001 From: netzknecht <38781482+netzknecht@users.noreply.github.com> Date: Mon, 16 Dec 2024 12:38:25 +0100 Subject: [PATCH] Update README.md Mixins must return closures to work properly. I've also added a useful "sum" method to the mixins example. --- README.md | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cd335c0..a3002ba 100644 --- a/README.md +++ b/README.md @@ -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() + )) + ); + }; + } + } ```