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 | * @throws ReflectionException |
||||
30 | */ |
||||
31 | public function __construct() |
||||
32 | { |
||||
33 | FilterProvider::getInstance(); |
||||
34 | |||||
35 | // Parsing through all the providers |
||||
36 | foreach (FilterProvider::collect() as $provider) { |
||||
37 | $filter = new ExtractFilters($provider); |
||||
38 | |||||
39 | // Registering the properties |
||||
40 | $this->registry['filters'] += $filter->getFilters(); |
||||
41 | $this->registry['messages'] += $filter->getMessages(); |
||||
42 | } |
||||
43 | } |
||||
44 | |||||
45 | /** |
||||
46 | * Collecting the registry based upon the given target. |
||||
47 | * |
||||
48 | * @param string $type |
||||
49 | * @param string $index |
||||
50 | * |
||||
51 | * @return string|void |
||||
52 | */ |
||||
53 | public function getRegistry(string $type, string $index): string |
||||
54 | { |
||||
55 | $targetIndex = null; |
||||
56 | |||||
57 | // Whether the type exists inside the $data array |
||||
58 | if ($this->hasType($type)) { |
||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||
59 | |||||
60 | // Whether the $data array has the given index |
||||
61 | if ($this->hasIndex($type, $index)) { |
||||
62 | $targetIndex = $index; |
||||
63 | } |
||||
64 | |||||
65 | // In this case the $data array has no index of $index, now we'll try to reverse search by value |
||||
66 | if ($this->hasValue($type, $index)) { |
||||
67 | $targetIndex = $this->getIndexByValue($type, $index); |
||||
68 | } |
||||
69 | } |
||||
70 | |||||
71 | return $this->getValuesByIndex($type, $targetIndex); |
||||
0 ignored issues
–
show
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
![]() |
|||||
72 | } |
||||
73 | |||||
74 | /** |
||||
75 | * Whether the given type is a valid one. |
||||
76 | * |
||||
77 | * @param string $type |
||||
78 | * |
||||
79 | * @return bool |
||||
80 | */ |
||||
81 | public function isValidType(string $type): bool |
||||
82 | { |
||||
83 | return (bool) in_array($type, self::REGISTRY_TYPES) ? true : false; |
||||
84 | } |
||||
85 | |||||
86 | /** |
||||
87 | * Validating whether registry contains the given type. |
||||
88 | * |
||||
89 | * @param string $type |
||||
90 | * |
||||
91 | * @return bool |
||||
92 | */ |
||||
93 | public function hasType(string $type): bool |
||||
94 | { |
||||
95 | // Whether the given type is a valid one. |
||||
96 | if ($this->isValidType($type)) { |
||||
97 | return (bool) array_key_exists($type, $this->registry) ? true : false; |
||||
98 | } |
||||
99 | |||||
100 | return (bool) false; |
||||
101 | } |
||||
102 | |||||
103 | /** |
||||
104 | * Validating whether the data contains the given index. |
||||
105 | * |
||||
106 | * @param string $type |
||||
107 | * @param string $index |
||||
108 | * |
||||
109 | * @return bool |
||||
110 | */ |
||||
111 | public function hasIndex(string $type, string $index): bool |
||||
112 | { |
||||
113 | if ($this->hasType($type)) { |
||||
114 | return (bool) array_key_exists($index, $this->registry[$type]) ? true : false; |
||||
115 | } |
||||
116 | |||||
117 | return (bool) false; |
||||
118 | } |
||||
119 | |||||
120 | /** |
||||
121 | * Validating whether the data contains the given value. |
||||
122 | * |
||||
123 | * @param string $type |
||||
124 | * @param string $value |
||||
125 | * |
||||
126 | * @return bool |
||||
127 | */ |
||||
128 | public function hasValue(string $type, string $value): bool |
||||
129 | { |
||||
130 | if ($this->hasType($type)) { |
||||
131 | // Reversing the data array; ['key' => 'value'] will now be ['value' => 'key'] |
||||
132 | $reverseData = array_flip($this->registry[$type]); |
||||
133 | |||||
134 | return (bool) (isset($reverseData[$value])) ? true : false; |
||||
135 | } |
||||
136 | |||||
137 | return (bool) false; |
||||
138 | } |
||||
139 | |||||
140 | /** |
||||
141 | * Validating whether the given index has a value. |
||||
142 | * |
||||
143 | * @param string $type |
||||
144 | * @param string $index |
||||
145 | * |
||||
146 | * @return bool |
||||
147 | */ |
||||
148 | public function indexHasValue(string $type, string $index): bool |
||||
149 | { |
||||
150 | if ($this->hasType($type) && $this->hasIndex($type, $index)) { |
||||
151 | return (bool) !empty($this->registry[$type][$index]) ? true : false; |
||||
152 | } |
||||
153 | |||||
154 | return (bool) false; |
||||
155 | } |
||||
156 | |||||
157 | /** |
||||
158 | * Collecting the index by the given value. |
||||
159 | * |
||||
160 | * @param string $type |
||||
161 | * @param string $value |
||||
162 | * |
||||
163 | * @return string|void |
||||
164 | */ |
||||
165 | public function getIndexByValue(string $type, string $value) |
||||
166 | { |
||||
167 | if ($this->hasType($type) && $this->hasValue($type, $value)) { |
||||
168 | // Reversing the data array; ['key' => 'value'] will now be ['value' => 'key'] |
||||
169 | $reverseData = array_flip($this->registry[$type]); |
||||
170 | |||||
171 | return $reverseData[$type][$value]; |
||||
172 | } |
||||
173 | } |
||||
174 | |||||
175 | /** |
||||
176 | * Collecting multiple values based on the given index. |
||||
177 | * |
||||
178 | * @param string $type |
||||
179 | * @param string $index |
||||
180 | * |
||||
181 | * @return array|void |
||||
182 | */ |
||||
183 | public function getValuesByIndex(string $type, string $index) |
||||
184 | { |
||||
185 | if ($this->hasType($type) && $this->hasIndex($type, $index) && $this->indexHasValue($type, $index)) { |
||||
186 | return $this->registry[$type][$index]; |
||||
187 | } |
||||
188 | } |
||||
189 | } |
||||
190 |