1 | <?php declare(strict_types = 1); |
||
14 | class StringConditionTree |
||
15 | { |
||
16 | /** Tree node root element identifier, needed for recursion */ |
||
17 | const ROOT_NAME = ''; |
||
18 | |||
19 | /** Final tree node branch identifier */ |
||
20 | const SELF_NAME = '@self'; |
||
21 | |||
22 | /** String parameter start marker */ |
||
23 | const PARAMETER_START = '{'; |
||
24 | |||
25 | /** String parameter end marker */ |
||
26 | const PARAMETER_END = '}'; |
||
27 | |||
28 | /** Parameter sorting length value for counting */ |
||
29 | const PARAMETER_COF = 2000; |
||
30 | |||
31 | /** @var TreeNode Resulting collection for debugging */ |
||
32 | protected $debug; |
||
33 | |||
34 | /** @var array Collection of input string => identifier */ |
||
35 | protected $source; |
||
36 | |||
37 | /** @var string Parametrized string start marker */ |
||
38 | protected $parameterStartMarker = self::PARAMETER_START; |
||
39 | |||
40 | /** @var string Parametrized string end marker */ |
||
41 | protected $parameterEndMarker = self::PARAMETER_END; |
||
42 | |||
43 | /** |
||
44 | * StringConditionTree constructor. |
||
45 | * |
||
46 | * @param string $parameterStartMarker Parametrized string start marker |
||
47 | * @param string $parameterEndMarker Parametrized string end marker |
||
48 | */ |
||
49 | 1 | public function __construct( |
|
56 | |||
57 | /** |
||
58 | * Build similarity strings tree. |
||
59 | * |
||
60 | * @param array $input Collection of strings |
||
61 | * |
||
62 | * @return TreeNode Resulting similarity strings tree |
||
63 | */ |
||
64 | 1 | public function process(array $input): TreeNode |
|
76 | |||
77 | /** |
||
78 | * Recursive string similarity tree builder. |
||
79 | * |
||
80 | * @param string $prefix |
||
81 | * @param array $input |
||
82 | * @param TreeNode $result |
||
83 | */ |
||
84 | 1 | protected function innerProcessor(string $prefix, array $input, TreeNode $result) |
|
117 | |||
118 | /** |
||
119 | * Get collection of grouped longest matching prefixes with strings sub-array. |
||
120 | * |
||
121 | * @param array $input Input strings array |
||
122 | * |
||
123 | * @return array Longest matching prefixes array |
||
124 | */ |
||
125 | 1 | protected function getLMPCollection(array $input): array |
|
152 | |||
153 | /** |
||
154 | * Find longest matching prefix between two strings. |
||
155 | * |
||
156 | * @param string $initialString Initial string |
||
157 | * @param string $comparedString Compared string |
||
158 | * |
||
159 | * @return string Longest matching prefix |
||
160 | */ |
||
161 | 1 | protected function getLongestMatchingPrefix(string $initialString, string $comparedString): string |
|
212 | |||
213 | /** |
||
214 | * Add only unique value to array. |
||
215 | * |
||
216 | * @param mixed $value Unique value |
||
217 | * @param array $array Array for adding unique value |
||
218 | * @param bool $strict Strict uniqueness check |
||
219 | * |
||
220 | * @return bool True if unique value was added |
||
221 | * @see in_array(); |
||
222 | * |
||
223 | */ |
||
224 | 1 | protected function addUniqueToArray($value, &$array, bool $strict = true) |
|
241 | |||
242 | /** |
||
243 | * Sort strings array considering PCG and NPCG string structure. |
||
244 | * |
||
245 | * @param array $input Input array for sorting |
||
246 | * |
||
247 | * @return array Sorted keys array |
||
248 | */ |
||
249 | 1 | public function sortArrayByKeys(array $input): array |
|
271 | |||
272 | /** |
||
273 | * Iterate LMP and remove duplicate strings in other LMPs. |
||
274 | * |
||
275 | * @param array $prefixes LMP collection, returning value |
||
276 | */ |
||
277 | 1 | protected function filterLMPStrings(array &$prefixes) |
|
286 | |||
287 | /** |
||
288 | * Find all duplication of source array values in compared array and remove them. |
||
289 | * |
||
290 | * @param array $source Source array |
||
291 | * @param array $compared Compared array for filtering duplicates |
||
292 | */ |
||
293 | 1 | protected function removeDuplicatesInSubArray(array $source, array &$compared) |
|
303 | |||
304 | /** |
||
305 | * Remove key string from the beginning of all sub-array strings. |
||
306 | * |
||
307 | * @param array $array Input array of key => [keyStrings...] |
||
308 | * |
||
309 | * @return array Processed array with removed keys from beginning of sub arrays |
||
310 | */ |
||
311 | 1 | protected function removeKeyFromArrayStrings(array $array): array |
|
333 | } |
||
334 |