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 filter aliases from the defined registry class. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private $aliases = array(); |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
30 | |||
31 | /** |
||
32 | * Extracting all the filters from the configuration |
||
33 | * Then we proceed to parse those filters to extract the filters, thresholds, and regular expression patterns. |
||
34 | * |
||
35 | * @param array $configuration |
||
36 | * |
||
37 | * @return array |
||
38 | */ |
||
39 | public function getArguments(array $configuration): array |
||
40 | { |
||
41 | $arguments = array(); |
||
42 | // Parsing through all the fields and its assigned filters |
||
43 | foreach ($configuration as $field => $filters) { |
||
44 | // Parsing through the applied filters |
||
45 | foreach (explode(self::FILTER_DELIMITER, $filters) as $filter) { |
||
46 | $filterName = $this->filterArguments($filter, self::PATTERNS['filter']); |
||
47 | $filterThresholds = $this->filterArguments($filter, self::PATTERNS['thresholds']); |
||
48 | $thresholds = $this->getThresholds($filterThresholds); |
||
49 | |||
50 | // Extracting data from the configuration and storing it inside the arguments array |
||
51 | $arguments[] = [ |
||
52 | 'field' => $field, |
||
53 | 'filter' => $filterName, |
||
54 | 'thresholds' => $thresholds, |
||
55 | ]; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | return $arguments; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Extracting the filter name from the arguments. |
||
64 | * |
||
65 | * @param string $arguments |
||
66 | * @param string $pattern |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | private function filterArguments(string $arguments, string $pattern): string |
||
71 | { |
||
72 | // Whether the string contains filter thresholds |
||
73 | if (strpos($arguments, self::THRESHOLD_PREFIX) !== false) { |
||
74 | // Extracting the filter name which is defined before the threshold prefix and returning it |
||
75 | preg_match($pattern, $arguments, $matches); |
||
76 | |||
77 | // Returning index #1 (Or the second item in the array) since the first item contains the delimiter |
||
78 | return $matches[1]; |
||
79 | } |
||
80 | |||
81 | // The string contains no threshold prefix so the blank string will be returned |
||
82 | return $arguments; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Extracting the filter threshold(s) from the arguments. |
||
87 | * |
||
88 | * @param string $thresholds |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | private function getThresholds(string $thresholds): array |
||
93 | { |
||
94 | $collection = array(); |
||
95 | $items = explode(self::LIST_SEPARATOR, $thresholds); |
||
96 | |||
97 | // Parsing through all thresholds (If there are any) and assigning the correct type to it. |
||
98 | if (count($items) > 0) { |
||
99 | foreach ($items as $threshold) { |
||
100 | $collection[] = $this->setCorrectThresholdType($threshold); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | return $collection; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Determines which type the threshold is and assigning the right type to it. |
||
109 | * |
||
110 | * @param string $threshold |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | private function setCorrectThresholdType(string $threshold): string |
||
115 | { |
||
116 | if (filter_var($threshold, FILTER_VALIDATE_BOOLEAN)) { |
||
117 | return (bool) $threshold; |
||
118 | } |
||
119 | |||
120 | if (filter_var($threshold, FILTER_VALIDATE_INT)) { |
||
121 | return (int) $threshold; |
||
122 | } |
||
123 | |||
124 | if (filter_var($threshold, FILTER_VALIDATE_BOOLEAN)) { |
||
125 | return (float) $threshold; |
||
126 | } |
||
127 | |||
128 | return $threshold; |
||
129 | } |
||
130 | } |
||
131 |