1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediadevs\Validator\Helpers; |
4
|
|
|
|
5
|
|
|
use ReflectionException; |
6
|
|
|
use Mediadevs\Validator\Services\FilterProvider; |
7
|
|
|
|
8
|
|
|
class Registry |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The registry types for this library, these are de default properties for each filter class. |
12
|
|
|
*/ |
13
|
|
|
public const REGISTRY_TYPES = array('filters', 'aliases', 'messages'); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* All the registered classes will be stored in here. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $registry = array( |
21
|
|
|
'filters' => array(), |
22
|
|
|
'messages' => array(), |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Loading all the classes from \Services\FilterProvider and parsing the filters to collect the configuration |
27
|
|
|
* Using te previously collected configuration to generate a registry which can be used for validation. |
28
|
|
|
* |
29
|
|
|
* @return bool |
30
|
|
|
* @throws ReflectionException |
31
|
|
|
*/ |
32
|
|
|
public function load(): bool |
33
|
|
|
{ |
34
|
|
|
// Parsing through all the providers |
35
|
|
|
foreach (FilterProvider::collect() as $provider) { |
36
|
|
|
$filter = new ExtractFilters($provider); |
37
|
|
|
|
38
|
|
|
// Registering the properties |
39
|
|
|
$this->registry['filters'] += $filter->getFilters(); |
40
|
|
|
$this->registry['messages'] += $filter->getMessages(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Collecting the registry based upon the given target. |
48
|
|
|
* |
49
|
|
|
* @param string $type |
50
|
|
|
* @param string $index |
51
|
|
|
* |
52
|
|
|
* @return string|void |
53
|
|
|
*/ |
54
|
|
|
public function getRegistry(string $type, string $index): string |
55
|
|
|
{ |
56
|
|
|
$targetIndex = null; |
57
|
|
|
|
58
|
|
|
// Whether the type exists inside the $data array |
59
|
|
|
if (self::hasType($type)) { |
|
|
|
|
60
|
|
|
|
61
|
|
|
// Whether the $data array has the given index |
62
|
|
|
if (self::hasIndex($type, $index)) { |
|
|
|
|
63
|
|
|
$targetIndex = $index; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// In this case the $data array has no index of $index, now we'll try to reverse search by value |
67
|
|
|
if (self::hasValue($type, $index)) { |
|
|
|
|
68
|
|
|
$targetIndex = self::getIndexByValue($type, $index); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return self::getValuesByIndex($type, $targetIndex); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Whether the given type is a valid one. |
77
|
|
|
* |
78
|
|
|
* @param string $type |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function isValidType(string $type): bool |
83
|
|
|
{ |
84
|
|
|
return (bool) in_array($type, self::REGISTRY_TYPES) ? true : false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Validating whether registry contains the given type. |
89
|
|
|
* |
90
|
|
|
* @param string $type |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function hasType(string $type): bool |
95
|
|
|
{ |
96
|
|
|
// Whether the given type is a valid one. |
97
|
|
|
if (self::isValidType($type)) { |
|
|
|
|
98
|
|
|
return (bool) array_key_exists($type, $this->registry) ? true : false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return (bool) false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Validating whether the data contains the given index. |
106
|
|
|
* |
107
|
|
|
* @param string $type |
108
|
|
|
* @param string $index |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function hasIndex(string $type, string $index): bool |
113
|
|
|
{ |
114
|
|
|
if (self::hasType($type)) { |
|
|
|
|
115
|
|
|
return (bool) array_key_exists($index, $this->registry[$type]) ? true : false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return (bool) false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Validating whether the data contains the given value. |
123
|
|
|
* |
124
|
|
|
* @param string $type |
125
|
|
|
* @param string $value |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function hasValue(string $type, string $value): bool |
130
|
|
|
{ |
131
|
|
|
if (self::hasType($type)) { |
|
|
|
|
132
|
|
|
// Reversing the data array; ['key' => 'value'] will now be ['value' => 'key'] |
133
|
|
|
$reverseData = array_flip($this->registry[$type]); |
134
|
|
|
|
135
|
|
|
return (bool) (isset($reverseData[$value])) ? true : false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return (bool) false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Validating whether the given index has a value. |
143
|
|
|
* |
144
|
|
|
* @param string $type |
145
|
|
|
* @param string $index |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function indexHasValue(string $type, string $index): bool |
150
|
|
|
{ |
151
|
|
|
if (self::hasType($type) && self::hasIndex($type, $index)) { |
|
|
|
|
152
|
|
|
return (bool) !empty($this->registry[$type][$index]) ? true : false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return (bool) false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Collecting the index by the given value. |
160
|
|
|
* |
161
|
|
|
* @param string $type |
162
|
|
|
* @param string $value |
163
|
|
|
* |
164
|
|
|
* @return string|void |
165
|
|
|
*/ |
166
|
|
|
public function getIndexByValue(string $type, string $value) |
167
|
|
|
{ |
168
|
|
|
if (self::hasType($type) && self::hasValue($type, $value)) { |
|
|
|
|
169
|
|
|
// Reversing the data array; ['key' => 'value'] will now be ['value' => 'key'] |
170
|
|
|
$reverseData = array_flip($this->registry[$type]); |
171
|
|
|
|
172
|
|
|
return $reverseData[$type][$value]; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Collecting multiple values based on the given index. |
178
|
|
|
* |
179
|
|
|
* @param string $type |
180
|
|
|
* @param string $index |
181
|
|
|
* |
182
|
|
|
* @return array|void |
183
|
|
|
*/ |
184
|
|
|
public function getValuesByIndex(string $type, string $index) |
185
|
|
|
{ |
186
|
|
|
if (self::hasType($type) && self::hasIndex($type, $index) && self::indexHasValue($type, $index)) { |
|
|
|
|
187
|
|
|
return $this->registry[$type][$index]; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|