1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediadevs\Validator\Helpers; |
4
|
|
|
|
5
|
|
|
class Registry extends Singleton |
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
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private static $registry = array( |
17
|
|
|
'filters' => array(), |
18
|
|
|
'messages' => array(), |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Registering the class in the $data array |
23
|
|
|
* |
24
|
|
|
* @param string $type |
25
|
|
|
* @param string $index |
26
|
|
|
* @param $data ($data has no strict type, it can be either an array or a string) |
|
|
|
|
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public static function register(string $type, string $index, $data): void |
31
|
|
|
{ |
32
|
|
|
if (self::isValidType($type)) { |
33
|
|
|
// Which type should be handled, the types are simple the required properties for a filter |
34
|
|
|
switch ($type) { |
35
|
|
|
// 'identifiers' |
36
|
|
|
case self::REGISTRY_TYPES[0]: |
37
|
|
|
self::handleIdentifier($index, $data); |
38
|
|
|
break; |
39
|
|
|
|
40
|
|
|
// 'aliases' |
41
|
|
|
case self::REGISTRY_TYPES[1]: |
42
|
|
|
self::handleAliases($index, $data); |
43
|
|
|
break; |
44
|
|
|
|
45
|
|
|
// 'messages' |
46
|
|
|
case self::REGISTRY_TYPES[2]: |
47
|
|
|
self::handleMessages($index, $data); |
48
|
|
|
break; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Registering the filters inside the $data array |
55
|
|
|
* |
56
|
|
|
* @param string $index |
57
|
|
|
* @param $data |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
private static function handleIdentifier(string $index, $data): void |
62
|
|
|
{ |
63
|
|
|
$registry = array($index => $data); |
64
|
|
|
|
65
|
|
|
self::$registry[self::REGISTRY_TYPES[0]] = array_merge(self::$registry[self::REGISTRY_TYPES[0]], $registry); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Registering the filters inside the $data array |
70
|
|
|
* |
71
|
|
|
* @param string $index |
72
|
|
|
* @param $data |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
private static function handleAliases(string $index, $data): void |
77
|
|
|
{ |
78
|
|
|
$registry = array(); |
79
|
|
|
|
80
|
|
|
// Parsing through the data (in this case the aliases) and assigning the correct class to it. |
81
|
|
|
foreach ($data as $alias) { |
82
|
|
|
$registry[$alias] = self::getRegistry(self::REGISTRY_TYPES[1], $index); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
self::$registry[self::REGISTRY_TYPES[0]] = array_merge(self::$registry[self::REGISTRY_TYPES[0]], $registry); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Registering the filters inside the $data array |
90
|
|
|
* |
91
|
|
|
* @param string $index |
92
|
|
|
* @param $data |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
private static function handleMessages(string $index, $data): void |
97
|
|
|
{ |
98
|
|
|
$registry = array($index => $data); |
99
|
|
|
|
100
|
|
|
self::$registry[self::REGISTRY_TYPES[2]] = array_merge(self::$registry[self::REGISTRY_TYPES[2]], $registry); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Collecting the registry based upon the given target |
105
|
|
|
* |
106
|
|
|
* @param string $type |
107
|
|
|
* @param string $index |
108
|
|
|
* |
109
|
|
|
* @return string|void |
110
|
|
|
*/ |
111
|
|
|
public static function getRegistry(string $type, string $index): string |
112
|
|
|
{ |
113
|
|
|
$targetIndex = null; |
114
|
|
|
|
115
|
|
|
// Whether the type exists inside the $data array |
116
|
|
|
if (self::hasType($type)) { |
117
|
|
|
|
118
|
|
|
// Whether the $data array has the given index |
119
|
|
|
if (self::hasIndex($type, $index)) { |
120
|
|
|
$targetIndex = $index; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// In this case the $data array has no index of $index, now we'll try to reverse search by value |
124
|
|
|
if (self::hasValue($type, $index)) { |
125
|
|
|
$targetIndex = self::getIndexByValue($type, $index); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return self::getValuesByIndex($type, $targetIndex); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Whether the given type is a valid one. |
134
|
|
|
* |
135
|
|
|
* @param string $type |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public static function isValidType(string $type): bool |
140
|
|
|
{ |
141
|
|
|
return (bool) in_array($type, self::REGISTRY_TYPES) ? true : false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Validating whether registry contains the given type |
146
|
|
|
* |
147
|
|
|
* @param string $type |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
public static function hasType(string $type): bool |
152
|
|
|
{ |
153
|
|
|
// Whether the given type is a valid one. |
154
|
|
|
if (self::isValidType($type)) { |
155
|
|
|
return (bool) array_key_exists($type, self::$registry) ? true : false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return (bool) false; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Validating whether the data contains the given index |
163
|
|
|
* |
164
|
|
|
* @param string $type |
165
|
|
|
* @param string $index |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public static function hasIndex(string $type, string $index): bool |
170
|
|
|
{ |
171
|
|
|
if (self::hasType($type)) { |
172
|
|
|
return (bool) array_key_exists($index, self::$registry[$type]) ? true : false; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return (bool) false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Validating whether the data contains the given value |
180
|
|
|
* |
181
|
|
|
* @param string $type |
182
|
|
|
* @param string $value |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public static function hasValue(string $type, string $value): bool |
187
|
|
|
{ |
188
|
|
|
if (self::hasType($type)) { |
189
|
|
|
// Reversing the data array; ['key' => 'value'] will now be ['value' => 'key'] |
190
|
|
|
$reverseData = array_flip(self::$registry[$type]); |
191
|
|
|
|
192
|
|
|
return (bool) (isset($reverseData[$value])) ? true : false; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
return (bool) false; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Validating whether the given index has a value |
200
|
|
|
* |
201
|
|
|
* @param string $type |
202
|
|
|
* @param string $index |
203
|
|
|
* |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
public static function indexHasValue(string $type, string $index): bool |
207
|
|
|
{ |
208
|
|
|
if (self::hasType($type) && self::hasIndex($type, $index)) { |
209
|
|
|
return (bool) !empty(self::$registry[$type][$index]) ? true : false; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return (bool) false; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Collecting the index by the given value |
217
|
|
|
* |
218
|
|
|
* @param string $type |
219
|
|
|
* @param string $value |
220
|
|
|
* |
221
|
|
|
* @return string|void |
222
|
|
|
*/ |
223
|
|
|
public static function getIndexByValue(string $type, string $value) |
224
|
|
|
{ |
225
|
|
|
if (self::hasType($type) && self::hasValue($type, $value)) { |
226
|
|
|
// Reversing the data array; ['key' => 'value'] will now be ['value' => 'key'] |
227
|
|
|
$reverseData = array_flip(self::$registry[$type]); |
228
|
|
|
|
229
|
|
|
return $reverseData[$type][$value]; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Collecting multiple values based on the given index |
235
|
|
|
* |
236
|
|
|
* @param string $type |
237
|
|
|
* @param string $index |
238
|
|
|
* |
239
|
|
|
* @return array|void |
240
|
|
|
*/ |
241
|
|
|
public static function getValuesByIndex(string $type, string $index) |
242
|
|
|
{ |
243
|
|
|
if (self::hasType($type) && self::hasIndex($type, $index) && self::indexHasValue($type, $index)) { |
244
|
|
|
return self::$registry[$type][$index]; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|