Passed
Push — master ( da1c7b...d44a69 )
by Mike
01:46
created

ArrayHandler::handleCallable()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.9666
cc 4
nc 3
nop 3
crap 4
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\Business\Model\ArrayLocator\ArrayLocatorInterface;
0 ignored issues
show
Bug introduced by
The type Xervice\ArrayHandler\Bus...r\ArrayLocatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Xervice\ArrayHandler\Dependency\FieldHandlerPluginInterface;
10
11
class ArrayHandler implements ArrayHandlerInterface
12
{
13
    /**
14
     * @var \Xervice\ArrayHandler\Dependency\FieldHandlerPluginInterface
15
     */
16
    private $fieldHandler;
17
18
    /**
19
     * ArrayHandler constructor.
20
     *
21
     * @param \Xervice\ArrayHandler\Dependency\FieldHandlerPluginInterface $fieldHandler
22
     */
23 2
    public function __construct(
24
        FieldHandlerPluginInterface $fieldHandler
25
    ) {
26 2
        $this->fieldHandler = $fieldHandler;
27 2
    }
28
29
    /**
30
     * Cases:
31
     * '*' => callable
32
     * '*' => [ [...] ]
33
     * '*' => [ ... => [], ...]
34
     * int => 'string',
35
     * 'string' => 'string',
36
     * 'string' => [ [...] ]
37
     * 'string' => [ ... => [], ... ]
38
     * 'string' => callable
39
     * 'string.*' => [ [...] ]
40
     * 'string.*' => [ ... => [], ... ]
41
     * 'string.*' => callable
42
     *
43
     * @param array $payload
44
     * @param array $config
45
     *
46
     * @return array
47
     * @throws \Xervice\ArrayHandler\Business\Exception\ArrayHandlerException
48
     */
49 2
    public function handleArray(array $payload, array $config): array
50
    {
51 2
        foreach ($config as $key => $configItem) {
52 2
            if (is_string($key) && is_callable($configItem)) {
53 1
                $payload = $this->handleCallable($payload, $key, $configItem);
54 1
            } elseif (is_string($configItem)) {
55 1
                $payload = $this->handleString($payload, $key, $configItem);
56
            } elseif (is_array($configItem)) {
57
                // TODO: ? => array
58
            } else {
59 2
                throw new ArrayHandlerException('Config data is invalid.');
60
            }
61
        }
62
63 2
        return $payload;
64
    }
65
66
    /**
67
     * Cases:
68
     * int => 'string',
69
     * 'string' => 'string',
70
     *
71
     * @param array $payload
72
     * @param mixed $key
73
     * @param string $configItem
74
     *
75
     * @return array
76
     */
77 1
    protected function handleString(array $payload, $key, string $configItem): array
78
    {
79 1
        if (is_int($key)) {
80 1
            $key = $configItem;
81
        }
82
83 1
        return $this->fieldHandler->handleSimpleConfig($payload, $key, $configItem);
84
    }
85
86
    /**
87
     * Cases:
88
     * '*' => callable
89
     * 'string' => callable
90
     * 'string.*' => callable
91
     *
92
     * @param mixed $payload
93
     * @param string $key
94
     * @param callable $callable
95
     *
96
     * @return array
97
     */
98 1
    protected function handleCallable($payload, string $key, callable $callable): array
99
    {
100 1
        if ($key === '*') {
101 1
            foreach ($payload as $pkey => $pdata) {
102 1
                $payload = $this->handleCallable($payload, (string) $pkey, $callable);
103
            }
104 1
        } elseif (strpos($key, '.*') !== false) {
105 1
            $primary = substr($key, 0, strpos($key, '.*'));
106 1
            $payload[$primary] = $this->handleCallable($payload[$primary], '*', $callable);
107
        } else {
108 1
            $payload = $this->fieldHandler->handleCallableConfig($payload, $key, $callable);
109
        }
110
111 1
        return $payload;
112
    }
113
}