Passed
Push — main ( b3cf5c...f07e9b )
by Yaroslav
18:34
created

ModelEmulates::__get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NovaFlexibleContent\Layouts\LayoutTraits;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use NovaFlexibleContent\Layouts\Collections\LayoutsCollection;
8
use NovaFlexibleContent\Layouts\Layout;
9
use NovaFlexibleContent\Layouts\Preset;
10
11
trait ModelEmulates
12
{
13
    use AttributesManipulation;
14
15
    /**
16
     * Define that Layout is a model, when in fact it is not.
17
     *
18
     * @var bool
19
     */
20
    protected bool $exists = false;
21
22
    /**
23
     * Define that Layout is a model, when in fact it is not.
24
     *
25
     * @var bool
26
     */
27
    protected bool $wasRecentlyCreated = false;
28
29
    /**
30
     * The relation resolver callbacks for the Layout.
31
     *
32
     * @var array
33
     */
34
    protected static array $relationResolvers = [];
35
36
    /**
37
     * The loaded relationships for the Layout.
38
     *
39
     * @var array
40
     */
41
    protected $relations = [];
42
43
    /**
44
     * Check if relation exists.
45
     * Layouts do not have relations.
46
     */
47 9
    public function relationLoaded($key): bool
48
    {
49 9
        return false;
50
    }
51
52
    /**
53
     * Get the value indicating whether the IDs are incrementing.
54
     * Layouts do not have increment identifier.
55
     */
56 13
    public function getIncrementing(): bool
57
    {
58 13
        return false;
59
    }
60
61
    /**
62
     * Determine if the model uses timestamps.
63
     * Layouts do not use timestamps.
64
     */
65 13
    public function usesTimestamps(): bool
66
    {
67 13
        return false;
68
    }
69
70
    /**
71
     * Get the dynamic relation resolver if defined or inherited, or return null.
72
     * Since it is not possible to define a relation on a layout, this method
73
     * returns null
74
     *
75
     * @param string $class
76
     * @param string $key
77
     * @return mixed
78
     */
79 9
    public function relationResolver($class, $key)
80
    {
81 9
        return null;
82
    }
83
84
    /**
85
     * Force Fill the layout with an array of attributes.
86
     */
87 5
    public function forceFill(array $attributes)
88
    {
89 5
        foreach ($attributes as $key => $value) {
90 5
            $attribute = Str::replace('->', '.', $key);
91 5
            Arr::set($this->attributes, $attribute, $value);
0 ignored issues
show
Bug introduced by
It seems like $attribute can also be of type string[]; however, parameter $key of Illuminate\Support\Arr::set() does only seem to accept integer|null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
            Arr::set($this->attributes, /** @scrutinizer ignore-type */ $attribute, $value);
Loading history...
92
        }
93
94 5
        return $this;
95
    }
96
97 1
    public function groups(string $fieldName, string|array|null $type = null): LayoutsCollection
98
    {
99 1
        $methodName = "{$fieldName}Preset";
100 1
        if ($fieldName && method_exists($this, $methodName)) {
101 1
            $preset = $this->$methodName();
102 1
            if ($preset instanceof Preset) {
103 1
                $value = $this->flexible($fieldName, $preset);
0 ignored issues
show
Bug introduced by
It seems like flexible() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
                /** @scrutinizer ignore-call */ 
104
                $value = $this->flexible($fieldName, $preset);
Loading history...
104 1
                if ($type) {
105 1
                    $value = $value->whereName($type);
106
                }
107
108 1
                return $value;
109
            }
110
        }
111
112 1
        return LayoutsCollection::make();
113
    }
114
115 1
    public function group(string $fieldName, string|array|null $type = null): ?Layout
116
    {
117 1
        return $this->groups($fieldName, $type)->first();
118
    }
119
120 2
    public function __get($key)
121
    {
122 2
        if (Str::startsWith($key, 'flexible')) {
123 1
            $field = Str::camel(Str::after($key, 'flexible'));
124
125 1
            if ($value = $this->groups($field)) {
126 1
                return $value;
127
            }
128
        }
129
130 2
        return parent::__get($key);
131
    }
132
}
133