Skip to content

Commit

Permalink
Implement Arrayable and SerializesCastableAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
glennjacobs committed Sep 15, 2023
1 parent 36c5672 commit 25a3f06
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
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,
];
}
}
2 changes: 1 addition & 1 deletion packages/core/tests/Unit/Models/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function can_serialize_an_order()

$data = $order->toArray();

$this->assertIsInt($order->total);
$this->assertIsInt($data['total']['value']);
}

/** @test */
Expand Down

0 comments on commit 25a3f06

Please sign in to comment.