Skip to content
Closed
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "moneyphp/money",
"name": "helvetitec/money",
"description": "PHP implementation of Fowler's Money pattern",
"license": "MIT",
"keywords": [
Expand All @@ -20,13 +20,19 @@
{
"name": "Frederik Bosch",
"email": "f.bosch@genkgo.nl"
},
{
"name": "Roman Arthur Wanner",
"email": "contact@helvetitec.com"
}
],
"homepage": "http://moneyphp.org",
"require": {
"php": "~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0",
"ext-bcmath": "*",
"ext-filter": "*"
"ext-filter": "*",
"ext-json": "*",
"livewire/livewire": "^3.5|^4.0"
},
"require-dev": {
"ext-gmp": "*",
Expand Down
29 changes: 28 additions & 1 deletion src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use JsonSerializable;
use Money\Calculator\BcMathCalculator;
use Money\Exception\InvalidArgumentException;
use Livewire\Wireable;

use function array_fill;
use function array_keys;
Expand All @@ -30,7 +31,7 @@
/**
* Money Value Object.
*/
final class Money implements JsonSerializable
final class Money implements JsonSerializable, Wireable
{
use MoneyFactory;

Expand Down Expand Up @@ -558,4 +559,30 @@ public static function getCalculator(): string
{
return self::$calculator;
}

/**
* @return array{
* amount: string,
* currency: string
* }
*/
public function toLivewire(): array
{
return [
'amount' => $this->getAmount(),
'currency' => $this->getCurrency()->getCode(),
];
}

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

public function __toString(): string
{
return json_encode($this->toLivewire());
}
}