1 | <?php declare(strict_types = 1); |
||
13 | class StringConditionTree |
||
14 | { |
||
15 | const ROOT_NAME = ''; |
||
16 | const SELF_NAME = '@self'; |
||
17 | |||
18 | /** @var TreeNode Resulting collection for debugging */ |
||
19 | protected $debug; |
||
20 | |||
21 | /** @var callable TreeNode value handler */ |
||
22 | protected $treeNodeValueHandler; |
||
23 | |||
24 | /** |
||
25 | * Set TreeNode value handler. |
||
26 | * |
||
27 | * @param callable $handler TreeNode value handler |
||
28 | */ |
||
29 | public function setTreeNodeValueHandler(callable $handler) |
||
33 | |||
34 | /** |
||
35 | * Build similarity strings tree. |
||
36 | * |
||
37 | * @param array $input Collection of strings |
||
38 | * |
||
39 | * @return TreeNode Resulting similarity strings tree |
||
40 | */ |
||
41 | public function process(array $input): TreeNode |
||
51 | |||
52 | /** |
||
53 | * Sort array by key string lengths. |
||
54 | * |
||
55 | * @param array $input Input array for sorting |
||
56 | * @param int $order Sorting order |
||
57 | */ |
||
58 | protected function sortArrayByKeys(array &$input, int $order = SORT_ASC) |
||
62 | |||
63 | /** |
||
64 | * Add only unique value to array. |
||
65 | * |
||
66 | * @param mixed $value Unique value |
||
67 | * @param array $array Array for adding unique value |
||
68 | * @param bool $strict Strict uniqueness check |
||
69 | * |
||
70 | * @see in_array(); |
||
71 | * |
||
72 | * @return bool True if unique value was added |
||
73 | */ |
||
74 | protected function addUniqueToArray($value, &$array, bool $strict = true) |
||
90 | |||
91 | /** |
||
92 | * Find longest matching prefix between two strings. |
||
93 | * |
||
94 | * @param string $initialString Initial string |
||
95 | * @param string $comparedString Compared string |
||
96 | * |
||
97 | * @return string Longest matching prefix |
||
98 | */ |
||
99 | protected function getLongestMatchingPrefix(string $initialString, string $comparedString): string |
||
125 | |||
126 | /** |
||
127 | * Remove key string from the beginning of all sub-array strings. |
||
128 | * |
||
129 | * @param array $array Input array of key => [keyStrings...] |
||
130 | * |
||
131 | * @param string $selfMarker Marker for storing self pointer |
||
132 | * |
||
133 | * @return array Processed array with removed keys from beginning of sub arrays |
||
134 | */ |
||
135 | protected function removeKeyFromArrayStrings(array $array, string $selfMarker): array |
||
154 | |||
155 | /** |
||
156 | * Find all duplication of source array values in compared array and remove them. |
||
157 | * |
||
158 | * @param array $source Source array |
||
159 | * @param array $compared Compared array for filtering duplicates |
||
160 | */ |
||
161 | protected function removeDuplicatesInSubArray(array $source, array &$compared) |
||
171 | |||
172 | /** |
||
173 | * Recursive string similarity tree builder. |
||
174 | * |
||
175 | * @param string $prefix |
||
176 | * @param array $input |
||
177 | * @param TreeNode $result |
||
178 | * @param string $selfMarker |
||
179 | */ |
||
180 | protected function innerProcessor(string $prefix, array $input, TreeNode $result, $selfMarker = self::SELF_NAME) |
||
261 | } |
||
262 |