148 lines
5.7 KiB
PHP
148 lines
5.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Infrastructure\Adapters\Serializers;
|
|
|
|
use Domain\Enums\ProductBrand;
|
|
use Domain\Enums\TaxRate;
|
|
use Domain\Enums\Units\OrderUnit;
|
|
use Domain\Models\Entities\Basket;
|
|
use Domain\Models\ValueObjects\BasketProduct;
|
|
use Domain\Models\ValueObjects\DiscountRate;
|
|
use Domain\Models\ValueObjects\Entities\BasketId;
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
|
use Domain\Models\ValueObjects\IncoId;
|
|
use Illuminate\Support\Arr;
|
|
use Money\Currency;
|
|
use Money\Money;
|
|
use Shared\Helpers\UuidHelper;
|
|
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
class BasketSerializer
|
|
{
|
|
/** @return array<string,mixed> */
|
|
public function toArray(Basket $basket): array
|
|
{
|
|
return [
|
|
'id' => (string) $basket->id->value,
|
|
'items' => array_map(
|
|
fn (BasketProduct $item) => [
|
|
'id' => (string) $item->id->value,
|
|
'name' => $item->name,
|
|
'qty' => $item->qty,
|
|
'unitPriceHt' => $item->unitPriceHt,
|
|
'unitDiscount' => $item->unitDiscount,
|
|
'taxRate' => $item->taxRate->value,
|
|
'weightGrams' => $item->weightGrams,
|
|
'brand' => $item->brand?->value,
|
|
'incoId' => $item->incoId?->value,
|
|
'orderUnit' => $item->orderUnit->value,
|
|
'slug' => $item->slug,
|
|
'picture' => $item->picture,
|
|
'description' => $item->description,
|
|
'weightVariable' => $item->weightVariable,
|
|
'discountRateBp' => $item->discountRate->basisPoints(),
|
|
'priceKgTtc' => $item->priceKgTtc,
|
|
'options' => $item->options,
|
|
],
|
|
array_values($basket->items())
|
|
),
|
|
];
|
|
}
|
|
|
|
/** @param array<string,mixed> $data */
|
|
public function fromArray(array $data): Basket
|
|
{
|
|
$basketId = Arr::string($data, 'id');
|
|
$rawItems = Arr::array($data, 'items', []);
|
|
|
|
$hydratedItems = [];
|
|
|
|
foreach ($rawItems as $rawItem) {
|
|
$itemData = Arr::wrap($rawItem);
|
|
$id = Arr::string($itemData, 'id');
|
|
$hydratedItems[$id] = $this->hydrateBasketProduct($id, $itemData);
|
|
}
|
|
|
|
return new Basket(
|
|
id: new BasketId(UuidHelper::fromString($basketId)),
|
|
items: $hydratedItems
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<array-key,mixed> $itemData
|
|
*/
|
|
private function hydrateBasketProduct(string $id, array $itemData): BasketProduct
|
|
{
|
|
$brand = Arr::get($itemData, 'brand'); // using get because it can be null
|
|
$incoId = Arr::get($itemData, 'incoId');
|
|
$picture = Arr::get($itemData, 'picture');
|
|
$description = Arr::get($itemData, 'description');
|
|
$options = Arr::array($itemData, 'options', []);
|
|
|
|
foreach ($options as $key => $option) {
|
|
Assert::string($key);
|
|
}
|
|
|
|
return new BasketProduct(
|
|
id: new ProductId(UuidHelper::fromString($id)),
|
|
name: Arr::string($itemData, 'name'),
|
|
qty: Arr::integer($itemData, 'qty'),
|
|
unitPriceHt: $this->hydrateMoney($id, 'unitPriceHt', Arr::array($itemData, 'unitPriceHt')),
|
|
unitDiscount: $this->hydrateMoney($id, 'unitDiscount', Arr::array($itemData, 'unitDiscount')),
|
|
taxRate: TaxRate::from(Arr::integer($itemData, 'taxRate')),
|
|
weightGrams: Arr::integer($itemData, 'weightGrams'),
|
|
brand: is_string($brand) && $brand !== '' ? ProductBrand::from($brand) : null,
|
|
incoId: is_scalar($incoId) && $incoId !== 0 ? new IncoId((int) $incoId) : null,
|
|
orderUnit: OrderUnit::from(Arr::string($itemData, 'orderUnit')),
|
|
slug: Arr::string($itemData, 'slug'),
|
|
picture: is_string($picture) && $picture !== '' ? new StorageFilePath($picture) : null,
|
|
description: is_string($description) && $description !== '' ? $description : null,
|
|
weightVariable: Arr::boolean($itemData, 'weightVariable'),
|
|
discountRate: $this->hydrateDiscountRate($itemData),
|
|
priceKgTtc: $this->hydrateMoney($id, 'priceKgTtc', Arr::array($itemData, 'priceKgTtc')),
|
|
options: $options
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<array-key,mixed> $moneyData
|
|
*/
|
|
private function hydrateMoney(string $itemId, string $field, array $moneyData): Money
|
|
{
|
|
$currency = Arr::string($moneyData, 'currency');
|
|
if ($currency === '') {
|
|
throw new \DomainException("Currency is required for {$field} of item with id {$itemId}");
|
|
}
|
|
|
|
$rawAmount = Arr::get($moneyData, 'amount', 0);
|
|
|
|
return new Money(
|
|
is_scalar($rawAmount) ? (int) $rawAmount : 0,
|
|
new Currency($currency)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Reads the discount rate in basis points; falls back to the legacy float ratio
|
|
* (key "discountRate") stored in sessions created before the DiscountRate value object.
|
|
*
|
|
* @param array<array-key,mixed> $itemData
|
|
*/
|
|
private function hydrateDiscountRate(array $itemData): DiscountRate
|
|
{
|
|
$basisPoints = Arr::get($itemData, 'discountRateBp');
|
|
if (is_int($basisPoints)) {
|
|
return DiscountRate::fromBasisPoints($basisPoints);
|
|
}
|
|
|
|
$legacyRatio = Arr::get($itemData, 'discountRate', 0.0);
|
|
|
|
return is_numeric($legacyRatio)
|
|
? DiscountRate::fromRatio(min(max((float) $legacyRatio, 0.0), 1.0))
|
|
: DiscountRate::zero();
|
|
}
|
|
}
|