1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 02.03.17 at 13:25 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\stringconditiontree; |
7
|
|
|
|
8
|
|
|
use samsonframework\stringconditiontree\string\StructureCollection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class StringConditionTree |
12
|
|
|
* |
13
|
|
|
* TODO: Remove variable character group markers, move LMP search to string. |
14
|
|
|
* |
15
|
|
|
* @author Vitaly Egorov <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class StringConditionTree |
18
|
|
|
{ |
19
|
|
|
/** Tree node root element identifier, needed for recursion */ |
20
|
|
|
const ROOT_NAME = ''; |
21
|
|
|
|
22
|
|
|
/** Final tree node branch identifier */ |
23
|
|
|
const SELF_NAME = '@self'; |
24
|
|
|
|
25
|
|
|
/** String parameter start marker */ |
26
|
|
|
const PARAMETER_START = '{'; |
27
|
|
|
|
28
|
|
|
/** String parameter end marker */ |
29
|
|
|
const PARAMETER_END = '}'; |
30
|
|
|
|
31
|
|
|
/** Parameter sorting length value for counting */ |
32
|
|
|
const PARAMETER_COF = 2000; |
33
|
|
|
|
34
|
|
|
/** @var TreeNode Resulting internalCollection for debugging */ |
35
|
|
|
protected $debug; |
36
|
|
|
|
37
|
|
|
/** @var array Collection of string string => identifier */ |
38
|
|
|
protected $source; |
39
|
|
|
|
40
|
|
|
/** @var string Parametrized string start marker */ |
41
|
|
|
protected $parameterStartMarker = self::PARAMETER_START; |
42
|
|
|
|
43
|
|
|
/** @var string Parametrized string end marker */ |
44
|
|
|
protected $parameterEndMarker = self::PARAMETER_END; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* StringConditionTree constructor. |
48
|
|
|
* |
49
|
|
|
* @param string $parameterStartMarker Parametrized string start marker |
50
|
|
|
* @param string $parameterEndMarker Parametrized string end marker |
51
|
|
|
*/ |
52
|
1 |
|
public function __construct( |
53
|
|
|
string $parameterStartMarker = self::PARAMETER_START, |
54
|
|
|
string $parameterEndMarker = self::PARAMETER_END |
55
|
|
|
) { |
56
|
1 |
|
$this->parameterStartMarker = $parameterStartMarker; |
57
|
1 |
|
$this->parameterEndMarker = $parameterEndMarker; |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Build similarity strings tree. |
62
|
|
|
* |
63
|
|
|
* @param array $input Collection of strings |
64
|
|
|
* |
65
|
|
|
* @return TreeNode Resulting similarity strings tree |
66
|
|
|
*/ |
67
|
1 |
|
public function process(array $input): TreeNode |
68
|
|
|
{ |
69
|
1 |
|
$this->source = $input; |
70
|
|
|
|
71
|
1 |
|
$this->processor( |
72
|
1 |
|
StructureCollection::fromStringsArray(array_keys($input))->getCommonPrefixesCollection(), |
73
|
1 |
|
$this->debug = new TreeNode() |
74
|
|
|
); |
75
|
|
|
|
76
|
1 |
|
return $this->debug; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param StructureCollection[] $collection |
81
|
|
|
*/ |
82
|
1 |
|
protected function processor(array $collection, TreeNode $parent, string $parentPrefix = ''): void |
83
|
|
|
{ |
84
|
1 |
|
foreach ($collection as $prefix => $item) { |
85
|
|
|
// Create tree node. Pass string identifier if present |
86
|
1 |
|
$newChild = $parent->append($prefix, $this->source[$parentPrefix.$prefix] ?? ''); |
87
|
|
|
|
88
|
1 |
|
$lcpCollection = $item->getCommonPrefixesCollection(); |
89
|
|
|
|
90
|
1 |
|
$this->processor($lcpCollection, $newChild, $parentPrefix.$prefix); |
91
|
|
|
} |
92
|
1 |
|
} |
93
|
|
|
} |
94
|
|
|
|