ldc-cocotte/app/Casts/OrderReferenceCast.php

36 lines
1 KiB
PHP
Raw Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace App\Casts;
use Domain\Models\ValueObjects\OrderReference;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Ramsey\Uuid\Uuid;
2026-06-14 21:32:07 +02:00
/**
* @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));
2026-06-14 21:32:07 +02:00
}
/** @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();
2026-06-14 21:32:07 +02:00
}
}