1 | <?php declare(strict_types = 1); |
||
13 | class StringConditionTree |
||
14 | { |
||
15 | /** Tree node root element identifier, needed for recursion */ |
||
16 | const ROOT_NAME = ''; |
||
17 | |||
18 | /** Final tree node branch identifier */ |
||
19 | const SELF_NAME = '@self'; |
||
20 | |||
21 | /** String parameter start marker */ |
||
22 | const PARAMETER_START = '{'; |
||
23 | |||
24 | /** String parameter end marker */ |
||
25 | const PARAMETER_END = '}'; |
||
26 | |||
27 | /** Parameter sorting length value for counting */ |
||
28 | const PARAMETER_COF = 2000; |
||
29 | |||
30 | /** @var TreeNode Resulting collection for debugging */ |
||
31 | protected $debug; |
||
32 | |||
33 | /** @var array Collection of input string => identifier */ |
||
34 | protected $source; |
||
35 | |||
36 | /** @var string Parametrized string start marker */ |
||
37 | protected $parameterStartMarker = self::PARAMETER_START; |
||
38 | |||
39 | /** @var string Parametrized string end marker */ |
||
40 | protected $parameterEndMarker = self::PARAMETER_END; |
||
41 | |||
42 | /** @var StructureSorter */ |
||
43 | protected $structureSorter; |
||
44 | |||
45 | /** |
||
46 | * StringConditionTree constructor. |
||
47 | * |
||
48 | * @param StructureSorter|null $structureSorter Parametrized strings array structure sorter |
||
49 | * @param string $parameterStartMarker Parametrized string start marker |
||
50 | * @param string $parameterEndMarker Parametrized string end marker |
||
51 | */ |
||
52 | public function __construct( |
||
62 | |||
63 | /** |
||
64 | * Build similarity strings tree. |
||
65 | * |
||
66 | * @param array $input Collection of strings |
||
67 | * |
||
68 | * @return TreeNode Resulting similarity strings tree |
||
69 | */ |
||
70 | public function process(array $input): TreeNode |
||
82 | |||
83 | /** |
||
84 | * Recursive string similarity tree builder. |
||
85 | * |
||
86 | * @param string $prefix |
||
87 | * @param array $input |
||
88 | * @param TreeNode $result |
||
89 | */ |
||
90 | protected function innerProcessor(string $prefix, array $input, TreeNode $result) |
||
123 | |||
124 | /** |
||
125 | * Get collection of grouped longest matching prefixes with strings sub-array. |
||
126 | * |
||
127 | * @param array $input Input strings array |
||
128 | * |
||
129 | * @return array Longest matching prefixes array |
||
130 | */ |
||
131 | protected function getLMPCollection(array $input): array |
||
156 | |||
157 | /** |
||
158 | * Find longest matching prefix between two strings. |
||
159 | * |
||
160 | * @param string $initialString Initial string |
||
161 | * @param string $comparedString Compared string |
||
162 | * |
||
163 | * @return string Longest matching prefix |
||
164 | */ |
||
165 | protected function getLongestMatchingPrefix(string $initialString, string $comparedString): string |
||
216 | |||
217 | /** |
||
218 | * Add only unique value to array. |
||
219 | * |
||
220 | * @param mixed $value Unique value |
||
221 | * @param array $array Array for adding unique value |
||
222 | * @param bool $strict Strict uniqueness check |
||
223 | * |
||
224 | * @return bool True if unique value was added |
||
225 | * @see in_array(); |
||
226 | * |
||
227 | */ |
||
228 | protected function addUniqueToArray($value, &$array, bool $strict = true) |
||
245 | |||
246 | /** |
||
247 | * Iterate LMP and remove duplicate strings in other LMPs. |
||
248 | * |
||
249 | * @param array $prefixes LMP collection, returning value |
||
250 | */ |
||
251 | protected function filterLMPStrings(array &$prefixes) |
||
260 | |||
261 | /** |
||
262 | * Find all duplication of source array values in compared array and remove them. |
||
263 | * |
||
264 | * @param array $source Source array |
||
265 | * @param array $compared Compared array for filtering duplicates |
||
266 | */ |
||
267 | protected function removeDuplicatesInSubArray(array $source, array &$compared) |
||
277 | |||
278 | /** |
||
279 | * Remove key string from the beginning of all sub-array strings. |
||
280 | * |
||
281 | * @param array $array Input array of key => [keyStrings...] |
||
282 | * |
||
283 | * @return array Processed array with removed keys from beginning of sub arrays |
||
284 | */ |
||
285 | protected function removeKeyFromArrayStrings(array $array): array |
||
307 | } |
||
308 |