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
|
8 |
View Code Duplication |
public function __construct(string $string, int $length = 0) |
|
|
|
|
33
|
|
|
{ |
34
|
8 |
|
$this->size = 2; |
35
|
|
|
|
36
|
8 |
|
parent::__construct($string, $length); |
37
|
|
|
|
38
|
|
|
// Parse internal character groups |
39
|
8 |
|
$this->variableCG = VariableCG::fromString($string); |
40
|
8 |
|
$this->fixedCG = FixedCG::fromString($string); |
41
|
8 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return bool True if character group is fixed length otherwise false |
45
|
|
|
*/ |
46
|
4 |
|
public function isFixed(): bool |
47
|
|
|
{ |
48
|
4 |
|
return $this instanceof self; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
7 |
|
protected function compareLength(AbstractCharacterGroup $group): int |
55
|
|
|
{ |
56
|
|
|
/** @var VariableFixedCG $group */ |
57
|
|
|
|
58
|
|
|
// Shorter FCG has higher priority |
59
|
7 |
|
$return = $this->fixedCG->length <=> $group->fixedCG->length; |
|
|
|
|
60
|
|
|
|
61
|
|
|
// Fixed CG are equal |
62
|
|
|
// if ($return === 0) { |
63
|
|
|
// // Longer first VCG has higher priority |
64
|
|
|
// $return = $this->variableCG->length <=> $group->variableCG->length; |
65
|
|
|
// } |
66
|
|
|
// TODO: Check for filtering pattern and VCG with filter has priority |
67
|
|
|
// TODO: But how to compare filters if present? |
68
|
|
|
|
69
|
7 |
|
return $return; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.