anonymous//src/Eloquent/Casts/AsCollection.php$0   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 49
ccs 9
cts 12
cp 0.75
rs 10
wmc 7
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
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
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
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
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
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
84
     * @return string
85
     */
86
    public static function using($class)
87
    {
88
        return static::class . ':' . $class;
89
    }
90
}
91