Passed
Branch dev (8b2306)
by Alex
02:48
created

Group::setConstraintPreservingQuantifiers()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 7
cp 0
rs 9.4285
cc 3
eloc 7
nc 4
nop 2
crap 12
1
<?php
2
3
/**
4
 * Codeburner Framework.
5
 *
6
 * @author Alex Rohleder <[email protected]>
7
 * @copyright 2016 Alex Rohleder
8
 * @license http://opensource.org/licenses/MIT
9
 */
10
11
namespace Codeburner\Router;
12
13
/**
14
 * Group several routes and abstract operations applied to all.
15
 *
16
 * @author Alex Rohleder <[email protected]>
17
 */
18
19
class Group
20
{
21
22
    /**
23
     * All grouped route objects.
24
     *
25
     * @var Route[]
26
     */
27
28
    protected $routes;
29
30
    /**
31
     * Group constructor.
32
     *
33
     * @param Route[] $routes
34
     */
35
36 28
    public function __construct(array $routes = [])
37
    {
38 28
        $this->routes = $routes;
39 28
    }
40
41
    /**
42
     * Set a new Route or merge an existing group of routes.
43
     *
44
     * @param Group|Route $route
45
     * @return self
46
     */
47
48 8
    public function set($route)
49
    {
50 8
        if ($route instanceof Group) {
51 8
            foreach ($route->all() as $r)
52 8
                $this->routes[] = $r;
53 8
        } else  $this->routes[] = $route;
54 8
        return  $this;
55
    }
56
57
    /**
58
     * Return all grouped routes objects.
59
     *
60
     * @return Route[]
61
     */
62
63 9
    public function all()
64
    {
65 9
        return $this->routes;
66
    }
67
68
    /**
69
     * Get a specific route of the group, routes receive a key based on
70
     * the order they are added to the group.
71
     *
72
     * @param int $number
73
     * @return Route
74
     */
75
76 3
    public function nth($number)
77
    {
78 3
        return $this->routes[$number];
79
    }
80
81
    /**
82
     * Forget the registration of all grouped routes on to collector.
83
     * After the forget the route object will still exist but will not
84
     * count for the matcher.
85
     *
86
     * @return self
87
     */
88
89 3
    public function forget()
90
    {
91 3
        foreach ($this->routes as $route)
92 3
            $route->forget();
93 3
        return $this;
94
    }
95
96
    /**
97
     * Set one HTTP method to all grouped routes.
98
     *
99
     * @param string $method The HTTP Method
100
     * @return self
101
     */
102
103
    public function setMethod($method)
104
    {
105
        foreach ($this->routes as $route)
106
            $route->setMethod($method);
107
        return $this;
108
    }
109
110
    /**
111
     * Set one action to all grouped routes.
112
     *
113
     * @param string $action
114
     * @return self
115
     */
116
117
    public function setAction($action)
118
    {
119
        foreach ($this->routes as $route)
120
            $route->setAction($action);
121
        return $this;
122
    }
123
124
    /**
125
     * Set one namespace to all grouped routes.
126
     *
127
     * @param string $namespace
128
     * @return self
129
     */
130
131
    public function setNamespace($namespace)
132
    {
133
        foreach ($this->routes as $route)
134
            $route->setNamespace($namespace);
135
        return $this;
136
    }
137
138
    /**
139
     * Add a prefix to all grouped routes pattern.
140
     *
141
     * @param string $prefix
142
     * @return self
143
     */
144
145 3
    public function setPrefix($prefix)
146
    {
147 3
        $prefix = "/" . ltrim($prefix, "/");
148 3
        foreach ($this->routes as $route)
149 3
            $route->setPattern(rtrim($prefix . $route->getPattern(), "/"));
150 3
        return $this;
151
    }
152
153
    /**
154
     * Set metadata to all grouped routes.
155
     *
156
     * @param string $key
157
     * @param string $value
158
     *
159
     * @return $this
160
     */
161
162
    public function setMetadata($key, $value)
163
    {
164
        foreach ($this->routes as $route)
165
            $route->setMetadata($key, $value);
166
        return $this;
167
    }
168
169
    /**
170
     * Set a bunch of metadata to all grouped routes.
171
     *
172
     * @param mixed[] $metadata
173
     * @return $this
174
     */
175
176
    public function setMetadataArray(array $metadata)
177
    {
178
        foreach ($this->routes as $route)
179
            $route->setMetadataArray($metadata);
180
        return $this;
181
    }
182
183
    /**
184
     * Set default parameters to all grouped routes.
185
     *
186
     * @param mixed[] $defaults
187
     * @return $this
188
     */
189
190
    public function setDefaults(array $defaults)
191
    {
192
        foreach ($this->routes as $route)
193
            $route->setDefaults($defaults);
194
        return $this;
195
    }
196
197
    /**
198
     * Set a default parameter to all grouped routes.
199
     *
200
     * @param string $key
201
     * @param mixed $value
202
     *
203
     * @return $this
204
     */
205
206
    public function setDefault($key, $value)
207
    {
208
        foreach ($this->routes as $route)
209
            $route->setDefault($key, $value);
210
        return $this;
211
    }
212
213
    /**
214
     * Set one dispatch strategy to all grouped routes.
215
     *
216
     * @param string|Strategies\StrategyInterface $strategy
217
     * @return self
218
     */
219
220 7
    public function setStrategy($strategy)
221
    {
222 7
        foreach ($this->routes as $route)
223 7
            $route->setStrategy($strategy);
224 7
        return $this;
225
    }
226
227
    /**
228
     * Replace or define a constraint for all dynamic segments named by $name.
229
     *
230
     * @param string $name
231
     * @param string $regex
232
     *
233
     * @return self
234
     */
235
236
    public function setConstraint($name, $regex)
237
    {
238
        foreach ($this->routes as $route) {
239
            $pattern = $route->getPattern();
240
            $initPos = strpos($pattern, "{" . $name);
241
242
            if ($initPos !== false) {
243
                $endPos = strpos($pattern, "}", $initPos);
244
                $newPattern = substr_replace($pattern, "{" . "$name:$regex" . "}", $initPos, $endPos - $initPos + 1);
245
                $route->setPatternWithoutReset($newPattern);
246
            }
247
        }
248
249
        return $this;
250
    }
251
252
    /**
253
     * Define a constraint for a variable in all grouped routes pattern, but don't replace the
254
     * delimiter if one already exists.
255
     *
256
     * @param string $name
257
     * @param string $regex
258
     *
259
     * @return self
260
     */
261
262
    public function setConstraintPreservingQuantifiers($name, $regex)
263
    {
264
        $wildcards = $this->routes[0]->getCollector()->getWildcards();
265
        $quantifierPos = strpos($regex, "{") + strpos($regex, "+") + strpos($regex, "*");
266
        $quantifier = substr($regex, $quantifierPos);
267
        $quantifierErased = $quantifierPos !== false ? substr($regex, 0, $quantifierPos) : null;
268
        $regex = isset($wildcards[$quantifierErased]) ? $wildcards[$quantifierErased] . $quantifier : $regex;
269
270
        return $this->setConstraint($name, $regex);
271
    }
272
273
}
274