Completed
Push — master ( ec8e0b...6dedb2 )
by Vitaly
02:07
created

VariableCG::compareLength()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 27
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 10
cts 10
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 10
nc 4
nop 1
crap 7
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 21
    public function __construct($string, $length = 0)
37
    {
38 21
        parent::__construct($string, $length);
39
40 21
        $this->filter = $this->getFilter();
41 21
    }
42
43
    /**
44
     * Get variable character group filter value.
45
     *
46
     * @return string Filter value or empty string
47
     */
48 21
    protected function getFilter(): string
49
    {
50 21
        if (preg_match(static::PATTER_FILTER, $this->string, $matches)) {
51 11
            return $matches[self::PATTERN_FILTER_GROUP];
52
        }
53
54 17
        return '';
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 9
    protected function compareLength(AbstractCG $group): int
61
    {
62
        /** @var VariableCG $group */
63 9
        $variableFiltered = $this->isFiltered();
64 9
        $comparedFiltered = $group->isFiltered();
65
66
        /**
67
         * Both variable character groups are filtered
68
         * longer variable character groups has higher priority.
69
         */
70 9
        if ($variableFiltered && $comparedFiltered) {
71 3
            return strlen($this->filter) <=> strlen($group->filter);
72
        }
73
74
        // Only this variable character group is filtered
75 6
        if ($variableFiltered && $comparedFiltered === false) {
76 2
            return 1;
77
        }
78
79
        // Only compared variable character group is filtered
80 6
        if ($variableFiltered === false && $comparedFiltered) {
81 1
            return -1;
82
        }
83
84
        // Consider both variable character groups are not filtered
85 5
        return 0;
86
    }
87
88
    /**
89
     * @return bool Return true if variable character group has filter
90
     */
91 9
    protected function isFiltered(): bool
92
    {
93 9
        return $this->filter !== '';
94
    }
95
}
96