Files
TicketAssistent/app/Casts/VectorCast.php

36 lines
845 B
PHP

<?php
namespace App\Casts;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
class VectorCast implements CastsAttributes
{
public function get($model, string $key, $value, array $attributes): ?array
{
if ($value === null) {
return null;
}
if (is_array($value)) {
return $value;
}
$trimmed = trim((string) $value, '[]');
if ($trimmed === '') {
return [];
}
return array_map(static fn ($item) => (float) $item, explode(',', $trimmed));
}
public function set($model, string $key, $value, array $attributes): ?string
{
if ($value === null) {
return null;
}
$vector = array_map(static fn ($item) => (float) $item, $value);
return '['.implode(',', $vector).']';
}
}