Test Failed
Push — main ( 706477...92dbda )
by Yaroslav
03:43
created

getParsedFlexibleInputs()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 1
dl 0
loc 15
ccs 0
cts 9
cp 0
crap 6
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
    protected function requestHasParsableFlexibleInputs(Request $request): bool
21
    {
22
        return (
23
            ($request->isMethod('POST') || $request->isMethod('PUT')) &&
24
            $request->filled(FlexibleAttribute::REGISTER_FLEXIBLE_FIELD_NAME)
25
        );
26
    }
27
28
    /**
29
     * Transform the request's flexible values.
30
     *
31
     * @param \Illuminate\Http\Request $request
32
     * @return array
33
     */
34
    protected function getParsedFlexibleInputs(Request $request): array
35
    {
36
        $this->registerFlexibleFields($request->input(FlexibleAttribute::REGISTER_FLEXIBLE_FIELD_NAME));
37
38
        return array_reduce(array_keys($request->all()), function ($carry, $attribute) use ($request) {
39
            $value = $request->input($attribute);
40
41
            if (!$this->isFlexibleAttribute($attribute, $value)) {
42
                return $carry;
43
            }
44
45
            $carry[$attribute] = $this->getParsedFlexibleValue($value);
46
47
            return $carry;
48
        }, []);
49
    }
50
51
    /**
52
     * Apply JSON decode and recursively check for nested values
53
     *
54
     * @param mixed $value
55
     * @return array
56
     */
57
    protected function getParsedFlexibleValue($value)
58
    {
59
        if (is_string($value)) {
60
            $raw = json_decode($value, true);
61
        } else {
62
            $raw = $value;
63
        }
64
65
        if (!is_array($raw)) {
66
            return $value;
67
        }
68
69
        return array_map(function ($group) {
70
            return $this->getParsedFlexibleGroup($group);
71
        }, $raw);
72
    }
73
74
    /**
75
     * Cleans & prepares a filled group
76
     */
77
    protected function getParsedFlexibleGroup(array $group): array
78
    {
79
        $clean = [
80
            'layout'     => $group['layout']    ?? null,
81
            'key'        => $group['key']       ?? null,
82
            'collapsed'  => $group['collapsed'] ?? false,
83
            'attributes' => [],
84
        ];
85
86
        foreach ($group['attributes'] ?? [] as $attribute => $value) {
87
            $this->fillFlexibleAttributes($clean['attributes'], $clean['key'], $attribute, $value);
88
        }
89
90
        foreach ($clean['attributes'] as $attribute => $value) {
91
            if (!$this->isFlexibleAttribute($attribute, $value)) {
92
                continue;
93
            }
94
            $clean['attributes'][$attribute] = $this->getParsedFlexibleValue($value);
95
        }
96
97
        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
    protected function fillFlexibleAttributes(&$attributes, $group, $attribute, $value)
110
    {
111
        $attribute = $this->parseAttribute($attribute, $group);
112
113
        if ($attribute->isFlexibleFieldsRegister()) {
114
            $this->registerFlexibleFields($value, $group);
115
116
            return;
117
        }
118
119
        $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
    protected function parseAttribute($attribute, $group): FlexibleAttribute
130
    {
131
        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
    protected function registerFlexibleFields(array|string|null $value, ? string $group = null): void
142
    {
143
        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
        if (!is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
148
            $value = json_decode($value, true);
149
        }
150
151
        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
        foreach ($value as $attribute) {
156
            $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
    protected function registerFlexibleField($attribute, $group = null)
168
    {
169
        $attribute = $this->parseAttribute(strval($attribute), $group);
170
171
        $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
    protected function isFlexibleAttribute($attribute, $value)
183
    {
184
        if (!$this->getFlexibleAttribute($attribute)) {
185
            return false;
186
        }
187
188
        if (!$value || !is_string($value)) {
189
            return false;
190
        }
191
192
        return true;
193
    }
194
195
    /**
196
     * Retrieve a registered flexible attribute.
197
     */
198
    protected function getFlexibleAttribute(string $attribute): ?FlexibleAttribute
199
    {
200
        foreach ($this->registered as $registered) {
201
            if ($registered->name === $attribute) {
202
                return $registered;
203
            }
204
        }
205
206
        return null;
207
    }
208
}
209