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

RegexMatch   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 51
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A accept() 0 8 2
A getPattern() 0 4 1
A getModifiers() 0 4 1
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