Skip to content

Commit

Permalink
Added livewire stuff to money class
Browse files Browse the repository at this point in the history
  • Loading branch information
daredloco committed Jun 21, 2024
1 parent aad839c commit fa24ff5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"ext-bcmath": "*",
"ext-filter": "*",
"ext-json": "*"
"ext-json": "*",
"livewire/livewire": "^3.5"
},
"require-dev": {
"ext-gmp": "*",
Expand Down
18 changes: 17 additions & 1 deletion src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InvalidArgumentException;
use JsonSerializable;
use Money\Calculator\BcMathCalculator;
use Livewire\Wireable;

use function array_fill;
use function array_keys;
Expand All @@ -32,7 +33,7 @@
*
* @psalm-immutable
*/
class Money implements JsonSerializable
final class Money implements JsonSerializable, Wireable
{
use MoneyFactory;

Expand Down Expand Up @@ -537,4 +538,19 @@ public static function getCalculator(): string
{
return self::$calculator;
}

public function toLivewire()
{
return [
'amount' => $this->getAmount(),
'currency' => $this->getCurrency()->getCode(),
];
}

public static function fromLivewire($value)
{
$amount = $value['amount'];
$currency = $value['currency'];
return new static($amount, new Currency($currency));
}
}

0 comments on commit fa24ff5

Please sign in to comment.