Build Laravel 13 ticket assistant with Docker, Livewire admin, and helpdesk scraper command
This commit is contained in:
36
app/Casts/VectorCast.php
Normal file
36
app/Casts/VectorCast.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?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).']';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user