1 | <?php declare(strict_types=1); |
||
13 | class StructureSorter |
||
14 | { |
||
15 | /** Variable length characters group */ |
||
16 | const G_VARIABLE = 0; |
||
17 | |||
18 | /** Fixed length characters group */ |
||
19 | const G_FIXED = 1; |
||
20 | |||
21 | /** @var string Parametrized string start marker */ |
||
22 | protected $parameterStartMarker; |
||
23 | |||
24 | /** @var string Parametrized string end marker */ |
||
25 | protected $parameterEndMarker; |
||
26 | |||
27 | /** |
||
28 | * StructureSorter constructor. |
||
29 | * |
||
30 | * @param string $parameterStartMarker Parametrized string start marker |
||
31 | * @param string $parameterEndMarker Parametrized string end marker |
||
32 | */ |
||
33 | public function __construct(string $parameterStartMarker, string $parameterEndMarker) |
||
38 | |||
39 | /** |
||
40 | * Sort strings array considering PCG and NPCG string structure. |
||
41 | * |
||
42 | * @param array $input Input array for sorting |
||
43 | * |
||
44 | * @return array Sorted keys array |
||
45 | */ |
||
46 | public function sortArrayByKeys(array $input): array |
||
61 | |||
62 | /** |
||
63 | * Build string character group structure considering parametrized |
||
64 | * and not parametrized character groups and their length(PCG, NPCG). |
||
65 | * |
||
66 | * @param string $prefix Prefix string |
||
67 | * |
||
68 | * @return array String character groups structure |
||
69 | */ |
||
70 | protected function getPrefixStructure(string $prefix): array |
||
112 | |||
113 | /** |
||
114 | * Compare string structures. |
||
115 | * |
||
116 | * @param array $initial Initial string structure |
||
117 | * @param array $compared Compared string structure |
||
118 | * |
||
119 | * @return int Result of array elements comparison |
||
120 | */ |
||
121 | protected function compareStringStructure(array $initial, array $compared): int |
||
162 | |||
163 | /** |
||
164 | * Make CGS equals size. |
||
165 | * |
||
166 | * @param array $initial Initial CGS, will be changed |
||
167 | * @param array $compared Compared CGS, will be changed |
||
168 | * |
||
169 | * @return int Longest CGS size(now they are both equal) |
||
170 | */ |
||
171 | protected function equalizeStructures(array &$initial, array &$compared): int |
||
183 | |||
184 | /** |
||
185 | * Fill CSG with previous group value if not present. |
||
186 | * |
||
187 | * @param array $groups CSG for filling |
||
188 | * @param int $index CSG index |
||
189 | */ |
||
190 | private function fillMissingStructureGroup(array &$groups, int $index) |
||
196 | |||
197 | /** |
||
198 | * Compare two character group structure(CGS) length and define |
||
199 | * which one is longer. |
||
200 | * |
||
201 | * @param array $initial Initial CGS |
||
202 | * @param array $compared Compared CGS |
||
203 | * @param int $type CGS type (Variable|Fixed length) |
||
204 | * |
||
205 | * @return int -1 if initial CGS longer |
||
206 | * 0 if initial and compared CGS are equal |
||
207 | * 1 if compared CGS longer |
||
208 | */ |
||
209 | protected function compareStructureLengths(array $initial, array $compared, int $type = self::G_FIXED): int |
||
232 | |||
233 | /** |
||
234 | * Compare longer CGS considering that: |
||
235 | * - Shortest fixed CGS should have higher priority |
||
236 | * - Longest variable CGS should have higher priority |
||
237 | * |
||
238 | * @param array $initialGroup Initial CGS |
||
239 | * @param array $comparedGroup Compared CGS |
||
240 | * @param int $type Fixed/Variable CGS |
||
241 | * |
||
242 | * @return int|null Null if initial CGS is not longer than compared, |
||
243 | * otherwise -1/1 depending on CGS type. |
||
244 | */ |
||
245 | private function compareLength(array $initialGroup, array $comparedGroup, int $type) |
||
255 | } |
||
256 |