| @@ -2,9 +2,11 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Give\Framework\Support\ValueObjects; |
| 4 | 4 | |
| 5 | 5 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 | +use Give\Framework\Support\Contracts\Arrayable; | |
| 6 | 7 | use Give\Framework\Support\Currency; |
| 8 | +use JsonSerializable; | |
| 7 | 9 | use Money\Currency as VendorCurrency; |
| 8 | 10 | use Money\Money as VendorMoney; |
| 9 | 11 | |
| 10 | 12 | /** |
| @@ -10,8 +12,9 @@ | ||
| 10 | 12 | /** |
| 11 | 13 | * A decorator class for the vendor Money class which adds additional formatting and other convenience methods. Try and |
| 12 | 14 | * keep the vendor Money logic in the Currency facade. |
| 13 | 15 | * |
| 16 | + * @since 4.0.0 added JsonSerializable interface | |
| 14 | 17 | * @since 2.20.0 |
| 15 | 18 | * |
| 16 | 19 | * @method bool equals(Money $money ) |
| 17 | 20 | * @method Money subtract(Money $money) |
| @@ -18,9 +21,9 @@ | ||
| 18 | 21 | * @method Money add(Money $money) |
| 19 | 22 | * |
| 20 | 23 | * @mixin VendorMoney |
| 21 | 24 | */ |
| 22 | -class Money | |
| 25 | +class Money implements JsonSerializable, Arrayable | |
| 23 | 26 | { |
| 24 | 27 | /** |
| 25 | 28 | * @var VendorMoney |
| 26 | 29 | */ |
| @@ -154,6 +157,26 @@ | ||
| 154 | 157 | */ |
| 155 | 158 | public static function fromDecimal($amount, string $currency): Money |
| 156 | 159 | { |
| 157 | 160 | return self::fromMoney(Currency::parseFromDecimal($amount, $currency)); |
| 161 | + } | |
| 162 | + | |
| 163 | + /** | |
| 164 | + * @since 4.0.0 | |
| 165 | + */ | |
| 166 | + public function toArray(): array | |
| 167 | + { | |
| 168 | + return [ | |
| 169 | + 'value' => $this->formatToDecimal(), | |
| 170 | + 'valueInMinorUnits' => $this->formatToMinorAmount(), | |
| 171 | + 'currency' => $this->amount->getCurrency()->getCode(), | |
| 172 | + ]; | |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 176 | + * @since 4.0.0 | |
| 177 | + */ | |
| 178 | + public function jsonSerialize(): array | |
| 179 | + { | |
| 180 | + return $this->toArray(); | |
| 158 | 181 | } |
| 159 | 182 | } |