Test Failed
Pull Request — master (#3)
by Бабичев
02:15
created

Match::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Router;
4
5
use Bavix\Router\Rules\PatternRule;
6
7
class Match
8
{
9
10
    /**
11
     * @var array
12
     */
13
    protected $attributes;
14
15
    /**
16
     * @var PatternRule
17
     */
18
    protected $rule;
19
20
    /**
21
     * @var bool
22
     */
23
    protected $test;
24
25
    /**
26
     * @param PatternRule $rule
27
     * @param string $subject
28
     */
29
    public function __construct(PatternRule $rule, string $subject)
30
    {
31
        $this->rule = $rule;
32
        $result = \preg_match($this->regex(), $subject, $matches);
33
        $this->test = $result !== 0;
34
        $this->attributes = \array_filter(
35
            $matches,
36
            '\is_string',
37
            \ARRAY_FILTER_USE_KEY
38
        );
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    protected function regex(): string
45
    {
46
        $url = Build::url(
47
            $this->rule->getPath()->getPattern(),
48
            $this->rule->getHost(),
49
            $this->rule->getProtocol()
50
        );
51
52
        return '~^' . $url . '$~u';
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function getAttributes(): array
59
    {
60
        return $this->attributes;
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    public function isTest(): bool
67
    {
68
        return $this->test;
69
    }
70
71
}
72