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 next structure: |
10
|
|
|
* - variable length character group |
11
|
|
|
* - fixed length character group |
12
|
|
|
* |
13
|
|
|
* @author Vitaly Egorov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class VariableFixedCG extends AbstractCharacterGroup |
16
|
|
|
{ |
17
|
|
|
/** string Character group matching regexp pattern matching group name */ |
18
|
|
|
const PATTERN_GROUP = 'variableFixed'; |
19
|
|
|
|
20
|
|
|
/** string Character group matching regexp pattern */ |
21
|
|
|
const PATTERN = '(?<'.self::PATTERN_GROUP.'>'.VariableCG::PATTERN_REGEXP.FixedCG::PATTERN_REGEXP.')'; |
22
|
|
|
|
23
|
|
|
/** @var VariableCG */ |
24
|
|
|
protected $variableCG; |
25
|
|
|
|
26
|
|
|
/** @var FixedCG */ |
27
|
|
|
protected $fixedCG; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
15 |
|
public function __construct(string $string, int $length = 0) |
33
|
|
|
{ |
34
|
15 |
|
$this->size = 2; |
35
|
|
|
|
36
|
15 |
|
parent::__construct($string, $length); |
37
|
|
|
|
38
|
|
|
// Parse internal character groups |
39
|
15 |
|
$this->variableCG = VariableCG::fromString($string); |
40
|
15 |
|
$this->fixedCG = FixedCG::fromString($string); |
41
|
15 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return bool True if character group is fixed length otherwise false |
45
|
|
|
*/ |
46
|
|
|
public function isFixed(): bool |
47
|
|
|
{ |
48
|
|
|
return $this instanceof self; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
12 |
View Code Duplication |
public function compare(AbstractCharacterGroup $group): int |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
// Equal character group types - return length comparison |
57
|
12 |
|
if ($this->isSameType($group)) { |
58
|
|
|
// Variable character groups with longer length has higher priority |
59
|
11 |
|
return $this->compareLength($group); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Fixed character group has higher priority |
63
|
5 |
|
if ($group->isFixed()) { |
64
|
2 |
|
return -1; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* VariableFixed character group has higher priority than regular |
69
|
|
|
* variable character group. |
70
|
|
|
*/ |
71
|
4 |
|
return 1; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritdoc |
76
|
|
|
*/ |
77
|
11 |
|
protected function compareLength(AbstractCharacterGroup $group): int |
78
|
|
|
{ |
79
|
|
|
/** @var VariableFixedCG $group */ |
80
|
|
|
|
81
|
|
|
// Fixed CG are equal |
82
|
11 |
|
if (($return = $this->compareFixed($group)) === 0) { |
|
|
|
|
83
|
|
|
// Compare variable character groups |
84
|
7 |
|
$return = $this->variableCG->compare($group->variableCG); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
11 |
|
return $return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Longer fixed character group has higher priority. |
92
|
|
|
* |
93
|
|
|
* @param VariableFixedCG $group Compared character group |
94
|
|
|
* |
95
|
|
|
* @return int Comparison result |
96
|
|
|
*/ |
97
|
11 |
|
private function compareFixed(VariableFixedCG $group): int |
98
|
|
|
{ |
99
|
|
|
// Opposite fixed CG comparison |
100
|
11 |
|
return $this->fixedCG->length <=> $group->fixedCG->length; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.