Completed
Branch 1.0 (9c4148)
by Mike
03:56
created

Registry::getValuesByIndex()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 4
nc 2
nop 2
crap 20
1
<?php
2
3
namespace Mediadevs\Validator\Helpers;
4
5
class Registry
6
{
7
    /**
8
     * The registry types for this library, these are de default properties for each filter class.
9
     */
10
    public const REGISTRY_TYPES = array('filters', 'aliases', 'messages');
11
12
    /**
13
     * All the registered classes will be stored in here.
14
     *
15
     * @var array
16
     */
17
    private $registry = array(
18
        'filters'   => array(),
19
        'messages'  => array(),
20
    );
21
22
    /**
23
     * Loading all the classes from \Services\FilterProvider and parsing the filters to collect the configuration
24
     * Using te previously collected configuration to generate a registry which can be used for validation.
25
     *
26
     * @return bool
27
     */
28
    public function load(): bool
29
    {
30
        // If the loading went successfully; return true, else; return false.
31
        // the registry will look like this: 'filters -> 'identifier' => class;
32
    }
33
34
    /**
35
     * Registering the class in the $data array.
36
     *
37
     * @param string $type
38
     * @param string $index
39
     * @param mixed  $data  ($data has no strict type, it can be either an array or a string)
40
     *
41
     * @return void
42
     */
43
    public function register(string $type, string $index, $data): void
44
    {
45
        if (self::isValidType($type)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...Registry::isValidType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        if (self::/** @scrutinizer ignore-call */ isValidType($type)) {
Loading history...
46
            // Which type should be handled, the types are simple the required properties for a filter
47
            switch ($type) {
48
                // 'identifiers'
49
                case self::REGISTRY_TYPES[0]:
50
                    self::handleIdentifier($index, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...try::handleIdentifier() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
                    self::/** @scrutinizer ignore-call */ 
51
                          handleIdentifier($index, $data);
Loading history...
51
                    break;
52
53
                // 'aliases'
54
                case self::REGISTRY_TYPES[1]:
55
                    self::handleAliases($index, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...gistry::handleAliases() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
                    self::/** @scrutinizer ignore-call */ 
56
                          handleAliases($index, $data);
Loading history...
56
                    break;
57
58
                // 'messages'
59
                case self::REGISTRY_TYPES[2]:
60
                    self::handleMessages($index, $data);
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...istry::handleMessages() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
                    self::/** @scrutinizer ignore-call */ 
61
                          handleMessages($index, $data);
Loading history...
61
                    break;
62
            }
63
        }
64
    }
65
66
    /**
67
     * Registering the filters inside the $data array.
68
     *
69
     * @param string $index
70
     * @param mixed  $data
71
     *
72
     * @return void
73
     */
74
    private function handleIdentifier(string $index, $data): void
75
    {
76
        $registry = array($index => $data);
77
78
        $this->registry[self::REGISTRY_TYPES[0]] = array_merge($this->registry[self::REGISTRY_TYPES[0]], $registry);
79
    }
80
81
    /**
82
     * Registering the filters inside the $data array.
83
     *
84
     * @param string $index
85
     * @param mixed  $data
86
     *
87
     * @return void
88
     */
89
    private function handleAliases(string $index, $data): void
90
    {
91
        $registry = array();
92
93
        // Parsing through the data (in this case the aliases) and assigning the correct class to it.
94
        foreach ($data as $alias) {
95
            $registry[$alias] = self::getRegistry(self::REGISTRY_TYPES[1], $index);
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...Registry::getRegistry() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
            /** @scrutinizer ignore-call */ 
96
            $registry[$alias] = self::getRegistry(self::REGISTRY_TYPES[1], $index);
Loading history...
96
        }
97
98
        $this->registry[self::REGISTRY_TYPES[0]] = array_merge($this->registry[self::REGISTRY_TYPES[0]], $registry);
99
    }
100
101
    /**
102
     * Registering the filters inside the $data array.
103
     *
104
     * @param string $index
105
     * @param mixed  $data
106
     *
107
     * @return void
108
     */
109
    private function handleMessages(string $index, $data): void
110
    {
111
        $registry = array($index => $data);
112
113
        $this->registry[self::REGISTRY_TYPES[2]] = array_merge($this->registry[self::REGISTRY_TYPES[2]], $registry);
114
    }
115
116
    /**
117
     * Collecting the registry based upon the given target.
118
     *
119
     * @param string $type
120
     * @param string $index
121
     *
122
     * @return string|void
123
     */
124
    public function getRegistry(string $type, string $index): string
125
    {
126
        $targetIndex = null;
127
128
        // Whether the type exists inside the $data array
129
        if (self::hasType($type)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
        if (self::/** @scrutinizer ignore-call */ hasType($type)) {
Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
130
131
            // Whether the $data array has the given index
132
            if (self::hasIndex($type, $index)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasIndex() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

132
            if (self::/** @scrutinizer ignore-call */ hasIndex($type, $index)) {
Loading history...
133
                $targetIndex = $index;
134
            }
135
136
            // In this case the $data array has no index of $index, now we'll try to reverse search by value
137
            if (self::hasValue($type, $index)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasValue() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

137
            if (self::/** @scrutinizer ignore-call */ hasValue($type, $index)) {
Loading history...
138
                $targetIndex = self::getIndexByValue($type, $index);
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...stry::getIndexByValue() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
                /** @scrutinizer ignore-call */ 
139
                $targetIndex = self::getIndexByValue($type, $index);
Loading history...
139
            }
140
        }
141
142
        return self::getValuesByIndex($type, $targetIndex);
0 ignored issues
show
Bug introduced by
It seems like $targetIndex can also be of type null; however, parameter $index of Mediadevs\Validator\Help...try::getValuesByIndex() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
        return self::getValuesByIndex($type, /** @scrutinizer ignore-type */ $targetIndex);
Loading history...
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...try::getValuesByIndex() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
        return self::/** @scrutinizer ignore-call */ getValuesByIndex($type, $targetIndex);
Loading history...
143
    }
144
145
    /**
146
     * Whether the given type is a valid one.
147
     *
148
     * @param string $type
149
     *
150
     * @return bool
151
     */
152
    public function isValidType(string $type): bool
153
    {
154
        return (bool) in_array($type, self::REGISTRY_TYPES) ? true : false;
155
    }
156
157
    /**
158
     * Validating whether registry contains the given type.
159
     *
160
     * @param string $type
161
     *
162
     * @return bool
163
     */
164
    public function hasType(string $type): bool
165
    {
166
        // Whether the given type is a valid one.
167
        if (self::isValidType($type)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...Registry::isValidType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
        if (self::/** @scrutinizer ignore-call */ isValidType($type)) {
Loading history...
168
            return (bool) array_key_exists($type, $this->registry) ? true : false;
169
        }
170
171
        return (bool) false;
172
    }
173
174
    /**
175
     * Validating whether the data contains the given index.
176
     *
177
     * @param string $type
178
     * @param string $index
179
     *
180
     * @return bool
181
     */
182
    public function hasIndex(string $type, string $index): bool
183
    {
184
        if (self::hasType($type)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

184
        if (self::/** @scrutinizer ignore-call */ hasType($type)) {
Loading history...
185
            return (bool) array_key_exists($index, $this->registry[$type]) ? true : false;
186
        }
187
188
        return (bool) false;
189
    }
190
191
    /**
192
     * Validating whether the data contains the given value.
193
     *
194
     * @param string $type
195
     * @param string $value
196
     *
197
     * @return bool
198
     */
199
    public function hasValue(string $type, string $value): bool
200
    {
201
        if (self::hasType($type)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
        if (self::/** @scrutinizer ignore-call */ hasType($type)) {
Loading history...
202
            // Reversing the data array; ['key' => 'value'] will now be ['value' => 'key']
203
            $reverseData = array_flip($this->registry[$type]);
204
205
            return (bool) (isset($reverseData[$value])) ? true : false;
206
        }
207
208
        return (bool) false;
209
    }
210
211
    /**
212
     * Validating whether the given index has a value.
213
     *
214
     * @param string $type
215
     * @param string $index
216
     *
217
     * @return bool
218
     */
219
    public function indexHasValue(string $type, string $index): bool
220
    {
221
        if (self::hasType($type) && self::hasIndex($type, $index)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasIndex() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

221
        if (self::hasType($type) && self::/** @scrutinizer ignore-call */ hasIndex($type, $index)) {
Loading history...
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

221
        if (self::/** @scrutinizer ignore-call */ hasType($type) && self::hasIndex($type, $index)) {
Loading history...
222
            return (bool) !empty($this->registry[$type][$index]) ? true : false;
223
        }
224
225
        return (bool) false;
226
    }
227
228
    /**
229
     * Collecting the index by the given value.
230
     *
231
     * @param string $type
232
     * @param string $value
233
     *
234
     * @return string|void
235
     */
236
    public function getIndexByValue(string $type, string $value)
237
    {
238
        if (self::hasType($type) && self::hasValue($type, $value)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

238
        if (self::/** @scrutinizer ignore-call */ hasType($type) && self::hasValue($type, $value)) {
Loading history...
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasValue() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

238
        if (self::hasType($type) && self::/** @scrutinizer ignore-call */ hasValue($type, $value)) {
Loading history...
239
            // Reversing the data array; ['key' => 'value'] will now be ['value' => 'key']
240
            $reverseData = array_flip($this->registry[$type]);
241
242
            return $reverseData[$type][$value];
243
        }
244
    }
245
246
    /**
247
     * Collecting multiple values based on the given index.
248
     *
249
     * @param string $type
250
     * @param string $index
251
     *
252
     * @return array|void
253
     */
254
    public function getValuesByIndex(string $type, string $index)
255
    {
256
        if (self::hasType($type) && self::hasIndex($type, $index) && self::indexHasValue($type, $index)) {
0 ignored issues
show
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

256
        if (self::/** @scrutinizer ignore-call */ hasType($type) && self::hasIndex($type, $index) && self::indexHasValue($type, $index)) {
Loading history...
Bug Best Practice introduced by
The method Mediadevs\Validator\Helpers\Registry::hasIndex() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

256
        if (self::hasType($type) && self::/** @scrutinizer ignore-call */ hasIndex($type, $index) && self::indexHasValue($type, $index)) {
Loading history...
Bug Best Practice introduced by
The method Mediadevs\Validator\Help...gistry::indexHasValue() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

256
        if (self::hasType($type) && self::hasIndex($type, $index) && self::/** @scrutinizer ignore-call */ indexHasValue($type, $index)) {
Loading history...
257
            return $this->registry[$type][$index];
258
        }
259
    }
260
}
261