1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 06.04.17 at 07:34 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\stringconditiontree\string; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This class describes character group with variable length. |
10
|
|
|
* |
11
|
|
|
* @author Vitaly Egorov <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class VariableCG extends AbstractCG |
14
|
|
|
{ |
15
|
|
|
/** string Regular expression named filter group */ |
16
|
|
|
const PATTERN_FILTER_GROUP = 'filter'; |
17
|
|
|
|
18
|
|
|
/** string Variable string filter pattern */ |
19
|
|
|
const PATTER_FILTER = '/.*?:(?<'.self::PATTERN_FILTER_GROUP.'>[^}]+)/'; |
20
|
|
|
|
21
|
|
|
/** string Regular expression named character group group */ |
22
|
|
|
const PATTERN_GROUP = 'variable'; |
23
|
|
|
|
24
|
|
|
/** string Regular expression matching character group */ |
25
|
|
|
const PATTERN_REGEXP = '{.*?}'; |
26
|
|
|
|
27
|
|
|
/** string Character group matching regexp pattern */ |
28
|
|
|
const PATTERN = '(?<'.self::PATTERN_GROUP.'>'.self::PATTERN_REGEXP.')'; |
29
|
|
|
|
30
|
|
|
/** @var string Variable character group filter string */ |
31
|
|
|
protected $filter; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
22 |
|
public function __construct($string, $length = 0) |
37
|
|
|
{ |
38
|
22 |
|
parent::__construct($string, $length); |
39
|
|
|
|
40
|
22 |
|
$this->filter = $this->getFilter(); |
41
|
22 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get variable character group filter value. |
45
|
|
|
* |
46
|
|
|
* @return string Filter value or empty string |
47
|
|
|
*/ |
48
|
22 |
|
protected function getFilter(): string |
49
|
|
|
{ |
50
|
22 |
|
if (preg_match(static::PATTER_FILTER, $this->string, $matches)) { |
51
|
12 |
|
return $matches[self::PATTERN_FILTER_GROUP]; |
52
|
|
|
} |
53
|
|
|
|
54
|
18 |
|
return ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Whole variable length string should match. |
59
|
|
|
* |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
2 |
|
public function getCommonPrefix(AbstractCG $group): string |
63
|
|
|
{ |
64
|
2 |
|
if ($this->isSameType($group)) { |
65
|
2 |
|
return $this->string === $group->string ? $this->string : ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Pass to compared |
69
|
2 |
|
return $group->getCommonPrefix($this); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
9 |
|
protected function compareLength(AbstractCG $group): int |
76
|
|
|
{ |
77
|
|
|
/** @var VariableCG $group */ |
78
|
9 |
|
$variableFiltered = $this->isFiltered(); |
79
|
9 |
|
$comparedFiltered = $group->isFiltered(); |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Both variable character groups are filtered |
83
|
|
|
* longer variable character groups has higher priority. |
84
|
|
|
*/ |
85
|
9 |
|
if ($variableFiltered && $comparedFiltered) { |
86
|
3 |
|
return strlen($this->filter) <=> strlen($group->filter); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// Only this variable character group is filtered |
90
|
6 |
|
if ($variableFiltered && $comparedFiltered === false) { |
91
|
2 |
|
return 1; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Only compared variable character group is filtered |
95
|
6 |
|
if ($variableFiltered === false && $comparedFiltered) { |
96
|
1 |
|
return -1; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Consider both variable character groups are not filtered |
100
|
5 |
|
return 0; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return bool Return true if variable character group has filter |
105
|
|
|
*/ |
106
|
9 |
|
protected function isFiltered(): bool |
107
|
|
|
{ |
108
|
9 |
|
return $this->filter !== ''; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|