1 | <?php declare(strict_types = 1); |
||
18 | class StringConditionTree |
||
19 | { |
||
20 | /** Tree node root element identifier, needed for recursion */ |
||
21 | const ROOT_NAME = ''; |
||
22 | |||
23 | /** Final tree node branch identifier */ |
||
24 | const SELF_NAME = '@self'; |
||
25 | |||
26 | /** String parameter start marker */ |
||
27 | const PARAMETER_START = '{'; |
||
28 | |||
29 | /** String parameter end marker */ |
||
30 | const PARAMETER_END = '}'; |
||
31 | |||
32 | /** Parameter sorting length value for counting */ |
||
33 | const PARAMETER_COF = 2000; |
||
34 | |||
35 | /** @var TreeNode Resulting collection for debugging */ |
||
36 | protected $debug; |
||
37 | |||
38 | /** @var array Collection of string string => identifier */ |
||
39 | protected $source; |
||
40 | |||
41 | /** @var string Parametrized string start marker */ |
||
42 | protected $parameterStartMarker = self::PARAMETER_START; |
||
43 | |||
44 | /** @var string Parametrized string end marker */ |
||
45 | protected $parameterEndMarker = self::PARAMETER_END; |
||
46 | |||
47 | /** |
||
48 | * StringConditionTree constructor. |
||
49 | * |
||
50 | * @param string $parameterStartMarker Parametrized string start marker |
||
51 | * @param string $parameterEndMarker Parametrized string end marker |
||
52 | */ |
||
53 | 1 | public function __construct( |
|
60 | |||
61 | /** |
||
62 | * Build similarity strings tree. |
||
63 | * |
||
64 | * @param array $input Collection of strings |
||
65 | * |
||
66 | * @return TreeNode Resulting similarity strings tree |
||
67 | */ |
||
68 | 1 | public function process(array $input): TreeNode |
|
80 | |||
81 | /** |
||
82 | * Recursive string similarity tree builder. |
||
83 | * |
||
84 | * @param string $prefix |
||
85 | * @param array $input |
||
86 | * @param TreeNode $result |
||
87 | */ |
||
88 | 1 | protected function innerProcessor(string $prefix, array $input, TreeNode $result) |
|
121 | |||
122 | /** |
||
123 | * Get collection of grouped longest matching prefixes with strings sub-array. |
||
124 | * |
||
125 | * @param array $input Input strings array |
||
126 | * |
||
127 | * @return array Longest matching prefixes array |
||
128 | */ |
||
129 | 1 | protected function getLMPCollection(array $input): array |
|
177 | |||
178 | /** |
||
179 | * Sort structures array. |
||
180 | * |
||
181 | * @param array $strings Input strings array |
||
182 | * |
||
183 | * @return Structure[] Sorted structures array |
||
184 | */ |
||
185 | 1 | protected function sortStructures(array $strings): array |
|
201 | |||
202 | /** |
||
203 | * Sort strings array considering PCG and NPCG string structure. |
||
204 | * |
||
205 | * @param array $input Input array for sorting |
||
206 | * |
||
207 | * @return array Sorted keys array |
||
208 | */ |
||
209 | 1 | public function sortArrayByKeys(array $input): array |
|
231 | |||
232 | /** |
||
233 | * Iterate LMP and remove duplicate strings in other LMPs. |
||
234 | * |
||
235 | * @param array $prefixes LMP collection, returning value |
||
236 | */ |
||
237 | 1 | protected function filterLMPStrings(array &$prefixes) |
|
246 | |||
247 | /** |
||
248 | * Find all duplication of source array values in compared array and remove them. |
||
249 | * |
||
250 | * @param array $source Source array |
||
251 | * @param array $compared Compared array for filtering duplicates |
||
252 | */ |
||
253 | 1 | protected function removeDuplicatesInSubArray(array $source, array &$compared) |
|
263 | |||
264 | /** |
||
265 | * Remove key string from the beginning of all sub-array strings. |
||
266 | * |
||
267 | * @param array $array Input array of key => [keyStrings...] |
||
268 | * |
||
269 | * @return array Processed array with removed keys from beginning of sub arrays |
||
270 | */ |
||
271 | 1 | protected function removeKeyFromArrayStrings(array $array): array |
|
293 | } |
||
294 |