1 | <?php declare(strict_types=1); |
||
13 | class Structure extends IterableStructure |
||
14 | { |
||
15 | /** array Supported character group types */ |
||
16 | const CG_TYPES = [ |
||
17 | VariableFixedCG::class, |
||
18 | VariableCG::class, |
||
19 | FixedCG::class, |
||
20 | ]; |
||
21 | |||
22 | /** @var string Input string */ |
||
23 | public $string; |
||
24 | |||
25 | /** |
||
26 | * Create string character group structure from string string. |
||
27 | * |
||
28 | * @param string $input Input string for string character group structure |
||
29 | */ |
||
30 | 19 | public function __construct(string $input) |
|
46 | |||
47 | /** |
||
48 | * Compare structure character groups. |
||
49 | * |
||
50 | * @param Structure $structure Compared string structure group |
||
51 | * |
||
52 | * @return int Comparison result |
||
53 | */ |
||
54 | 17 | public function compare(Structure $structure): int |
|
75 | |||
76 | /** |
||
77 | * Get longest common prefix between strings. |
||
78 | * |
||
79 | * @param Structure $compared Compared character group structure |
||
80 | * |
||
81 | * @return string Strings Longest common prefix or empty string |
||
82 | * |
||
83 | */ |
||
84 | 2 | public function getCommonPrefix(Structure $compared) |
|
85 | { |
||
86 | 2 | $longestPrefix = ''; |
|
87 | |||
88 | /** @var AbstractCG $group Iterate longest structure character groups */ |
||
89 | 2 | foreach ($this->getShortestStructure($compared) as $index => $group) { |
|
90 | 2 | $initialGroup = $this->groups[$index]; |
|
91 | 2 | $comparedGroup = $compared->groups[$index]; |
|
92 | |||
93 | // Get longest matching prefix between character groups. |
||
94 | 2 | $prefix = $initialGroup->getCommonPrefix($comparedGroup); |
|
95 | |||
96 | // Concatenate common prefix |
||
97 | 2 | $longestPrefix .= $prefix; |
|
98 | |||
99 | /** |
||
100 | * If returned prefix is not equal to initial/compared character groups then it means |
||
101 | * that it is shorter and we need to stop searching. |
||
102 | */ |
||
103 | 2 | if ($this->isStringNotMatchingAnyCG($prefix, $initialGroup, $comparedGroup)) { |
|
104 | 2 | break; |
|
105 | } |
||
106 | } |
||
107 | |||
108 | 2 | return $longestPrefix; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Define shortest structure. |
||
113 | * |
||
114 | * @param Structure $compared Structure to compare |
||
115 | * |
||
116 | * @return Structure Shortest structure |
||
117 | */ |
||
118 | 2 | private function getShortestStructure(Structure $compared): Structure |
|
123 | |||
124 | /** |
||
125 | * Define if string does not match any of the passed character groups. |
||
126 | * |
||
127 | * @param string $string String for comparison |
||
128 | * @param AbstractCG $initialGroup Initial character group |
||
129 | * @param AbstractCG $comparedGroup Compared character group |
||
130 | * |
||
131 | * @return bool True if string not matching any of character groups |
||
132 | */ |
||
133 | 2 | private function isStringNotMatchingAnyCG(string $string, AbstractCG $initialGroup, AbstractCG $comparedGroup): bool |
|
137 | } |
||
138 |