Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serialization to Price cast #1251

Closed
wants to merge 2 commits into from
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
16 changes: 15 additions & 1 deletion packages/core/src/Base/Casts/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<string, mixed> $attributes
*/
public function serialize($model, $key, $value, $attributes)
{
return $value->toArray();
}
}
11 changes: 1 addition & 10 deletions packages/core/src/Base/Casts/TaxBreakdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
18 changes: 17 additions & 1 deletion packages/core/src/DataTypes/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<TKey, TValue>
*/
public function toArray()
{
return [
'value' => $this->value,
'decimal' => $this->decimal,
'formatted' => $this->formatted,
'currency' => $this->currency->code,
];
}
}
25 changes: 25 additions & 0 deletions packages/core/tests/Unit/Models/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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($data['total']['value']);
}

/** @test */
public function order_has_correct_casting()
{
Expand Down