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

Rule::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Bavix\Router;
4
5
abstract class Rule implements \Serializable, \JsonSerializable
6
{
7
8
    use Attachable;
9
10
    /**
11
     * @var string
12
     */
13
    protected $_protocol;
14
15
    /**
16
     * @var string
17
     */
18
    protected $_host;
19
20
    /**
21
     * @var null|string
22
     */
23
    protected $type;
24
25
    /**
26
     * @var null|Path
27
     */
28
    protected $path;
29
30
    /**
31
     * @var null|array
32
     */
33
    protected $methods;
34
35
    /**
36
     * @var array
37
     */
38
    protected $defaults;
39
40
    /**
41
     * @var array
42
     */
43
    protected $middleware;
44
45
    /**
46
     * Rule constructor.
47
     *
48
     * @param string $key
49
     * @param iterable $storage
50
     * @param null|self $parent
51
     */
52 5
    public function __construct(string $key, $storage, ?self $parent = null)
53
    {
54 5
        $this->prepare();
55 5
        $this->initializer($key, $storage);
0 ignored issues
show
Bug introduced by
$storage of type iterable is incompatible with the type array expected by parameter $storage of Bavix\Router\Rule::initializer(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        $this->initializer($key, /** @scrutinizer ignore-type */ $storage);
Loading history...
56 5
        $this->pathInit();
57 5
        if ($parent) {
58 2
            $this->afterPrepare($parent);
59
        }
60 5
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getName(): string
66
    {
67
        return $this->_name;
68
    }
69
70
    /**
71
     * @return string
72
     */
73 3
    public function getProtocol(): string
74
    {
75 3
        return $this->_protocol;
76
    }
77
78
    /**
79
     * @return string
80
     */
81 3
    public function getHost(): string
82
    {
83 3
        return $this->_host;
84
    }
85
86
    /**
87
     * @return Path
88
     */
89 3
    public function getPath(): Path
90
    {
91 3
        return $this->path;
92
    }
93
94
    /**
95
     * @return null|array
96
     */
97 4
    public function getMethods(): ?array
98
    {
99 4
        return $this->methods;
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    public function getMiddleware(): array
106
    {
107
        return (array)$this->middleware;
108
    }
109
110
    /**
111
     * @return array
112
     */
113 4
    public function getDefaults(): array
114
    {
115 4
        return (array)$this->defaults;
116
    }
117
118
    /**
119
     * @param Path|null $path
120
     */
121 2
    protected function hinge(?Path $path): void
122
    {
123 2
        if ($this->path && $path) {
124 2
            $this->path->hinge($path);
125 2
            return;
126
        }
127
128
        $this->path = $path ?? $this->path;
129
    }
130
131
    /**
132
     * @param string $name
133
     * @param self   $parent
134
     */
135 2
    protected function arrayMerge(string $name, self $parent): void
136
    {
137 2
        $this->$name = \array_merge(
138 2
            (array)$parent->$name,
139 2
            (array)$this->$name
140
        );
141 2
    }
142
143
    /**
144
     * @param self $parent
145
     * @return void
146
     */
147 2
    protected function afterPrepare(self $parent): void
148
    {
149 2
        $this->hinge($parent->path);
150 2
        $this->_protocol = $parent->protocol ?? $parent->_protocol ?? $this->_protocol;
151 2
        $this->_host = $parent->host ?? $parent->_host ?? $this->_host;
152 2
        $this->_name = $parent->_name . '.' . $this->_name;
153 2
        $this->methods = $this->methods ?? $parent->methods;
154 2
        $this->arrayMerge('defaults', $parent);
155 2
        $this->arrayMerge('middleware', $parent);
156 2
    }
157
158
    /**
159
     * if this.path === string then new Path(string, [])
160
     * else this.path === array then new Path(...this.path)
161
     */
162 5
    protected function pathInit(): void
163
    {
164 5
        if (\is_string($this->path)) {
0 ignored issues
show
introduced by
The condition is_string($this->path) is always false.
Loading history...
165 4
            $this->path = new Path($this->path);
166 1
        } elseif (\is_array($this->path)) {
0 ignored issues
show
introduced by
The condition is_array($this->path) is always false.
Loading history...
167
            $this->path = new Path(...$this->path);
168
        }
169 5
    }
170
171
    /**
172
     * @return void
173
     */
174 5
    protected function prepare(): void
175
    {
176 5
        $this->_protocol = '\w+';
177 5
        $this->_host = '[^\/]+';
178 5
    }
179
180
    /**
181
     * @return array
182
     * @throws \ReflectionException
183
     */
184
    protected function serializeArray(): array
185
    {
186
        $properties = [];
187
        $reflation = new \ReflectionClass($this);
188
        $protected = $reflation->getProperties(\ReflectionProperty::IS_PROTECTED);
189
        foreach ($protected as $property) {
190
            $properties[] = $property->name;
191
        }
192
        return $properties;
193
    }
194
195
    /**
196
     * @return array
197
     * @throws \ReflectionException
198
     */
199
    public function __sleep(): array
200
    {
201
        return \array_keys($this->serializeArray());
202
    }
203
204
    /**
205
     * @inheritdoc
206
     * @throws \ReflectionException
207
     */
208
    public function serialize(): string
209
    {
210
        $data = [];
211
        foreach ($this->serializeArray() as $key => $value) {
212
            $data[$value] = $this->$value;
213
        }
214
        return \serialize($data);
215
    }
216
217
    /**
218
     * @inheritdoc
219
     */
220
    public function unserialize($serialized): void
221
    {
222
        /**
223
         * @var string $serialized
224
         * @var array $data
225
         */
226
        $data = \unserialize($serialized, (array)null);
227
        foreach ($data as $key => $value) {
228
            $this->{$key} = $value;
229
        }
230
    }
231
232
    /**
233
     * @inheritdoc
234
     * @throws \ReflectionException
235
     */
236
    public function jsonSerialize(): array
237
    {
238
        $data = [];
239
        foreach ($this->serializeArray() as $key => $value) {
240
            $key = \ltrim($value, '_');
241
            $data[$key] = $this->$value;
242
        }
243
        return $data;
244
    }
245
246
}
247