38 lines
1.2 KiB
PHP
38 lines
1.2 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Casts;
|
||
|
|
|
||
|
|
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
|
||
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
|
||
|
|
/** @implements CastsAttributes<ShopperEstablishmentCode, ShopperEstablishmentCode|string> */
|
||
|
|
class ShopperEstablishmentCodeCast implements CastsAttributes
|
||
|
|
{
|
||
|
|
/** @param array<string, mixed> $attributes */
|
||
|
|
public function get(Model $model, string $key, mixed $value, array $attributes): ShopperEstablishmentCode
|
||
|
|
{
|
||
|
|
if ($value === null || ! is_string($value)) {
|
||
|
|
throw new \UnexpectedValueException('ShopperEstablishmentCode must be a string.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return new ShopperEstablishmentCode($value);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @param array<string, mixed> $attributes */
|
||
|
|
public function set(Model $model, string $key, mixed $value, array $attributes): string
|
||
|
|
{
|
||
|
|
if (is_string($value)) {
|
||
|
|
$value = new ShopperEstablishmentCode($value);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (! $value instanceof ShopperEstablishmentCode) {
|
||
|
|
throw new \UnexpectedValueException('The given value is not an instance of ShopperEstablishmentCode.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return $value->value;
|
||
|
|
}
|
||
|
|
}
|