|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Routing\Route; |
|
26
|
|
|
|
|
27
|
|
|
use ArrayIterator; |
|
28
|
|
|
use IteratorAggregate; |
|
29
|
|
|
use Brickoo\Component\Routing\Route\Exception\DuplicateRouteException; |
|
30
|
|
|
use Brickoo\Component\Routing\Route\Exception\RouteNotFoundException; |
|
31
|
|
|
use Brickoo\Component\Common\Assert; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* RouteCollection |
|
35
|
|
|
* |
|
36
|
|
|
* Implements an iterable route collection. |
|
37
|
|
|
* @author Celestino Diaz <[email protected]> |
|
38
|
|
|
*/ |
|
39
|
|
|
class RouteCollection implements IteratorAggregate { |
|
40
|
|
|
|
|
41
|
|
|
/** @var string */ |
|
42
|
|
|
private $name; |
|
43
|
|
|
|
|
44
|
|
|
/** @var string */ |
|
45
|
|
|
private $path; |
|
46
|
|
|
|
|
47
|
|
|
/** @var array */ |
|
48
|
|
|
private $routes; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Class constructor. |
|
52
|
|
|
* @param string $name the collection (unique) name |
|
53
|
|
|
* @param string $path the routes common path |
|
54
|
|
|
* @param array $routes the initial collection routes |
|
55
|
|
|
*/ |
|
56
|
3 |
|
public function __construct($name = "", $path = "", array $routes = []) { |
|
57
|
3 |
|
Assert::isString($name); |
|
58
|
2 |
|
Assert::isString($path); |
|
59
|
|
|
|
|
60
|
1 |
|
$this->name = $name; |
|
61
|
1 |
|
$this->path = $path; |
|
62
|
1 |
|
$this->routes = []; |
|
63
|
1 |
|
$this->addRoutes($routes); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns the route collection (unique) name. |
|
68
|
|
|
* @return string the collection name |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getName() { |
|
71
|
1 |
|
return $this->name; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Checks if the name is set. |
|
76
|
|
|
* @return boolean check result |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function hasName() { |
|
79
|
1 |
|
return ($this->name != ""); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the routes common path. |
|
84
|
|
|
* @return string the routes common path |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function getPath() { |
|
87
|
1 |
|
return $this->path; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Checks if the path is set. |
|
92
|
|
|
* @return boolean check result |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function hasPath() { |
|
95
|
1 |
|
return ($this->path != ""); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns all containing routes. |
|
100
|
|
|
* @return array the containing routes |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function getRoutes() { |
|
103
|
1 |
|
return $this->routes; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Adds routes to the current collection. |
|
108
|
|
|
* @param array $routes values implementing \Brickoo\Component\Routing\Route\Route |
|
109
|
|
|
* @throws \Brickoo\Component\Routing\Route\Exception\DuplicateRouteException |
|
110
|
|
|
* @return \Brickoo\Component\Routing\Route\RouteCollection |
|
111
|
|
|
*/ |
|
112
|
2 |
|
public function addRoutes(array $routes) { |
|
113
|
2 |
|
foreach ($routes as $route) { |
|
114
|
2 |
|
if ($this->hasRoute(($routeName = $route->getName()))) { |
|
115
|
1 |
|
throw new DuplicateRouteException($routeName); |
|
116
|
|
|
} |
|
117
|
2 |
|
$this->routes[$routeName] = $route; |
|
118
|
2 |
|
} |
|
119
|
2 |
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Checks if the collection contains routes. |
|
124
|
|
|
* @return boolean check result |
|
125
|
|
|
*/ |
|
126
|
1 |
|
public function hasRoutes() { |
|
127
|
1 |
|
return (!empty($this->routes)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Returns the route matching the unique name. |
|
132
|
|
|
* @param string $routeName the route to return |
|
133
|
|
|
* @throws \Brickoo\Component\Routing\Route\Exception\RouteNotFoundException |
|
134
|
|
|
* @throws \InvalidArgumentException |
|
135
|
|
|
* @return \Brickoo\Component\Routing\Route\Route |
|
136
|
|
|
*/ |
|
137
|
3 |
|
public function getRoute($routeName) { |
|
138
|
3 |
|
Assert::isString($routeName); |
|
139
|
|
|
|
|
140
|
2 |
|
if (!$this->hasRoute($routeName)) { |
|
141
|
1 |
|
throw new RouteNotFoundException($routeName); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
return $this->routes[$routeName]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Checks if the route is in the collection. |
|
149
|
|
|
* @param string $routeName the route to check |
|
150
|
|
|
* @throws \InvalidArgumentException if an argument is invalid |
|
151
|
|
|
* @return boolean check result |
|
152
|
|
|
*/ |
|
153
|
2 |
|
public function hasRoute($routeName) { |
|
154
|
2 |
|
Assert::isString($routeName); |
|
155
|
1 |
|
return isset($this->routes[$routeName]); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* {@inheritDoc} |
|
160
|
|
|
* @see IteratorAggregate::getIterator() |
|
161
|
|
|
* @return \ArrayIterator containing the collection routes |
|
162
|
|
|
*/ |
|
163
|
1 |
|
public function getIterator() { |
|
164
|
1 |
|
return new ArrayIterator($this->getRoutes()); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|