mirror of
https://github.com/pelican-dev/panel.git
synced 2025-06-13 08:11:08 +02:00
44 lines
1.2 KiB
PHP
44 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Casts;
|
|
|
|
use App\Models\Objects\Endpoint;
|
|
use Illuminate\Contracts\Database\Eloquent\Castable;
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class EndpointCollection implements Castable
|
|
{
|
|
public static function castUsing(array $arguments)
|
|
{
|
|
return new class() implements CastsAttributes
|
|
{
|
|
public function get($model, $key, $value, $attributes)
|
|
{
|
|
if (!isset($attributes[$key])) {
|
|
return new Collection();
|
|
}
|
|
|
|
$data = json_decode($attributes[$key], true);
|
|
|
|
return (new Collection($data))->map(function ($value) {
|
|
return new Endpoint($value);
|
|
});
|
|
}
|
|
|
|
public function set($model, $key, $value, $attributes)
|
|
{
|
|
if (!is_array($value) && !$value instanceof Collection) {
|
|
return new Collection();
|
|
}
|
|
|
|
if (!$value instanceof Collection) {
|
|
$value = new Collection($value);
|
|
}
|
|
|
|
return $value->map(fn ($endpoint) => (string) $endpoint);
|
|
}
|
|
};
|
|
}
|
|
}
|