getParsedFlexibleInputs()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NovaFlexibleContent\Http;
4
5
use Illuminate\Http\Request;
6
7
trait ParsesFlexibleAttributes
8
{
9
    /**
10
     * The registered flexible field attributes.
11
     */
12
    protected array $registered = [];
13
14
    /**
15
     * Check if given request should be handled by the middleware.
16
     *
17
     * @param \Illuminate\Http\Request $request
18
     * @return bool
19
     */
20 9
    protected function requestHasParsableFlexibleInputs(Request $request): bool
21
    {
22 9
        return (
23 9
            ($request->isMethod('POST') || $request->isMethod('PUT')) &&
24 9
            $request->filled(FlexibleAttribute::REGISTER_FLEXIBLE_FIELD_NAME)
25 9
        );
26
    }
27
28
    /**
29
     * Transform the request's flexible values.
30
     *
31
     * @param \Illuminate\Http\Request $request
32
     * @return array
33
     */
34 5
    protected function getParsedFlexibleInputs(Request $request): array
35
    {
36 5
        $this->registerFlexibleFields($request->input(FlexibleAttribute::REGISTER_FLEXIBLE_FIELD_NAME));
37
38 5
        return array_reduce(array_keys($request->all()), function ($carry, $attribute) use ($request) {
39 5
            $value = $request->input($attribute);
40
41 5
            if (!$this->isFlexibleAttribute($attribute, $value)) {
42 5
                return $carry;
43
            }
44
45 5
            $carry[$attribute] = $this->getParsedFlexibleValue($value);
46
47 5
            return $carry;
48 5
        }, []);
49
    }
50
51
    /**
52
     * Apply JSON decode and recursively check for nested values
53
     *
54
     * @param mixed $value
55
     * @return array
56
     */
57 5
    protected function getParsedFlexibleValue($value)
58
    {
59 5
        if (is_string($value)) {
60 5
            $raw = json_decode($value, true);
61
        } else {
62
            $raw = $value;
63
        }
64
65 5
        if (!is_array($raw)) {
66
            return $value;
67
        }
68
69 5
        return array_map(function ($group) {
70 5
            return $this->getParsedFlexibleGroup($group);
71 5
        }, $raw);
72
    }
73
74
    /**
75
     * Cleans & prepares a filled group
76
     */
77 5
    protected function getParsedFlexibleGroup(array $group): array
78
    {
79 5
        $clean = [
80 5
            'layout'     => $group['layout']    ?? null,
81 5
            'key'        => $group['key']       ?? null,
82 5
            'collapsed'  => $group['collapsed'] ?? false,
83 5
            'attributes' => [],
84 5
        ];
85
86 5
        foreach ($group['attributes'] ?? [] as $attribute => $value) {
87 5
            $this->fillFlexibleAttributes($clean['attributes'], $clean['key'], $attribute, $value);
88
        }
89
90 5
        foreach ($clean['attributes'] as $attribute => $value) {
91 5
            if (!$this->isFlexibleAttribute($attribute, $value)) {
92 5
                continue;
93
            }
94 1
            $clean['attributes'][$attribute] = $this->getParsedFlexibleValue($value);
95
        }
96
97 5
        return $clean;
98
    }
99
100
    /**
101
     * Fill a flexible group's attributes with cleaned attributes & values
102
     *
103
     * @param array $attributes
104
     * @param string $group
105
     * @param string $attribute
106
     * @param string $value
107
     * @return void
108
     */
109 5
    protected function fillFlexibleAttributes(&$attributes, $group, $attribute, $value)
110
    {
111 5
        $attribute = $this->parseAttribute($attribute, $group);
112
113 5
        if ($attribute->isFlexibleFieldsRegister()) {
114 1
            $this->registerFlexibleFields($value, $group);
115
116 1
            return;
117
        }
118
119 5
        $attribute->setDataIn($attributes, $value);
120
    }
121
122
    /**
123
     * Analyse and clean up the raw attribute
124
     *
125
     * @param string $attribute
126
     * @param string $group
127
     * @return \NovaFlexibleContent\Http\FlexibleAttribute
128
     */
129 5
    protected function parseAttribute($attribute, $group): FlexibleAttribute
130
    {
131 5
        return new FlexibleAttribute($attribute, $group);
132
    }
133
134
    /**
135
     * Add flexible attributes to the register
136
     *
137
     * @param null|string $value
138
     * @param null|string $group
139
     * @return void
140
     */
141 5
    protected function registerFlexibleFields(array|string|null $value, ? string $group = null): void
142
    {
143 5
        if (!$value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
144
            return;
145
        }
146
147 5
        if (!is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
148 5
            $value = json_decode($value, true);
149
        }
150
151 5
        if (!is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
152
            return;
153
        }
154
155 5
        foreach ($value as $attribute) {
156 5
            $this->registerFlexibleField($attribute, $group);
157
        }
158
    }
159
160
    /**
161
     * Add an attribute to the register
162
     *
163
     * @param mixed $attribute
164
     * @param null|string $group
165
     * @return void
166
     */
167 5
    protected function registerFlexibleField($attribute, $group = null)
168
    {
169 5
        $attribute = $this->parseAttribute(strval($attribute), $group);
170
171 5
        $this->registered[] = $attribute;
172
    }
173
174
    /**
175
     * Check if given attribute is a registered and usable
176
     * flexible attribute
177
     *
178
     * @param string $attribute
179
     * @param mixed $value
180
     * @return bool
181
     */
182 5
    protected function isFlexibleAttribute($attribute, $value)
183
    {
184 5
        if (!$this->getFlexibleAttribute($attribute)) {
185 5
            return false;
186
        }
187
188 5
        if (!$value || !is_string($value)) {
189
            return false;
190
        }
191
192 5
        return true;
193
    }
194
195
    /**
196
     * Retrieve a registered flexible attribute.
197
     */
198 5
    protected function getFlexibleAttribute(string $attribute): ?FlexibleAttribute
199
    {
200 5
        foreach ($this->registered as $registered) {
201 5
            if ($registered->name === $attribute) {
202 5
                return $registered;
203
            }
204
        }
205
206 5
        return null;
207
    }
208
}
209