Passed
Push — master ( d44a69...0a12ca )
by Mike
01:51
created

ArrayHandler::handleByType()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 3
crap 5.0342
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 $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
            foreach ($payload as $pkey => $pdata) {
100 1
                $payload[$pkey] = $this->handleConfig($payload[$pkey], $config);
101
            }
102 1
        } elseif (is_string($key) && strpos($key, '.*') !== false) {
103 1
            $primary = substr($key, 0, strpos($key, '.*'));
104 1
            foreach ($payload[$primary] as $pkey => $pdata) {
105 1
                $payload[$primary][$pkey] = $this->handleConfig($pdata, $config);
106
            }
107 1
        } elseif (is_int($key) && is_array($config)) {
108 1
            $payload = $this->handleConfig($payload, $config);
109
        } else {
110 1
            if ($key === FieldHandlerPluginInterface::HANDLE_THIS) {
111 1
                $payload = $this->fieldHandler->handleArrayConfig($payload, $config);
112 1
            } elseif (isset($payload[$key]) && is_string($payload[$key])) {
113 1
                $payload = $this->fieldHandler->handleNestedConfig($payload, $key, $config);
114
            } else {
115 1
                $payload[$key] = $this->handleConfig($payload[$key], $config);
116
            }
117
        }
118
119 1
        return $payload;
120
    }
121
122
    /**
123
     * Cases:
124
     * int => 'string',
125
     * 'string' => 'string',
126
     *
127
     * @param array $payload
128
     * @param mixed $key
129
     * @param string $configItem
130
     *
131
     * @return array
132
     */
133 2
    protected function handleString(array $payload, $key, string $configItem): array
134
    {
135 2
        if (is_int($key)) {
136 2
            $key = $configItem;
137
        }
138
139 2
        return $this->fieldHandler->handleSimpleConfig($payload, $key, $configItem);
140
    }
141
142
    /**
143
     * Cases:
144
     * '*' => callable
145
     * 'string' => callable
146
     * 'string.*' => callable
147
     *
148
     * @param mixed $payload
149
     * @param $key
150
     * @param callable $callable
151
     *
152
     * @return array
153
     */
154 2
    protected function handleCallable($payload, $key, callable $callable): array
155
    {
156 2
        if ($key === '*') {
157 2
            foreach ($payload as $pkey => $pdata) {
158 2
                $payload = $this->handleCallable($payload, (string) $pkey, $callable);
159
            }
160 2
        } elseif (strpos($key, '.*') !== false) {
161 2
            $primary = substr($key, 0, strpos($key, '.*'));
162 2
            $payload[$primary] = $this->handleCallable($payload[$primary], '*', $callable);
163
        } else {
164 2
            $payload = $this->fieldHandler->handleCallableConfig($payload, $key, $callable);
165
        }
166
167 2
        return $payload;
168
    }
169
}