Passed
Push — master ( 0a12ca...ae750b )
by Mike
01:43
created

ArrayHandler::handleArrayWildcard()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Xervice\ArrayHandler\Business\Model;
5
6
7
use Xervice\ArrayHandler\Business\Exception\ArrayHandlerException;
8
use Xervice\ArrayHandler\Dependency\FieldHandlerPluginInterface;
9
10
class ArrayHandler implements ArrayHandlerInterface
11
{
12
    /**
13
     * @var \Xervice\ArrayHandler\Dependency\FieldHandlerPluginInterface
14
     */
15
    private $fieldHandler;
16
17
    /**
18
     * ArrayHandler constructor.
19
     *
20
     * @param \Xervice\ArrayHandler\Dependency\FieldHandlerPluginInterface $fieldHandler
21
     */
22 3
    public function __construct(
23
        FieldHandlerPluginInterface $fieldHandler
24
    ) {
25 3
        $this->fieldHandler = $fieldHandler;
26 3
    }
27
28
    /**
29
     * Cases:
30
     * '*' => callable
31
     * '*' => [ [...] ]
32
     * '*' => [ ... => [], ...]
33
     * int => 'string',
34
     * 'string' => 'string',
35
     * 'string' => [ [...] ]
36
     * 'string' => [ ... => [], ... ]
37
     * 'string' => callable
38
     * 'string.*' => [ [...] ]
39
     * 'string.*' => [ ... => [], ... ]
40
     * 'string.*' => callable
41
     *
42
     * @param array $payload
43
     * @param array $config
44
     *
45
     * @return array
46
     * @throws \Xervice\ArrayHandler\Business\Exception\ArrayHandlerException
47
     */
48 3
    public function handleConfig(array $payload, array $config): array
49
    {
50 3
        foreach ($config as $key => $configItem) {
51 3
            $payload = $this->handleByType($payload, $key, $configItem);
52
        }
53
54 3
        return $payload;
55
    }
56
57
    /**
58
     * @param array $payload
59
     * @param mixed $key
60
     * @param mixed $config
61
     *
62
     * @return array
63
     * @throws \Xervice\ArrayHandler\Business\Exception\ArrayHandlerException
64
     */
65 3
    protected function handleByType(array $payload, $key, $config): array
66
    {
67 3
        if (is_string($key) && is_callable($config)) {
68 2
            $payload = $this->handleCallable($payload, $key, $config);
69 2
        } elseif (is_string($config)) {
70 2
            $payload = $this->handleString($payload, $key, $config);
71 1
        } elseif (is_array($config)) {
72 1
            $payload = $this->handleArray($payload, $key, $config);
73
        } else {
74
            throw new ArrayHandlerException('Config data is invalid.');
75
        }
76
77 3
        return $payload;
78
    }
79
80
    /**
81
     * Cases:
82
     * '*' => [ [...] ]
83
     * '*' => [ ... => [], ...]
84
     * 'string' => [ [...] ]
85
     * 'string' => [ ... => [], ... ]
86
     * 'string.*' => [ [...] ]
87
     * 'string.*' => [ ... => [], ... ]
88
     * int => []
89
     *
90
     * @param array $payload
91
     * @param mixed $key
92
     * @param array $config
93
     *
94
     * @return array
95
     */
96 1
    protected function handleArray(array $payload, $key, array $config): array
97
    {
98 1
        if ($key === '*') {
99 1
            $payload = $this->handleArrayWildcard($payload, $config);
100 1
        } elseif (is_string($key) && strpos($key, '.*') !== false) {
101 1
            $payload = $this->handleArraySubwildcard($payload, $key, $config);
102 1
        } elseif (is_int($key) && is_array($config)) {
103 1
            $payload = $this->handleConfig($payload, $config);
104
        } else {
105 1
            $payload = $this->handleArrayConfig($payload, $key, $config);
106
        }
107
108 1
        return $payload;
109
    }
110
111
    /**
112
     * Cases:
113
     * int => 'string',
114
     * 'string' => 'string',
115
     *
116
     * @param array $payload
117
     * @param mixed $key
118
     * @param string $configItem
119
     *
120
     * @return array
121
     */
122 2
    protected function handleString(array $payload, $key, string $configItem): array
123
    {
124 2
        if (is_int($key)) {
125 2
            $key = $configItem;
126
        }
127
128 2
        return $this->fieldHandler->handleSimpleConfig($payload, $key, $configItem);
129
    }
130
131
    /**
132
     * Cases:
133
     * '*' => callable
134
     * 'string' => callable
135
     * 'string.*' => callable
136
     *
137
     * @param mixed $payload
138
     * @param $key
139
     * @param callable $callable
140
     *
141
     * @return array
142
     */
143 2
    protected function handleCallable($payload, $key, callable $callable): array
144
    {
145 2
        if ($key === '*') {
146 2
            foreach ($payload as $pkey => $pdata) {
147 2
                $payload = $this->handleCallable($payload, (string) $pkey, $callable);
148
            }
149 2
        } elseif (strpos($key, '.*') !== false) {
150 2
            $primary = substr($key, 0, strpos($key, '.*'));
151 2
            $payload[$primary] = $this->handleCallable($payload[$primary], '*', $callable);
152
        } else {
153 2
            $payload = $this->fieldHandler->handleCallableConfig($payload, $key, $callable);
154
        }
155
156 2
        return $payload;
157
    }
158
159
    /**
160
     * @param array $payload
161
     * @param array $config
162
     *
163
     * @return array
164
     * @throws \Xervice\ArrayHandler\Business\Exception\ArrayHandlerException
165
     */
166 1
    protected function handleArrayWildcard(array $payload, array $config): array
167
    {
168 1
        foreach ($payload as $pkey => $pdata) {
169 1
            $payload[$pkey] = $this->handleConfig($payload[$pkey], $config);
170
        }
171 1
        return $payload;
172
}
173
174
    /**
175
     * @param array $payload
176
     * @param $key
177
     * @param array $config
178
     *
179
     * @return array
180
     * @throws \Xervice\ArrayHandler\Business\Exception\ArrayHandlerException
181
     */
182 1
    protected function handleArraySubwildcard(array $payload, $key, array $config): array
183
    {
184 1
        $primary = substr($key, 0, strpos($key, '.*'));
185 1
        foreach ($payload[$primary] as $pkey => $pdata) {
186 1
            $payload[$primary][$pkey] = $this->handleConfig($pdata, $config);
187
        }
188 1
        return $payload;
189
}
190
191
    /**
192
     * @param array $payload
193
     * @param $key
194
     * @param array $config
195
     *
196
     * @return array
197
     * @throws \Xervice\ArrayHandler\Business\Exception\ArrayHandlerException
198
     */
199 1
    protected function handleArrayConfig(array $payload, $key, array $config): array
200
    {
201 1
        if ($key === FieldHandlerPluginInterface::HANDLE_THIS) {
202 1
            $payload = $this->fieldHandler->handleArrayConfig($payload, $config);
203 1
        } elseif (isset($payload[$key]) && is_string($payload[$key])) {
204 1
            $payload = $this->fieldHandler->handleNestedConfig($payload, $key, $config);
205
        } else {
206 1
            $payload[$key] = $this->handleConfig($payload[$key], $config);
207
        }
208 1
        return $payload;
209
}
210
}