ldc-boutique-cnc-sable/app/Casts/OrderReferenceCast.php

35 lines
1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Casts;
use Domain\Models\ValueObjects\OrderReference;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Ramsey\Uuid\Uuid;
/**
* @implements CastsAttributes<OrderReference,OrderReference>
*/
class OrderReferenceCast implements CastsAttributes
{
/** @param array<string, mixed> $attributes */
public function get($model, string $key, $value, array $attributes): OrderReference
{
if ($value === null || ! is_string($value)) {
throw new \UnexpectedValueException('OrderReference cannot be null.');
}
return new OrderReference(Uuid::fromString($value));
}
/** @param array<string, mixed> $attributes */
public function set($model, string $key, $value, array $attributes): string
{
if (! $value instanceof OrderReference) {
throw new \UnexpectedValueException('The given value is not an instance of OrderReference.');
}
return $value->value->toString();
}
}