|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2014 Krzysztof Magosa |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
* |
|
10
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
* See the License for the specific language governing permissions and |
|
14
|
|
|
* limitations under the License. |
|
15
|
|
|
*/ |
|
16
|
|
|
namespace KM\Saffron; |
|
17
|
|
|
|
|
18
|
|
|
use KM\Saffron\Exception\RouteAlreadyRegistered; |
|
19
|
|
|
use KM\Saffron\Collection; |
|
20
|
|
|
|
|
21
|
|
|
class RoutesCollection extends Collection |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Create instance of Route and returns it. |
|
25
|
|
|
* Also looks for duplicated names. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $name Name of route |
|
28
|
|
|
* @return Route |
|
29
|
|
|
*/ |
|
30
|
|
|
public function route($name) |
|
31
|
|
|
{ |
|
32
|
|
|
if ($this->offsetExists($name)) { |
|
33
|
|
|
throw new RouteAlreadyRegistered("Route with name $name is already registered"); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$route = new Route($name); |
|
37
|
|
|
$this[$name] = $route; |
|
38
|
|
|
return $route; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Groups routes by domain keeping the order |
|
43
|
|
|
* @mixed RoutesCollection |
|
44
|
|
|
*/ |
|
45
|
|
|
public function groupByDomain() |
|
46
|
|
|
{ |
|
47
|
|
|
$class = get_called_class(); |
|
48
|
|
|
$collection = new $class(); |
|
49
|
|
|
$index = 0; |
|
50
|
|
|
$lastDomain = null; |
|
51
|
|
|
|
|
52
|
|
|
foreach ($this as $route) { |
|
53
|
|
|
if ($route->getDomain() !== $lastDomain) { |
|
54
|
|
|
$lastDomain = $route->getDomain(); |
|
55
|
|
|
$index++; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (!isset($collection[$index])) { |
|
59
|
|
|
$collection[$index] = new $class(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$collection[$index]->append($route); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $collection; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Checks whether routes in collection has domain condition. |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function hasDomain() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->has( |
|
75
|
|
|
function ($route) { |
|
76
|
|
|
return $route->hasDomain(); |
|
77
|
|
|
} |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Checks whether routes in collection has method condition. |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function hasMethod() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->has( |
|
88
|
|
|
function ($route) { |
|
89
|
|
|
return $route->hasMethod(); |
|
90
|
|
|
} |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Checks whether routes in collection has https condition. |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
public function hasHttps() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->has( |
|
101
|
|
|
function ($route) { |
|
102
|
|
|
return $route->hasHttps(); |
|
103
|
|
|
} |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|