44 lines
1.3 KiB
PHP
44 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Casts;
|
|
|
|
use App\Enums\PartnerType;
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class PartnerTypeCast implements CastsAttributes
|
|
{
|
|
/**
|
|
* Normalisiert DB-Werte (z.B. "retailer", "broker") zu gültigen Enum-Backing-Values.
|
|
*
|
|
* @param array<string, mixed> $attributes
|
|
*/
|
|
public function get(Model $model, string $key, mixed $value, array $attributes): ?PartnerType
|
|
{
|
|
if ($value === null || $value === '') {
|
|
return null;
|
|
}
|
|
|
|
$normalized = match (strtolower((string) $value)) {
|
|
'retailer' => PartnerType::Retailer->value,
|
|
'manufacturer' => PartnerType::Manufacturer->value,
|
|
'estate-agent', 'broker' => PartnerType::EstateAgent->value,
|
|
'customer' => PartnerType::Customer->value,
|
|
default => $value,
|
|
};
|
|
|
|
return PartnerType::tryFrom($normalized);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $attributes
|
|
*/
|
|
public function set(Model $model, string $key, mixed $value, array $attributes): ?string
|
|
{
|
|
if ($value === null) {
|
|
return null;
|
|
}
|
|
|
|
return $value instanceof PartnerType ? $value->value : (string) $value;
|
|
}
|
|
}
|