From 36c5672de3a44de49c2f088c6ef8f8ab58b0a2da Mon Sep 17 00:00:00 2001 From: Glenn Jacobs Date: Fri, 15 Sep 2023 18:10:19 +0100 Subject: [PATCH 1/2] Update OrderTest.php --- packages/core/tests/Unit/Models/OrderTest.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/core/tests/Unit/Models/OrderTest.php b/packages/core/tests/Unit/Models/OrderTest.php index a1e9be1a7d..985e76b101 100644 --- a/packages/core/tests/Unit/Models/OrderTest.php +++ b/packages/core/tests/Unit/Models/OrderTest.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Lunar\Base\ValueObjects\Cart\ShippingBreakdown; use Lunar\Base\ValueObjects\Cart\ShippingBreakdownItem; +use Lunar\Base\OrderReferenceGenerator; use Lunar\DataTypes\Price; use Lunar\Models\Cart; use Lunar\Models\Currency; @@ -17,6 +18,7 @@ use Lunar\Models\Transaction; use Lunar\Tests\Stubs\User; use Lunar\Tests\TestCase; +use Ramsey\Uuid\Type\Integer; /** * @group lunar.orders @@ -72,6 +74,29 @@ public function can_make_an_order() $this->assertDatabaseHas((new Order())->getTable(), $data); } + /** @test */ + public function can_serialize_an_order() + { + Currency::factory()->create([ + 'default' => true, + ]); + + $order = Order::factory()->create([ + 'user_id' => null, + 'tax_breakdown' => [ + [ + 'description' => 'VAT', + 'total' => 99, + 'percentage' => 20, + ], + ], + ]); + + $data = $order->toArray(); + + $this->assertIsInt($order->total); + } + /** @test */ public function order_has_correct_casting() { From 25a3f06af3c1f65e6f9b2a186ee9f7c3b31a025d Mon Sep 17 00:00:00 2001 From: Glenn Jacobs Date: Fri, 15 Sep 2023 18:37:39 +0100 Subject: [PATCH 2/2] Implement Arrayable and SerializesCastableAttributes --- packages/core/src/Base/Casts/Price.php | 16 +++++++++++++++- packages/core/src/Base/Casts/TaxBreakdown.php | 11 +---------- packages/core/src/DataTypes/Price.php | 18 +++++++++++++++++- packages/core/tests/Unit/Models/OrderTest.php | 2 +- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/packages/core/src/Base/Casts/Price.php b/packages/core/src/Base/Casts/Price.php index 1f59878679..3d1be4a38d 100644 --- a/packages/core/src/Base/Casts/Price.php +++ b/packages/core/src/Base/Casts/Price.php @@ -3,11 +3,12 @@ namespace Lunar\Base\Casts; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; +use Illuminate\Contracts\Database\Eloquent\SerializesCastableAttributes; use Illuminate\Support\Facades\Validator; use Lunar\DataTypes\Price as PriceDataType; use Lunar\Models\Currency; -class Price implements CastsAttributes +class Price implements CastsAttributes, SerializesCastableAttributes { /** * Cast the given value. @@ -55,4 +56,17 @@ public function set($model, $key, $value, $attributes) $key => $value, ]; } + + /** + * Get the serialized representation of the value. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @param \Illuminate\Support\Collection $value + * @param array $attributes + */ + public function serialize($model, $key, $value, $attributes) + { + return $value->toArray(); + } } diff --git a/packages/core/src/Base/Casts/TaxBreakdown.php b/packages/core/src/Base/Casts/TaxBreakdown.php index 562efd2cea..41cf88cd38 100644 --- a/packages/core/src/Base/Casts/TaxBreakdown.php +++ b/packages/core/src/Base/Casts/TaxBreakdown.php @@ -66,16 +66,7 @@ public function set($model, $key, $value, $attributes) public function serialize($model, $key, $value, $attributes) { return $value->map(function ($rate) { - $rate = is_array($rate) ? (object) $rate : $rate; - - if ($rate->total instanceof Price) { - $rate->total = (object) [ - 'value' => $rate->total->value, - 'formatted' => $rate->total->formatted, - 'currency' => $rate->total->currency->toArray(), - ]; - } - + $rate->total = $rate->total->toArray(); return $rate; })->toJson(); } diff --git a/packages/core/src/DataTypes/Price.php b/packages/core/src/DataTypes/Price.php index 7aa386b7bd..40f39a0bcd 100644 --- a/packages/core/src/DataTypes/Price.php +++ b/packages/core/src/DataTypes/Price.php @@ -2,11 +2,12 @@ namespace Lunar\DataTypes; +use Illuminate\Contracts\Support\Arrayable; use Lunar\Exceptions\InvalidDataTypeValueException; use Lunar\Models\Currency; use Lunar\Pricing\DefaultPriceFormatter; -class Price +class Price implements Arrayable { /** * Initialise the Price datatype. @@ -100,4 +101,19 @@ protected function formatValue(int|float $value, ...$arguments): mixed { return $this->formatter()->formatValue($value, ...$arguments); } + + /** + * Get the instance as an array. + * + * @return array + */ + public function toArray() + { + return [ + 'value' => $this->value, + 'decimal' => $this->decimal, + 'formatted' => $this->formatted, + 'currency' => $this->currency->code, + ]; + } } diff --git a/packages/core/tests/Unit/Models/OrderTest.php b/packages/core/tests/Unit/Models/OrderTest.php index 985e76b101..e1b8a3f956 100644 --- a/packages/core/tests/Unit/Models/OrderTest.php +++ b/packages/core/tests/Unit/Models/OrderTest.php @@ -94,7 +94,7 @@ public function can_serialize_an_order() $data = $order->toArray(); - $this->assertIsInt($order->total); + $this->assertIsInt($data['total']['value']); } /** @test */