Created
October 26, 2024 08:58
-
-
Save devhammed/8531f595b9a1d25f76265fb95432c752 to your computer and use it in GitHub Desktop.
Livewire Synthesizer for Money and Currency objects from https://github.com/akaunting/laravel-money package.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Support\Synthesizers; | |
use Akaunting\Money\Currency; | |
use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth; | |
class CurrencySynthesizer extends Synth | |
{ | |
public static string $key = 'currency'; | |
public static function match(mixed $target): bool | |
{ | |
return $target instanceof Currency; | |
} | |
public function dehydrate(Currency $target): array | |
{ | |
return [$target->getCurrency(), []]; | |
} | |
public function hydrate(string $value): Currency | |
{ | |
return currency($value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Support\Synthesizers; | |
use Akaunting\Money\Money; | |
use Akaunting\Money\Currency; | |
use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth; | |
class MoneySynthesizer extends Synth | |
{ | |
public static string $key = 'money'; | |
public static function match(mixed $target): bool | |
{ | |
return $target instanceof Money || | |
(is_array($target) | |
&& isset($target['amount'], $target['currency']) | |
&& is_numeric($target['amount']) | |
&& $target['currency'] instanceof Currency | |
); | |
} | |
public function dehydrate(Money|array $target): array | |
{ | |
if ($target instanceof Money) { | |
return [ | |
[ | |
'amount' => $target->getMinorAmount(), | |
'currency' => $target->getCurrency()->getCurrency(), | |
], | |
[], | |
]; | |
} | |
return [ | |
[ | |
'amount' => $target['amount'], | |
'currency' => $target['currency']->getCurrency(), | |
], | |
[], | |
]; | |
} | |
public function hydrate(array $value): Money | |
{ | |
return money($value['amount'], $value['currency']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment