Passed
Push — master ( 20e747...679572 )
by Ivan
01:59
created

RegexNavigationItem::getRegexPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Everlution\Navigation;
6
7
use Everlution\Navigation\Extension\ManualMatch;
8
use Everlution\Navigation\Extension\ManualMatchExtension;
9
10
/**
11
 * Class RegexNavigationItem.
12
 * @author Ivan Barlog <[email protected]>
13
 */
14
class RegexNavigationItem extends NavigationItem implements RegexableItem, ManualMatch
15
{
16
    use ManualMatchExtension;
17
18
    /** @var string */
19
    private $regexPattern;
20
    /** @var string */
21
    private $regexModifiers;
22
23
    public function __construct(
24
        string $label,
25
        string $regexPattern,
26
        string $modifiers = '',
27
        Item $parent = null,
28
        array $children = []
29
    ) {
30
        $pattern = sprintf('/%s/%s', str_replace('/', '\/', $regexPattern), $modifiers);
31
32
        if (false === preg_match($pattern, '')) {
33
            throw new InvalidRegexPatternException($regexPattern);
34
        }
35
36
        parent::__construct($pattern, $label, $parent, $children);
37
38
        $this->regexPattern = $regexPattern;
39
        $this->regexModifiers = $modifiers;
40
    }
41
42
    public function getRegexPattern(): string
43
    {
44
        return $this->regexPattern;
45
    }
46
47
    public function getRegexModifiers(): string
48
    {
49
        return $this->regexModifiers;
50
    }
51
52
    public function getPattern(): string
53
    {
54
        return $this->getUri();
55
    }
56
}
57