Completed
Push — master ( f97877...0df277 )
by Maxim
05:56
created

ValidationData::getRegularValues()   A

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
                $nestedValues = $this->getValues($itemValue, $keys);
123
                $isLastWildcard = ! array_contains($keys, ValidationToken::WILDCARD_VALUES);
124
125
                foreach ($nestedValues as $nestedKey => $nestedValue) {
126
                    // do no collect null values unless it is the last wildcard node
127
                    if ($nestedValue !== null || $isLastWildcard) {
128
                        $values[$this->buildKey($itemKey, $nestedKey)] = $nestedValue;
129
                    }
130
                }
131
            }
132
        }
133
134
        return $values;
135
    }
136
137
    /**
138
     * @param mixed $data
139
     * @param string $key
140
     * @param array $keys
141
     *
142
     * @return array
143
     */
144
    protected function getRegularValues($data, $key, array $keys) {
145
        $values = [];
146
147
        if (count($keys) === 0) {
148
            $values[$key] = $this->propertyReader->getProperty($data, $key);
149
        }
150
151
        if (count($keys) > 0) {
152
            $value = $this->propertyReader->getProperty($data, $key);
153
            $nestedValues = $this->getValues($value, $keys);
154
155
            foreach ($nestedValues as $nestedKey => $nestedValue) {
156
                $values[$this->buildKey($key, $nestedKey)] = $nestedValue;
157
            }
158
        }
159
160
        return $values;
161
    }
162
163
    /**
164
     * @param string $key
165
     *
166
     * @return array
167
     */
168
    protected function splitKey($key) {
169
        return explode('.', $key);
170
    }
171
172
    /**
173
     * @param array ...$keys
174
     *
175
     * @return string
176
     */
177
    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...
178
        return implode('.', func_get_args());
179
    }
180
181
    /**
182
     * @return IPropertyReader
183
     */
184
    protected function createPropertyReader() {
185
        return new PropertyReader();
186
    }
187
}
188