ValidationData::getRegularValues()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 10
nc 4
nop 3
1
<?php
2
3
namespace Weew\Validator;
4
5
use Traversable;
6
use Weew\Contracts\IArrayable;
7
8
class ValidationData implements IValidationData {
9
    /**
10
     * @var IPropertyReader
11
     */
12
    protected $propertyReader;
13
14
    /**
15
     * @var mixed
16
     */
17
    protected $data;
18
19
    /**
20
     * ValidationData constructor.
21
     *
22
     * @param $data
23
     * @param IPropertyReader $propertyReader
24
     */
25
    public function __construct($data, IPropertyReader $propertyReader = null) {
26
        if ( ! $propertyReader instanceof IPropertyReader) {
27
            $propertyReader = $this->createPropertyReader();
28
        }
29
30
        $this->data = $data;
31
        $this->propertyReader = $propertyReader;
32
    }
33
34
    /**
35
     * @param string $key
36
     *
37
     * @return array
38
     */
39
    public function get($key) {
40
        return $this->getValues(
41
            $this->data, $this->splitKey($key)
42
        );
43
    }
44
45
    /**
46
     * @param mixed $data
47
     * @param array $keys
48
     *
49
     * @return array
50
     */
51
    protected function getValues($data, array $keys) {
52
        $key = array_shift($keys);
53
54
        if (array_contains(['', null], $key)) {
55
            return [];
56
        }
57
58
        if ($key === ValidationToken::WILDCARD_VALUES) {
59
            $values = $this->getWildcardValues($data, $keys);
60
        } else if ($key === ValidationToken::WILDCARD_KEYS) {
61
            $values = $this->getWildcardKeys($data, $keys);
62
        } else {
63
            $values = $this->getRegularValues($data, $key, $keys);
64
        }
65
66
        return $values;
67
    }
68
69
    /**
70
     * @param mixed $data
71
     * @param array $keys
72
     *
73
     * @return array
74
     */
75
    protected function getWildcardKeys($data, array $keys) {
0 ignored issues
show
Unused Code introduced by
The parameter $keys is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
        $values = [];
77
78 View Code Duplication
        if ( ! is_array($data) && ! $data instanceof Traversable) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
            if ($data instanceof IArrayable) {
80
                $data = $data->toArray();
81
            }
82
83
            if ( ! is_array($data)) {
84
                $data = [];
85
            }
86
        }
87
88
        foreach ($data as $itemKey => $itemValue) {
89
            $values[$itemKey] = $itemKey;
90
        }
91
92
        return $values;
93
    }
94
95
    /**
96
     * @param mixed $data
97
     * @param array $keys
98
     *
99
     * @return array
100
     */
101
    protected function getWildcardValues($data, array $keys) {
102
        $values = [];
103
104 View Code Duplication
        if ( ! is_array($data) && ! $data instanceof Traversable) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
            if ($data instanceof IArrayable) {
106
                $data = $data->toArray();
107
            }
108
109
            if ( ! is_array($data)) {
110
                $data = [];
111
            }
112
        }
113
114
        if (count($keys) === 0) {
115
            foreach ($data as $itemKey => $itemValue) {
116
                $values[$itemKey] = $itemValue;
117
            }
118
        }
119
120
        if (count($keys) > 0) {
121
            foreach ($data as $itemKey => $itemValue) {
122
                // since there are still selector keys left and the next
123
                // item is not an array, it is safe to skip it
124
                if ( ! is_array($itemValue)) {
125
                    continue;
126
                }
127
128
                $nestedValues = $this->getValues($itemValue, $keys);
129
130
                foreach ($nestedValues as $nestedKey => $nestedValue) {
131
                    $values[$this->buildKey($itemKey, $nestedKey)] = $nestedValue;
132
                }
133
            }
134
        }
135
136
        return $values;
137
    }
138
139
    /**
140
     * @param mixed $data
141
     * @param string $key
142
     * @param array $keys
143
     *
144
     * @return array
145
     */
146
    protected function getRegularValues($data, $key, array $keys) {
147
        $values = [];
148
149
        if (count($keys) === 0) {
150
            $values[$key] = $this->propertyReader->getProperty($data, $key);
151
        }
152
153
        if (count($keys) > 0) {
154
            $value = $this->propertyReader->getProperty($data, $key);
155
            $nestedValues = $this->getValues($value, $keys);
156
157
            foreach ($nestedValues as $nestedKey => $nestedValue) {
158
                $values[$this->buildKey($key, $nestedKey)] = $nestedValue;
159
            }
160
        }
161
162
        return $values;
163
    }
164
165
    /**
166
     * @param string $key
167
     *
168
     * @return array
169
     */
170
    protected function splitKey($key) {
171
        return explode('.', $key);
172
    }
173
174
    /**
175
     * @param array ...$keys
176
     *
177
     * @return string
178
     */
179
    protected function buildKey($keys) {
0 ignored issues
show
Unused Code introduced by
The parameter $keys is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
180
        return implode('.', func_get_args());
181
    }
182
183
    /**
184
     * @return IPropertyReader
185
     */
186
    protected function createPropertyReader() {
187
        return new PropertyReader();
188
    }
189
}
190