1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediadevs\Validator\Helpers; |
4
|
|
|
|
5
|
|
|
class Arguments |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Delimiters, separators and prefixes for parsing the configuration |
9
|
|
|
*/ |
10
|
|
|
private const FILTER_DELIMITER = '|'; |
11
|
|
|
private const THRESHOLD_PREFIX = ':'; |
12
|
|
|
private const LIST_SEPARATOR = ','; |
13
|
|
|
private const REGEX_DELIMITER = '/'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Regular expression patterns for parsing the configuration and extracting the arguments |
17
|
|
|
*/ |
18
|
|
|
private const PATTERNS = array( |
19
|
|
|
'filter' => '/(.*)' . self::THRESHOLD_PREFIX . '/', |
20
|
|
|
'thresholds' => '/' . self::THRESHOLD_PREFIX . '(.*)/', |
21
|
|
|
'regular_expression' => '/(?<=\\' . self::REGEX_DELIMITER . ')(.*?)(?=\\' . self::REGEX_DELIMITER . ')/', |
22
|
|
|
); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The filters from the defined registry class |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $filters = array(); |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The filter aliases from the defined registry class |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $aliases = array(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Arguments constructor. |
38
|
|
|
*/ |
39
|
|
|
public function __construct() |
40
|
|
|
{ |
41
|
|
|
} |
42
|
|
|
/** |
43
|
|
|
* Extracting all the filters from the configuration |
44
|
|
|
* Then we proceed to parse those filters to extract the filters, thresholds, and regular expression patterns |
45
|
|
|
* @param array $configuration |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getArguments(array $configuration): array |
50
|
|
|
{ |
51
|
|
|
$arguments = array(); |
52
|
|
|
// Parsing through all the fields and its assigned filters |
53
|
|
|
foreach ($configuration as $field => $filters) { |
54
|
|
|
// Parsing through the applied filters |
55
|
|
|
foreach (explode(self::FILTER_DELIMITER, $filters) as $filter) { |
56
|
|
|
$filterName = $this->filterArguments($filter, self::PATTERNS['filter']); |
57
|
|
|
$filterThresholds = $this->filterArguments($filter, self::PATTERNS['thresholds']); |
58
|
|
|
|
59
|
|
|
// Whether the filter is a regular expression |
60
|
|
|
if ($this->filterIsRegularExpression($filterName)) { |
61
|
|
|
$thresholds = $this->getPatternsFromThresholds($filterThresholds); |
62
|
|
|
} else { |
63
|
|
|
$thresholds = $this->getThresholds($filterThresholds); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Extracting data from the configuration and storing it inside the arguments array |
67
|
|
|
$arguments[] = [ |
68
|
|
|
'field' => $field, |
69
|
|
|
'filter' => $filter, |
70
|
|
|
'thresholds' => $thresholds |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $arguments; |
76
|
|
|
} |
77
|
|
|
/** |
78
|
|
|
* Extracting the filter name from the arguments |
79
|
|
|
* @param string $arguments |
80
|
|
|
* @param string $pattern |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
private function filterArguments(string $arguments, string $pattern): string |
85
|
|
|
{ |
86
|
|
|
// Whether the string contains filter thresholds |
87
|
|
|
if (strpos($arguments, self::THRESHOLD_PREFIX) !== false) { |
88
|
|
|
// Extracting the filter name which is defined before the threshold prefix and returning it |
89
|
|
|
preg_match($pattern, $arguments, $matches); |
90
|
|
|
|
91
|
|
|
// Returning index #1 (Or the second item in the array) since the first item contains the delimiter |
92
|
|
|
return $matches[1]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// The string contains no threshold prefix so the blank string will be returned |
96
|
|
|
return $arguments; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Extracting the filter threshold(s) from the arguments |
101
|
|
|
* @param string $thresholds |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
private function getThresholds(string $thresholds): array |
106
|
|
|
{ |
107
|
|
|
$collection = array(); |
108
|
|
|
$items = explode(self::LIST_SEPARATOR, $thresholds); |
109
|
|
|
|
110
|
|
|
// Parsing through all thresholds (If there are any) and assigning the correct type to it. |
111
|
|
|
if (count($items) > 0) { |
112
|
|
|
foreach ($items as $threshold) { |
113
|
|
|
$collection[] = $this->setCorrectThresholdType($threshold); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $collection; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Determines whether the filter is "regular_expression". |
122
|
|
|
* @param string $filter |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
private function filterIsRegularExpression(string $filter): bool |
127
|
|
|
{ |
128
|
|
|
$identifier = 'regular_expression'; |
129
|
|
|
// Instantiating the ValidationRegistry |
130
|
|
|
$aliases = $this->getAliasesByFilter($identifier, $this->aliases); |
|
|
|
|
131
|
|
|
|
132
|
|
|
// Validating whether the filter name matches any of the aliases or "regular_expression" it self. |
133
|
|
|
return $filter === $identifier || in_array($filter, $aliases); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* If the filter is a regular expression it will handle the thresholds the right way and return clean results |
138
|
|
|
* @param string $thresholds |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
private function getPatternsFromThresholds(string $thresholds): array |
143
|
|
|
{ |
144
|
|
|
$collection = array(); |
145
|
|
|
|
146
|
|
|
// Checking if there are any patterns for this filters |
147
|
|
|
if (strpos($thresholds, ':')) { |
148
|
|
|
preg_match(self::PATTERNS['regular_expression'], $thresholds, $patterns); |
149
|
|
|
|
150
|
|
|
// Parsing through the patterns and storing them inside the thresholds array. |
151
|
|
|
foreach (explode(self::LIST_SEPARATOR, $patterns) as $pattern) { |
152
|
|
|
$collection['thresholds'][] = (string) $pattern; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $collection; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Determines which type the threshold is and assigning the right type to it. |
161
|
|
|
* @param string $threshold |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
private function setCorrectThresholdType(string $threshold): string |
166
|
|
|
{ |
167
|
|
|
if (filter_var($threshold, FILTER_VALIDATE_BOOLEAN)) { |
168
|
|
|
return (bool) $threshold; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if (filter_var($threshold, FILTER_VALIDATE_INT)) { |
172
|
|
|
return (int) $threshold; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (filter_var($threshold, FILTER_VALIDATE_BOOLEAN)) { |
176
|
|
|
return (float) $threshold; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $threshold; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|