Completed
Pull Request — master (#3)
by Бабичев
02:07
created

Match   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Test Coverage

Coverage 61.53%

Importance

Changes 0
Metric Value
wmc 22
dl 0
loc 217
ccs 40
cts 65
cp 0.6153
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A regex() 0 3 1
A getQuery() 0 3 1
A methodAllowed() 0 6 2
A unserialize() 0 4 2
A serialize() 0 3 1
A getAttributes() 0 3 1
A getSubject() 0 3 1
A getProtocol() 0 3 1
A test() 0 12 2
A getPattern() 0 6 1
A getPath() 0 3 1
A getHost() 0 3 1
A isTest() 0 3 1
A getRule() 0 3 1
A __construct() 0 8 1
A jsonSerialize() 0 11 1
A setAttributes() 0 5 3
1
<?php
2
3
namespace Bavix\Router;
4
5
use Bavix\Router\Rules\PatternRule;
6
7
class Match implements \Serializable, \JsonSerializable
8
{
9
10
    /**
11
     * @var array
12
     */
13
    protected $urlData;
14
15
    /**
16
     * @var string
17
     */
18
    protected $protocol;
19
20
    /**
21
     * @var string
22
     */
23
    protected $host;
24
25
    /**
26
     * @var array
27
     */
28
    protected $attributes = [];
29
30
    /**
31
     * @var string
32
     */
33
    protected $method;
34
35
    /**
36
     * @var string
37
     */
38
    protected $subject;
39
40
    /**
41
     * @var PatternRule
42
     */
43
    protected $rule;
44
45
    /**
46
     * @var bool
47
     */
48
    protected $test;
49
50
    /**
51
     * @param PatternRule $rule
52
     * @param string $subject
53
     * @param string $method
54
     */
55 4
    public function __construct(PatternRule $rule, string $subject, string $method)
56
    {
57 4
        $this->attributes = $rule->getDefaults();
58 4
        $this->urlData = \parse_url($subject);
59 4
        $this->rule = $rule;
60 4
        $this->method = $method;
61 4
        $this->subject = $subject;
62 4
        $this->test();
63 4
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getProtocol(): ?string
69
    {
70
        return $this->urlData[\PHP_URL_SCHEME] ?? null;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function getHost(): ?string
77
    {
78
        return $this->urlData[\PHP_URL_HOST] ?? null;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getPath(): ?string
85
    {
86
        return $this->urlData[\PHP_URL_PATH] ?? null;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getQuery(): string
93
    {
94
        return $this->urlData[\PHP_URL_QUERY] ?? null;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getSubject(): string
101
    {
102
        return $this->subject;
103
    }
104
105
    /**
106
     * @return PatternRule
107
     */
108 1
    public function getRule(): PatternRule
109
    {
110 1
        return $this->rule;
111
    }
112
113
    /**
114
     * @return array
115
     */
116 3
    public function getAttributes(): array
117
    {
118 3
        return $this->attributes;
119
    }
120
121
    /**
122
     * @return string
123
     */
124 3
    public function getPattern(): string
125
    {
126 3
        return Build::url(
127 3
            $this->rule->getPath()->getPattern(),
128 3
            $this->rule->getHost(),
129 3
            $this->rule->getProtocol()
130
        );
131
    }
132
133
    /**
134
     * @return bool
135
     */
136 4
    public function isTest(): bool
137
    {
138 4
        return $this->test;
139
    }
140
141
    /**
142
     * @param array $attributes
143
     */
144 3
    protected function setAttributes(array $attributes): void
145
    {
146 3
        foreach ($attributes as $key => $value) {
147 3
            if ($value !== '') {
148 3
                $this->attributes[$key] = $value;
149
            }
150
        }
151 3
    }
152
153
    /**
154
     * check method
155
     *
156
     * @return bool
157
     */
158 4
    protected function methodAllowed(): bool
159
    {
160 4
        $this->test = $this->rule->getMethods() === null ||
161 2
            \in_array($this->method, $this->rule->getMethods(), true);
162
163 4
        return $this->isTest();
164
    }
165
166
    /**
167
     * check subject
168
     */
169 4
    protected function test(): void
170
    {
171 4
        if (!$this->methodAllowed()) {
172 1
            return;
173
        }
174
175 3
        $result = \preg_match($this->regex(), $this->subject, $matches);
176 3
        $this->test = $result !== 0;
177 3
        $this->setAttributes(\array_filter(
178 3
            $matches,
179 3
            '\is_string',
180 3
            \ARRAY_FILTER_USE_KEY
181
        ));
182 3
    }
183
184
    /**
185
     * @return string
186
     */
187 3
    protected function regex(): string
188
    {
189 3
        return '~^' . $this->getPattern() . '$~u';
190
    }
191
192
    /**
193
     * @inheritdoc
194
     */
195
    public function serialize(): string
196
    {
197
        return \serialize($this->jsonSerialize());
198
    }
199
200
    /**
201
     * @inheritdoc
202
     */
203
    public function unserialize($serialized): void
204
    {
205
        foreach (\unserialize($serialized, null) as $key => $value) {
206
            $this->{$key} = $value;
207
        }
208
    }
209
210
    /**
211
     * @inheritdoc
212
     */
213
    public function jsonSerialize(): array
214
    {
215
        return [
216
            'urlData' => $this->urlData,
217
            'protocol' => $this->protocol,
218
            'host' => $this->host,
219
            'attributes ' => $this->attributes ,
220
            'method' => $this->method,
221
            'subject' => $this->subject,
222
            'rule' => $this->rule,
223
            'test' => $this->test,
224
        ];
225
    }
226
227
}
228