1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\Routing;
use ICanBoogie\Accessor\AccessorTrait;
/**
* A route.
*
* @property-read Pattern $pattern The pattern of the route.
* @property-read string $controller The class name of the controller.
* @property-read string|null $action Controller action.
* @property-read string $id Route identifier.
* @property-read string|null $location Redirection destination.
* @property-read string|array|null $via The supported HTTP methods.
* @property-read string $url The contextualized URL of the route.
* @property-read string $absolute_url The contextualized absolute URL of the route.
* @property-read mixed $formatting_value The value used to format the route.
* @property-read bool $has_formatting_value `true` if the route has a formatting value, `false` otherwise.
*/
class Route
{
use AccessorTrait;
static protected $invalid_construct_properties = [ 'formatting_value', 'url', 'absolute_url' ];
/**
* Creates a new {@link Route} instance from a route definition.
*
* @param array $definition
*
* @return static
*/
static public function from(array $definition)
{
$class = get_called_class();
if (isset($definition[RouteDefinition::CONSTRUCTOR]))
{
$class = $definition[RouteDefinition::CONSTRUCTOR];
}
return new $class($definition[RouteDefinition::PATTERN], $definition);
}
/**
* Pattern of the route.
*
* @var Pattern
*/
private $pattern;
protected function get_pattern()
{
return $this->pattern;
}
/**
* Controller's class name or function.
*
* @var string
*/
private $controller;
protected function get_controller()
{
return $this->controller;
}
/**
* Controller action.
*
* @var string
*/
private $action;
protected function get_action()
{
return $this->action;
}
/**
* Identifier of the route.
*
* @var string
*/
private $id;
protected function get_id()
{
return $this->id;
}
/**
* Redirect location.
*
* If the property is defined the route is considered an alias.
*
* @var string
*/
private $location;
protected function get_location()
{
return $this->location;
}
/**
* Request methods accepted by the route.
*
* @var string
*/
private $via;
protected function get_via()
{
return $this->via;
}
/**
* Formatting value.
*
* @var mixed
*/
private $formatting_value;
/**
* Returns the formatting value.
*
* @return mixed
*/
protected function get_formatting_value()
{
return $this->formatting_value;
}
/**
* Whether the route has a formatting value.
*
* @return bool `true` if the route has a formatting value, `false` otherwise.
*/
protected function get_has_formatting_value()
{
return $this->formatting_value !== null;
}
/**
* Returns relative URL.
*
* @return string
*/
protected function get_url()
{
return $this->format($this->formatting_value)->url;
}
/**
* Returns absolute URL.
*
* @return string
*/
protected function get_absolute_url()
{
return $this->format($this->formatting_value)->absolute_url;
}
/**
* Initializes the {@link $pattern} property and the properties provided.
*
* @param string $pattern
* @param array $properties
*/
public function __construct($pattern, array $properties)
{
$this->pattern = Pattern::from($pattern);
unset($properties['pattern']);
$this->assert_properties_are_valid($properties, self::$invalid_construct_properties);
foreach ($properties as $property => $value)
{
$this->$property = $value;
}
}
public function __clone()
{
$this->formatting_value = null;
}
/**
* Formats a route into a relative URL using its formatting value.
*
* @return string
*/
public function __toString()
{
return (string) $this->url;
}
/**
* Asserts that properties are valid.
*
* @param array $properties
* @param array $invalid
*
* @throws \InvalidArgumentException if a property is not valid.
*/
protected function assert_properties_are_valid(array $properties, array $invalid)
{
$invalid = array_combine($invalid, $invalid);
$invalid = array_intersect_key($properties, $invalid);
if (!$invalid)
{
return;
}
throw new \InvalidArgumentException("Invalid construct property: " . implode(', ', $invalid));
}
/**
* Formats the route with the specified values.
*
* Note: The formatting of the route is deferred to its {@link Pattern} instance.
*
* @param object|array|null $values
*
* @return FormattedRoute
*/
public function format($values = null)
{
return new FormattedRoute($this->pattern->format($values), $this);
}
/**
* Assigns a formatting value to a route.
*
* @param mixed $formatting_value A formatting value.
*
* @return Route A new route bound to a formatting value.
*/
public function assign($formatting_value)
{
$clone = clone $this;
#
# We could write directly to `formatting_value`, but since it is marked _read-only_
# we resort to shenanigans to keep the IDE happy :)
#
$ref = &$clone->formatting_value;
$ref = $formatting_value;
return $clone;
}
}