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

Match::unserialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
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 array
32
     */
33
    protected $groups = [];
34
35
    /**
36
     * @var string
37
     */
38
    protected $method;
39
40
    /**
41
     * @var string
42
     */
43
    protected $subject;
44
45
    /**
46
     * @var PatternRule
47
     */
48
    protected $rule;
49
50
    /**
51
     * @var bool
52
     */
53
    protected $test;
54
55
    /**
56
     * @param PatternRule $rule
57
     * @param string $subject
58
     * @param string $method
59
     */
60 4
    public function __construct(PatternRule $rule, string $subject, string $method)
61
    {
62 4
        $this->attributes = $rule->getDefaults();
63 4
        $this->urlData = \parse_url($subject);
64 4
        $this->rule = $rule;
65 4
        $this->method = $method;
66 4
        $this->subject = $subject;
67 4
        $this->test();
68 4
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getGroups(): array
74
    {
75
        return $this->groups;
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getProtocol(): ?string
82
    {
83
        return $this->urlData[\PHP_URL_SCHEME] ?? null;
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getHost(): ?string
90
    {
91
        return $this->urlData[\PHP_URL_HOST] ?? null;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getPath(): ?string
98
    {
99
        return $this->urlData[\PHP_URL_PATH] ?? null;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getQuery(): string
106
    {
107
        return $this->urlData[\PHP_URL_QUERY] ?? null;
108
    }
109
110
    /**
111
     * @return string
112
     */
113
    public function getSubject(): string
114
    {
115
        return $this->subject;
116
    }
117
118
    /**
119
     * @return PatternRule
120
     */
121 1
    public function getRule(): PatternRule
122
    {
123 1
        return $this->rule;
124
    }
125
126
    /**
127
     * @return array
128
     */
129 3
    public function getAttributes(): array
130
    {
131 3
        return $this->attributes;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 3
    public function getPattern(): string
138
    {
139 3
        return Server::url(
140 3
            $this->rule->getPath()->getPattern(),
141 3
            $this->rule->getHost(),
142 3
            $this->rule->getProtocol()
143
        );
144
    }
145
146
    /**
147
     * @return bool
148
     */
149 4
    public function isTest(): bool
150
    {
151 4
        return $this->test;
152
    }
153
154
    /**
155
     * @param array $groups
156
     */
157 3
    protected function setGroups(array $groups): void
158
    {
159 3
        foreach ($groups as $key => $value) {
160 3
            if ($value !== '') {
161 3
                $this->groups[$key] = $value;
162
            }
163
        }
164
165 3
        $this->attributes = \array_merge(
166 3
            $this->attributes,
167 3
            $this->groups
168
        );
169 3
    }
170
171
    /**
172
     * check method
173
     *
174
     * @return bool
175
     */
176 4
    protected function methodAllowed(): bool
177
    {
178 4
        $this->test = $this->rule->getMethods() === null ||
179 2
            \in_array($this->method, $this->rule->getMethods(), true);
180
181 4
        return $this->isTest();
182
    }
183
184
    /**
185
     * check subject
186
     */
187 4
    protected function test(): void
188
    {
189 4
        if (!$this->methodAllowed()) {
190 1
            return;
191
        }
192
193 3
        $result = \preg_match($this->regex(), $this->subject, $matches);
194 3
        $this->test = $result !== 0;
195 3
        $this->setGroups(\array_filter(
196 3
            $matches,
197 3
            '\is_string',
198 3
            \ARRAY_FILTER_USE_KEY
199
        ));
200 3
    }
201
202
    /**
203
     * @return string
204
     */
205 3
    protected function regex(): string
206
    {
207 3
        return '~^' . $this->getPattern() . '$~u';
208
    }
209
210
    /**
211
     * @return array
212
     */
213
    public function __sleep(): array
214
    {
215
        return \array_keys($this->jsonSerialize());
216
    }
217
218
    /**
219
     * @inheritdoc
220
     */
221
    public function serialize(): string
222
    {
223
        return \serialize($this->jsonSerialize());
224
    }
225
226
    /**
227
     * @inheritdoc
228
     */
229
    public function unserialize($serialized): void
230
    {
231
        $data = \unserialize($serialized, (array)null);
232
        foreach ($data as $key => $value) {
233
            $this->{$key} = $value;
234
        }
235
    }
236
237
    /**
238
     * @inheritdoc
239
     */
240
    public function jsonSerialize(): array
241
    {
242
        return [
243
            'urlData' => $this->urlData,
244
            'protocol' => $this->protocol,
245
            'host' => $this->host,
246
            'attributes ' => $this->attributes ,
247
            'method' => $this->method,
248
            'subject' => $this->subject,
249
            'rule' => $this->rule,
250
            'test' => $this->test,
251
        ];
252
    }
253
254
}
255