LaravelFreelancerNL /
laravel-arangodb
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace LaravelFreelancerNL\Aranguent\Eloquent\Casts; |
||
| 6 | |||
| 7 | use Illuminate\Contracts\Database\Eloquent\CastsAttributes; |
||
| 8 | use Illuminate\Database\Eloquent\Casts\AsCollection as IlluminateAsCollection; |
||
| 9 | use Illuminate\Support\Collection; |
||
| 10 | use InvalidArgumentException; |
||
| 11 | use LaravelFreelancerNL\Aranguent\Eloquent\Model; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @SuppressWarnings("PHPMD.UndefinedVariable") |
||
| 15 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||
| 16 | */ |
||
| 17 | class AsCollection extends IlluminateAsCollection |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Get the caster class to use when casting from / to this cast target. |
||
| 21 | * |
||
| 22 | * @param array<array-key, mixed> $arguments |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 23 | * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\Collection<array-key, mixed>, iterable<mixed>> |
||
| 24 | */ |
||
| 25 | 1 | public static function castUsing(array $arguments) |
|
| 26 | { |
||
| 27 | 1 | return new class ($arguments) implements CastsAttributes { |
|
| 28 | /** |
||
| 29 | * @param array<array-key, mixed> $arguments |
||
|
0 ignored issues
–
show
|
|||
| 30 | */ |
||
| 31 | 1 | public function __construct(protected array $arguments) {} |
|
| 32 | |||
| 33 | /** |
||
| 34 | * @param $model |
||
| 35 | * @param $key |
||
| 36 | * @param $value |
||
| 37 | * @param $attributes |
||
| 38 | * @return Collection|mixed|void|null |
||
| 39 | * |
||
| 40 | * @SuppressWarnings("PHPMD.UndefinedVariable") |
||
| 41 | */ |
||
| 42 | public function get($model, $key, $value, $attributes) |
||
| 43 | { |
||
| 44 | 1 | if (! isset($attributes[$key])) { |
|
| 45 | return; |
||
| 46 | } |
||
| 47 | |||
| 48 | 1 | $data = $attributes[$key]; |
|
| 49 | |||
| 50 | 1 | if (is_object($data)) { |
|
| 51 | $data = (array) $data; |
||
| 52 | } |
||
| 53 | |||
| 54 | |||
| 55 | 1 | $collectionClass = $this->arguments[0] ?? Collection::class; |
|
| 56 | |||
| 57 | 1 | if (! is_a($collectionClass, Collection::class, true)) { |
|
| 58 | throw new InvalidArgumentException('The provided class must extend [' . Collection::class . '].'); |
||
| 59 | } |
||
| 60 | |||
| 61 | 1 | return is_array($data) ? new $collectionClass($data) : null; |
|
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param Model $model |
||
| 66 | * @param string $key |
||
| 67 | * @param mixed $value |
||
| 68 | * @param mixed[] $attributes |
||
| 69 | * @return mixed[] |
||
| 70 | * |
||
| 71 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||
| 72 | */ |
||
| 73 | public function set($model, $key, $value, $attributes) |
||
| 74 | { |
||
| 75 | 1 | return [$key => $value]; |
|
| 76 | } |
||
| 77 | 1 | }; |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Specify the collection for the cast. |
||
| 82 | * |
||
| 83 | * @param class-string $class |
||
|
0 ignored issues
–
show
|
|||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public static function using($class) |
||
| 87 | { |
||
| 88 | return static::class . ':' . $class; |
||
| 89 | } |
||
| 90 | } |
||
| 91 |