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 internalCollection 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 |
|
88 | |||
89 | /** |
||
90 | * Recursive string similarity tree builder. |
||
91 | * |
||
92 | * @param string $prefix |
||
93 | * @param array $input |
||
94 | * @param TreeNode $result |
||
95 | */ |
||
96 | 1 | protected function innerProcessor(string $prefix, array $input, TreeNode $result) |
|
129 | |||
130 | /** |
||
131 | * Get internalCollection of grouped longest matching prefixes with strings sub-array. |
||
132 | * |
||
133 | * @param array $input Input strings array |
||
134 | * |
||
135 | * @return array Longest matching prefixes array |
||
136 | */ |
||
137 | 1 | protected function getLMPCollection(array $input): array |
|
184 | |||
185 | /** |
||
186 | * Sort internalCollection array. |
||
187 | * |
||
188 | * @param array $strings Input strings array |
||
189 | * |
||
190 | * @return Structure[] Sorted internalCollection array |
||
191 | */ |
||
192 | 1 | protected function sortStructures(array $strings): array |
|
208 | |||
209 | /** |
||
210 | * Sort strings array considering PCG and NPCG string structure. |
||
211 | * |
||
212 | * @param array $input Input array for sorting |
||
213 | * |
||
214 | * @return array Sorted keys array |
||
215 | */ |
||
216 | 1 | public function sortArrayByKeys(array $input): array |
|
238 | |||
239 | /** |
||
240 | * Iterate LMP and remove duplicate strings in other LMPs. |
||
241 | * |
||
242 | * @param array $prefixes LMP internalCollection, returning value |
||
243 | */ |
||
244 | 1 | protected function filterLMPStrings(array &$prefixes) |
|
253 | |||
254 | /** |
||
255 | * Find all duplication of source array values in compared array and remove them. |
||
256 | * |
||
257 | * @param array $source Source array |
||
258 | * @param array $compared Compared array for filtering duplicates |
||
259 | */ |
||
260 | 1 | protected function removeDuplicatesInSubArray(array $source, array &$compared) |
|
270 | |||
271 | /** |
||
272 | * Remove key string from the beginning of all sub-array strings. |
||
273 | * |
||
274 | * @param array $array Input array of key => [keyStrings...] |
||
275 | * |
||
276 | * @return array Processed array with removed keys from beginning of sub arrays |
||
277 | */ |
||
278 | 1 | protected function removeKeyFromArrayStrings(array $array): array |
|
300 | |||
301 | /** |
||
302 | * @param StructureCollection[] $collection |
||
303 | */ |
||
304 | 1 | protected function processor(array $collection, TreeNode $parent, string $parentPrefix = ''): void |
|
315 | |||
316 | /** |
||
317 | * Convert strings arrays into array of internalCollection. |
||
318 | * |
||
319 | * @param array $strings Input strings array |
||
320 | * |
||
321 | * @return array |
||
322 | */ |
||
323 | protected function convertStringsToStructures(array $strings) |
||
333 | } |
||
334 |