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