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)) { |
|
|
|
|
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); |
|
|
|
|
51
|
|
|
break; |
52
|
|
|
|
53
|
|
|
// 'aliases' |
54
|
|
|
case self::REGISTRY_TYPES[1]: |
55
|
|
|
self::handleAliases($index, $data); |
|
|
|
|
56
|
|
|
break; |
57
|
|
|
|
58
|
|
|
// 'messages' |
59
|
|
|
case self::REGISTRY_TYPES[2]: |
60
|
|
|
self::handleMessages($index, $data); |
|
|
|
|
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); |
|
|
|
|
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)) { |
|
|
|
|
130
|
|
|
|
131
|
|
|
// Whether the $data array has the given index |
132
|
|
|
if (self::hasIndex($type, $index)) { |
|
|
|
|
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)) { |
|
|
|
|
138
|
|
|
$targetIndex = self::getIndexByValue($type, $index); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return self::getValuesByIndex($type, $targetIndex); |
|
|
|
|
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)) { |
|
|
|
|
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)) { |
|
|
|
|
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)) { |
|
|
|
|
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)) { |
|
|
|
|
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)) { |
|
|
|
|
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)) { |
|
|
|
|
257
|
|
|
return $this->registry[$type][$index]; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|