Test Failed
Push — master ( 3eb0e4...9196b6 )
by Ivan
02:13
created

RegexMatch::getModifiers()   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
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Everlution\Navigation\Voter\Regex;
6
7
use Everlution\Navigation\Property\BaseProperty;
8
use Everlution\Navigation\Voter\Match;
9
use Everlution\Navigation\Voter\Voter;
10
11
/**
12
 * Class RegexMatch.
13
 * @author Ivan Barlog <[email protected]>
14
 */
15
class RegexMatch extends BaseProperty implements Match
16
{
17
    /** @var string */
18
    private $regex;
19
    /** @var string */
20
    private $pattern;
21
    /** @var string */
22
    private $modifiers;
23
24
    public function __construct(string $pattern, string $modifiers = '')
25
    {
26
        $regex = sprintf('/%s/%s', str_replace('/', '\/', $pattern), $modifiers);
27
28
        if (false === preg_match($regex, '')) {
29
            throw new InvalidRegexPatternException($pattern);
30
        }
31
32
        $this->regex = $regex;
33
        $this->pattern = $pattern;
34
        $this->modifiers = $modifiers;
35
    }
36
37
    /**
38
     * @param Voter $voter
39
     * @return null|string
40
     */
41
    public function accept(Voter &$voter)
42
    {
43
        if (!$voter instanceof RegexVoter) {
44
            return null;
45
        }
46
47
        return $this->regex;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getPattern(): string
54
    {
55
        return $this->pattern;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getModifiers(): string
62
    {
63
        return $this->modifiers;
64
    }
65
}
66