Passed
Branch dev (140aec)
by Alex
02:37
created

Group::setDefault()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
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
     * Set a new Route or merge an existing group of routes.
32
     *
33
     * @param Group|Route $route
34
     * @return self
35
     */
36
37 28
    public function set($route)
38
    {
39 28
        if ($route instanceof Group) {
40 28
            foreach ($route->all() as $r)
41 28
                $this->routes[] = $r;
42 28
        } else  $this->routes[] = $route;
43 28
        return  $this;
44
    }
45
46
    /**
47
     * A fast way to register a route into the group
48
     *
49
     * @param Route $route
50
     */
51
52 42
    public function setRoute(Route $route)
53
    {
54 42
        $this->routes[] = $route;
55 42
    }
56
57
    /**
58
     * Return all grouped routes objects.
59
     *
60
     * @return Route[]
61
     */
62
63 28
    public function all()
64
    {
65 28
        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 4
    public function nth($number)
77
    {
78 4
        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 1
    public function setMethod($method)
104
    {
105 1
        foreach ($this->routes as $route)
106 1
            $route->setMethod($method);
107 1
        return $this;
108
    }
109
110
    /**
111
     * Set one action to all grouped routes.
112
     *
113
     * @param string $action
114
     * @return self
115
     */
116
117 3
    public function setAction($action)
118
    {
119 3
        foreach ($this->routes as $route)
120 3
            $route->setAction($action);
121 3
        return $this;
122
    }
123
124
    /**
125
     * Set one namespace to all grouped routes.
126
     *
127
     * @param string $namespace
128
     * @return self
129
     */
130
131 1
    public function setNamespace($namespace)
132
    {
133 1
        foreach ($this->routes as $route)
134 1
            $route->setNamespace($namespace);
135 1
        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 1
    public function setMetadata($key, $value)
163
    {
164 1
        foreach ($this->routes as $route)
165 1
            $route->setMetadata($key, $value);
166 1
        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 1
    public function setMetadataArray(array $metadata)
177
    {
178 1
        foreach ($this->routes as $route)
179 1
            $route->setMetadataArray($metadata);
180 1
        return $this;
181
    }
182
183
    /**
184
     * Set default parameters to all grouped routes.
185
     *
186
     * @param mixed[] $defaults
187
     * @return $this
188
     */
189
190 1
    public function setDefaults(array $defaults)
191
    {
192 1
        foreach ($this->routes as $route)
193 1
            $route->setDefaults($defaults);
194 1
        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 1
    public function setDefault($key, $value)
207
    {
208 1
        foreach ($this->routes as $route)
209 1
            $route->setDefault($key, $value);
210 1
        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 9
    public function setStrategy($strategy)
221
    {
222 9
        foreach ($this->routes as $route)
223 9
            $route->setStrategy($strategy);
224 9
        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 1
    public function setConstraint($name, $regex)
237
    {
238 1
        foreach ($this->routes as $route)
239 1
            $route->setConstraint($name, $regex);
240 1
        return $this;
241
    }
242
243
}
244