Completed
Push — master ( 605ec5...e9cc16 )
by Vitaly
02:18
created

AbstractCG::isFixed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 AbstractCG
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
    protected $length;
26
27
    /** @var string Character group string */
28
    protected $string;
29
30
    /**
31
     * AbstractCharacterGroup constructor.
32
     *
33
     * @param string $string Character group string
34
     * @param int    $length Character group length
35
     */
36 55
    public function __construct(string $string, int $length = null)
37
    {
38 55
        $this->string = $string;
39 55
        $this->length = $length ?? strlen($string);
40 55
    }
41
42
    /**
43
     * Create character group from string string.
44
     *
45
     * @param string $input Input string
46
     *
47
     * @return null|AbstractCG|FixedCG|VariableCG Character group instance
48
     */
49 49
    public static function fromString(string &$input): ?AbstractCG
50
    {
51 49
        if (preg_match('/^'.static::PATTERN.'/', $input, $matches)) {
52
            // Replace only first occurrence of character group
53 45
            if (($pos = strpos($input, $matches[0])) !== false) {
54 45
                $input = substr_replace($input, '', $pos, strlen($matches[0]));
55
56 45
                $className = static::class;
57 45
                return new $className($matches[static::PATTERN_GROUP], strlen($matches[static::PATTERN_GROUP]));
58
            }
59
        }
60
61 25
        return null;
62
    }
63
64
    /**
65
     * Compare character groups.
66
     *
67
     * @param AbstractCG|FixedCG|VariableCG|VariableFixedCG $group Compared character group
68
     *
69
     * @return int -1, 0, 1 Lower, equal, higher
70
     */
71
    abstract public function compare(AbstractCG $group): int;
72
73
    /**
74
     * Check if compared character group has same type.
75
     *
76
     * @param AbstractCG $group Compared character group
77
     *
78
     * @return bool True if character group has same type otherwise false
79
     */
80 42
    public function isSameType(AbstractCG $group): bool
81
    {
82 42
        return get_class($group) === get_class($this);
83
    }
84
85
    /**
86
     * Get two character groups longest common prefix.
87
     *
88
     * @param AbstractCG|FixedCG|VariableCG|VariableFixedCG $group Compared character group
89
     *
90
     * @return string Longest common prefix or empty string
91
     */
92
    abstract public function getCommonPrefix(AbstractCG $group): string;
93
94
    /**
95
     * @return string Character group string
96
     */
97 4
    public function getString(): string
98
    {
99 4
        return $this->string;
100
    }
101
102
    /**
103
     * Same character group type comparison.
104
     *
105
     * @param AbstractCG|FixedCG|VariableCG|VariableFixedCG $group Compared character group
106
     *
107
     * @return int -1, 0, 1 Character groups comparison result
108
     */
109
    abstract protected function compareLength(AbstractCG $group): int;
110
}
111