Completed
Push — master ( e2d05f...edeffb )
by Vitaly
09:12
created

AbstractCharacterGroup::compare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
crap 3
1
<?php declare(strict_types=1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 06.04.17 at 07:28
5
 */
6
namespace samsonframework\stringconditiontree\string;
7
8
/**
9
 * This class describes string structure character group.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
abstract class AbstractCharacterGroup
14
{
15
    /** string Character group matching regexp pattern matching group name */
16
    const PATTERN_GROUP = '';
17
18
    /** string Regular expression matching character group */
19
    const PATTERN_REGEXP = '';
20
21
    /** string Character group matching regexp pattern */
22
    const PATTERN = '';
23
24
    /** @var int Character group length */
25
    public $length;
26
27
    /** @var string Character group string */
28
    public $string;
29
30
    /** @var int Character group size */
31
    protected $size = 1;
32
33
    /**
34
     * AbstractCharacterGroup constructor.
35
     *
36
     * @param string $string Character group string
37
     * @param int    $length Character group length
38
     */
39 18
    public function __construct(string $string, int $length = 0)
40
    {
41 18
        $this->string = $string;
42 18
        $this->length = $length;
43 18
    }
44
45
    /**
46
     * Create character group from input string.
47
     *
48
     * @param string $input Input string
49
     *
50
     * @return null|AbstractCharacterGroup|FixedCG|VariableCG|FixedVariableFixedCG Character group instance
51
     */
52 14
    public static function fromString(string &$input)
53
    {
54 14
        if (preg_match('/^'.static::PATTERN.'/', $input, $matches)) {
55
            // Replace only first occurrence of character group
56 14
            if (($pos = strpos($input, $matches[0])) !== false) {
57 14
                $input = substr_replace($input, '', $pos, strlen($matches[0]));
58
59 14
                $className = static::class;
60 14
                return new $className($matches[static::PATTERN_GROUP], strlen($matches[static::PATTERN_GROUP]));
61
            }
62
        }
63 11
    }
64
65
    /**
66
     * @return bool True if character group is variable length otherwise false
67
     */
68 1
    public function isVariable(): bool
69
    {
70 1
        return $this instanceof VariableCG;
71
    }
72
73
    /**
74
     * Compare character groups.
75
     *
76
     * @param AbstractCharacterGroup $group Compared character group
77
     *
78
     * @return int -1, 0, 1 Lower, equal, higher
79
     */
80 14
    public function compare(AbstractCharacterGroup $group): int
81
    {
82
        // Equal character group types - return length comparison
83 14
        if ($this->isSameType($group)) {
84
            // Variable character groups with longer length has higher priority
85 14
            return $this->compareLength($group);
86
        }
87
88
        /**
89
         * Character groups are different:
90
         * Fixed character groups has higher priority,
91
         * variable character groups has lower priority
92
         */
93 5
        return $this->isFixed() ? 1 : -1;
94
    }
95
96
    /**
97
     * Check if compared character group has same type.
98
     *
99
     * @param AbstractCharacterGroup $group Compared character group
100
     *
101
     * @return bool True if character group has same type otherwise false
102
     */
103 15
    public function isSameType(AbstractCharacterGroup $group): bool
104
    {
105 15
        return get_class($group) === get_class($this);
106
    }
107
108
    /**
109
     * Compare this character group length to compared character group length.
110
     *
111
     * @param AbstractCharacterGroup $group Compared character group
112
     *
113
     * @return int -1, 0, 1 Character groups comparison result
114
     */
115
    abstract protected function compareLength(AbstractCharacterGroup $group): int;
116
117
    /**
118
     * @return bool True if character group is fixed length otherwise false
119
     */
120 6
    public function isFixed(): bool
121
    {
122 6
        return $this instanceof FixedCG;
123
    }
124
}
125