Passed
Push — master ( 65b336...1323cd )
by Бабичев
44s
created

Group   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 23
dl 0
loc 257
ccs 0
cts 69
cp 0
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A pushCollection() 0 4 1
A __construct() 0 11 1
A toArray() 0 12 1
A setMethods() 0 4 1
A pushPattern() 0 4 1
A getResolver() 0 17 2
A getDefaults() 0 3 1
A setName() 0 4 1
A getProtocol() 0 3 1
A setHost() 0 4 1
A getMethods() 0 3 1
A patterns() 0 5 3
A getName() 0 3 2
A getHost() 0 3 1
A setDefaults() 0 4 1
A setProtocol() 0 4 1
A getExtends() 0 3 1
A setExtends() 0 4 1
A jsonSerialize() 0 3 1
1
<?php
2
3
namespace Bavix\Router;
4
5
class Group implements \JsonSerializable
6
{
7
8
    /**
9
     * @var ResourceCollection[]
10
     */
11
    protected $collections = [];
12
13
    /**
14
     * @var Pattern[]
15
     */
16
    protected $resolver = [];
17
18
    /**
19
     * @var string
20
     */
21
    protected $name;
22
23
    /**
24
     * @var string
25
     */
26
    protected $path;
27
28
    /**
29
     * @var string
30
     */
31
    protected $protocol;
32
33
    /**
34
     * @var string
35
     */
36
    protected $host;
37
38
    /**
39
     * @var array
40
     */
41
    protected $extends = [];
42
43
    /**
44
     * @var array
45
     */
46
    protected $defaults = [];
47
48
    /**
49
     * @var array
50
     */
51
    protected $methods;
52
53
    /**
54
     * Group constructor.
55
     *
56
     * @param null|string $prefix
57
     * @param callable $callback
58
     */
59
    public function __construct(string $prefix, callable $callback)
60
    {
61
        $resolver = new Resolver(function (Pattern $pattern) {
62
            return $this->pushPattern($pattern);
63
        }, function (ResourceCollection $collection) {
64
            return $this->pushCollection($collection);
65
        });
66
67
        $this->path = $prefix;
68
        $closure = \Closure::fromCallable($callback);
69
        $closure->call($resolver, $resolver);
70
    }
71
72
    /**
73
     * @param Pattern $pattern
74
     *
75
     * @return Pattern
76
     */
77
    protected function pushPattern($pattern): Pattern
78
    {
79
        $this->resolver[$pattern->getName()] = $pattern;
80
        return $pattern;
81
    }
82
83
    /**
84
     * @param ResourceCollection $collection
85
     *
86
     * @return ResourceCollection
87
     */
88
    protected function pushCollection(ResourceCollection $collection): ResourceCollection
89
    {
90
        $this->collections[] = $collection;
91
        return $collection;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getName(): string
98
    {
99
        return $this->name ?: Helper::generateName($this->path);
100
    }
101
102
    /**
103
     * @param string $name
104
     *
105
     * @return $this
106
     */
107
    public function setName(string $name): self
108
    {
109
        $this->name = $name;
110
        return $this;
111
    }
112
113
    /**
114
     * @return null|string
115
     */
116
    public function getProtocol(): ?string
117
    {
118
        return $this->protocol;
119
    }
120
121
    /**
122
     * @param string $protocol
123
     * @return $this
124
     */
125
    public function setProtocol(string $protocol): self
126
    {
127
        $this->protocol = $protocol;
128
        return $this;
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    public function getHost(): ?string
135
    {
136
        return $this->host;
137
    }
138
139
    /**
140
     * @param string $host
141
     * @return $this
142
     */
143
    public function setHost(string $host): self
144
    {
145
        $this->host = $host;
146
        return $this;
147
    }
148
149
    /**
150
     * @return array
151
     */
152
    public function getExtends(): array
153
    {
154
        return $this->extends;
155
    }
156
157
    /**
158
     * @param array $extends
159
     * @return $this
160
     */
161
    public function setExtends(array $extends): self
162
    {
163
        $this->extends = $extends;
164
        return $this;
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    public function getDefaults(): array
171
    {
172
        return $this->defaults;
173
    }
174
175
    /**
176
     * @param array $defaults
177
     * @return $this
178
     */
179
    public function setDefaults(array $defaults): self
180
    {
181
        $this->defaults = $defaults;
182
        return $this;
183
    }
184
185
    /**
186
     * @return null|array
187
     */
188
    public function getMethods(): ?array
189
    {
190
        return $this->methods;
191
    }
192
193
    /**
194
     * @param null|array $methods
195
     * @return $this
196
     */
197
    public function setMethods(?array $methods): self
198
    {
199
        $this->methods = $methods;
200
        return $this;
201
    }
202
203
    /**
204
     * @return \Generator
205
     */
206
    protected function patterns(): \Generator
207
    {
208
        foreach ($this->collections as $collection) {
209
            foreach ($collection as $pattern) {
210
                yield $pattern;
211
            }
212
        }
213
    }
214
215
    /**
216
     * @return array
217
     */
218
    protected function getResolver(): array
219
    {
220
        $patterns = [];
221
222
        /**
223
         * @var Pattern[] $resolver
224
         */
225
        $resolver = \array_merge(
226
            $this->resolver,
227
            \iterator_to_array($this->patterns())
228
        );
229
230
        foreach ($resolver as $pattern) {
231
            $patterns[] = $pattern->toArray();
232
        }
233
234
        return \array_merge(...$patterns);
235
    }
236
237
    /**
238
     * @return array
239
     */
240
    public function toArray(): array
241
    {
242
        return [
243
            $this->getName() => [
244
                'type' => 'prefix',
245
                'protocol' => $this->getProtocol(),
246
                'host' => $this->getHost(),
247
                'path' => $this->path,
248
                'methods' => $this->getMethods(),
249
                'resolver' => $this->getResolver(),
250
                'defaults' => $this->getDefaults(),
251
                'extends' => $this->getExtends(),
252
            ]
253
        ];
254
    }
255
256
    /**
257
     * @return array
258
     */
259
    public function jsonSerialize(): array
260
    {
261
        return $this->toArray();
262
    }
263
264
}
265