1 | <?php |
||
30 | class Route |
||
31 | { |
||
32 | use AccessorTrait; |
||
33 | |||
34 | static protected $invalid_construct_properties = [ 'formatting_value', 'url', 'absolute_url' ]; |
||
35 | |||
36 | /** |
||
37 | * Creates a new {@link Route} instance from a route definition. |
||
38 | * |
||
39 | * @param array $definition |
||
40 | * |
||
41 | * @return static |
||
42 | */ |
||
43 | static public function from(array $definition) |
||
44 | { |
||
45 | $class = get_called_class(); |
||
46 | |||
47 | if (isset($definition[RouteDefinition::CONSTRUCTOR])) |
||
48 | { |
||
49 | $class = $definition[RouteDefinition::CONSTRUCTOR]; |
||
50 | } |
||
51 | |||
52 | return new $class($definition[RouteDefinition::PATTERN], $definition); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Pattern of the route. |
||
57 | * |
||
58 | * @var Pattern |
||
59 | */ |
||
60 | private $pattern; |
||
61 | |||
62 | protected function get_pattern() |
||
66 | |||
67 | /** |
||
68 | * Controller's class name or function. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | private $controller; |
||
73 | |||
74 | protected function get_controller() |
||
78 | |||
79 | /** |
||
80 | * Controller action. |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | private $action; |
||
85 | |||
86 | protected function get_action() |
||
90 | |||
91 | /** |
||
92 | * Identifier of the route. |
||
93 | * |
||
94 | * @var string |
||
95 | */ |
||
96 | private $id; |
||
97 | |||
98 | protected function get_id() |
||
102 | |||
103 | /** |
||
104 | * Redirect location. |
||
105 | * |
||
106 | * If the property is defined the route is considered an alias. |
||
107 | * |
||
108 | * @var string |
||
109 | */ |
||
110 | private $location; |
||
111 | |||
112 | protected function get_location() |
||
116 | |||
117 | /** |
||
118 | * Request methods accepted by the route. |
||
119 | * |
||
120 | * @var string |
||
121 | */ |
||
122 | private $via; |
||
123 | |||
124 | protected function get_via() |
||
128 | |||
129 | /** |
||
130 | * Formatting value. |
||
131 | * |
||
132 | * @var mixed |
||
133 | */ |
||
134 | private $formatting_value; |
||
135 | |||
136 | /** |
||
137 | * Returns the formatting value. |
||
138 | * |
||
139 | * @return mixed |
||
140 | */ |
||
141 | protected function get_formatting_value() |
||
145 | |||
146 | /** |
||
147 | * Whether the route has a formatting value. |
||
148 | * |
||
149 | * @return bool `true` if the route has a formatting value, `false` otherwise. |
||
150 | */ |
||
151 | protected function get_has_formatting_value() |
||
155 | |||
156 | /** |
||
157 | * Returns relative URL. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | protected function get_url() |
||
165 | |||
166 | /** |
||
167 | * Returns absolute URL. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | protected function get_absolute_url() |
||
175 | |||
176 | /** |
||
177 | * Initializes the {@link $pattern} property and the properties provided. |
||
178 | * |
||
179 | * @param string $pattern |
||
180 | * @param array $properties |
||
181 | */ |
||
182 | public function __construct($pattern, array $properties) |
||
183 | { |
||
184 | $this->pattern = Pattern::from($pattern); |
||
185 | |||
186 | unset($properties['pattern']); |
||
187 | |||
188 | $this->assert_properties_are_valid($properties, self::$invalid_construct_properties); |
||
189 | |||
190 | foreach ($properties as $property => $value) |
||
191 | { |
||
192 | $this->$property = $value; |
||
193 | } |
||
194 | } |
||
195 | |||
196 | public function __clone() |
||
200 | |||
201 | /** |
||
202 | * Formats a route into a relative URL using its formatting value. |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | public function __toString() |
||
210 | |||
211 | /** |
||
212 | * Asserts that properties are valid. |
||
213 | * |
||
214 | * @param array $properties |
||
215 | * @param array $invalid |
||
216 | * |
||
217 | * @throws \InvalidArgumentException if a property is not valid. |
||
218 | */ |
||
219 | protected function assert_properties_are_valid(array $properties, array $invalid) |
||
220 | { |
||
221 | $invalid = array_combine($invalid, $invalid); |
||
222 | $invalid = array_intersect_key($properties, $invalid); |
||
223 | |||
224 | if (!$invalid) |
||
225 | { |
||
226 | return; |
||
227 | } |
||
228 | |||
229 | throw new \InvalidArgumentException("Invalid construct property: " . implode(', ', $invalid)); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Formats the route with the specified values. |
||
234 | * |
||
235 | * Note: The formatting of the route is deferred to its {@link Pattern} instance. |
||
236 | * |
||
237 | * @param object|array|null $values |
||
238 | * |
||
239 | * @return FormattedRoute |
||
240 | */ |
||
241 | public function format($values = null) |
||
245 | |||
246 | /** |
||
247 | * Assigns a formatting value to a route. |
||
248 | * |
||
249 | * @param mixed $formatting_value A formatting value. |
||
250 | * |
||
251 | * @return Route A new route bound to a formatting value. |
||
252 | */ |
||
253 | public function assign($formatting_value) |
||
267 | } |
||
268 |