summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Router/Route.php26
-rw-r--r--src/Router/Routes/Glob.php198
-rw-r--r--src/Router/Routes/RouteBinding.php314
3 files changed, 327 insertions, 211 deletions
diff --git a/src/Router/Route.php b/src/Router/Route.php
index be5a052..d2158ea 100644
--- a/src/Router/Route.php
+++ b/src/Router/Route.php
@@ -10,27 +10,21 @@ class Route extends \stdClass
/**
* Class constructor
*
- * @param array $values
+ * @param array|stdClass $values
*/
- public function __construct(array $values)
- {
- foreach ($values as $key => $value) {
- $this->$key = $value;
- }
- }
-
- /**
- * Factory method
- *
- * @param array|\stdClass $values
- * @return Route
- */
- public static function create($values)
+ public function __construct($values)
{
if ($values instanceof \stdClass) {
$values = get_object_vars($values);
}
- return new static($values);
+ if (!is_array($values)) {
+ $type = (is_object($values) ? get_class($values) . ' ' : '') . gettype($values);
+ throw new \InvalidArgumentException("Route values should be an array, not a $type");
+ }
+
+ foreach ($values as $key => $value) {
+ $this->$key = $value;
+ }
}
}
diff --git a/src/Router/Routes/Glob.php b/src/Router/Routes/Glob.php
index 4c6d93c..a4495ee 100644
--- a/src/Router/Routes/Glob.php
+++ b/src/Router/Routes/Glob.php
@@ -6,6 +6,7 @@ use ArrayObject;
use Jasny\Router\UrlParsing;
use Jasny\Router\Routes;
use Jasny\Router\Route;
+use Jasny\Router\Routes\RouteBinding;
use Psr\Http\Message\ServerRequestInterface;
/**
@@ -14,6 +15,7 @@ use Psr\Http\Message\ServerRequestInterface;
class Glob extends ArrayObject implements Routes
{
use UrlParsing;
+ use RouteBinding;
/**
* Class constructor
@@ -47,7 +49,7 @@ class Glob extends ArrayObject implements Routes
throw new \InvalidArgumentException("Unable to create a Route from value " . var_export($value, true));
}
- return Route::create($value);
+ return new Route($value);
}
/**
@@ -162,200 +164,6 @@ class Glob extends ArrayObject implements Routes
/**
- * Fill out the routes variables based on the url parts.
- *
- * @param array|\stdClass $vars Route variables
- * @param ServerRequestInterface $request
- * @param array $parts URL parts
- * @return array
- */
- protected function bind($vars, ServerRequestInterface $request, array $parts)
- {
- $values = [];
- $type = is_array($vars) && array_keys($vars) === array_keys(array_keys($vars)) ? 'numeric' : 'assoc';
-
- foreach ($vars as $key => $var) {
- if (!isset($var)) continue;
-
- if (is_object($var) && !$var instanceof \stdClass) {
- $part = array($var);
- } elseif (!is_scalar($var)) {
- $part = array($this->bind($var, $request, $parts));
- } elseif ($var[0] === '$') {
- $options = array_map('trim', explode('|', $var));
- $part = $this->bindVar($type, $request, $parts, $options);
- } elseif ($var[0] === '~' && substr($var, -1) === '~') {
- $pieces = array_map('trim', explode('~', substr($var, 1, -1)));
- $bound = array_filter($this->bind($pieces, $request, $parts));
- $part = array(join('', $bound));
- } else {
- $part = array($var);
- }
-
- if ($type === 'assoc') {
- $values[$key] = $part[0];
- } else {
- $values = array_merge($values, $part);
- }
- }
-
- if ($vars instanceof Route) {
- $values = Route::create($values);
- } elseif (is_object($vars) && $type === 'assoc') {
- $values = (object)$values;
- }
-
- return $values;
- }
-
- /**
- * Bind variable
- *
- * @param string $type 'assoc' or 'numeric'
- * @param ServerRequestInterface $request
- * @param array $parts
- * @param array $options
- * @return array
- */
- protected function bindVar($type, ServerRequestInterface $request, array $parts, array $options)
- {
- foreach ($options as $option) {
- $value = null;
-
- $bound =
- $this->bindVarString($option, $value) ||
- $this->bindVarSuperGlobal($option, $request, $value) ||
- $this->bindVarRequestHeader($option, $request, $value) ||
- $this->bindVarMultipleUrlParts($option, $type, $parts, $value) ||
- $this->bindVarSingleUrlPart($option, $parts, $value);
-
- if ($bound && isset($value)) {
- return $value;
- }
- }
-
- return [null];
- }
-
- /**
- * Bind variable when option is a normal string
- *
- * @param string $option
- * @param mixed $value OUTPUT
- * @return boolean
- */
- protected function bindVarString($option, &$value)
- {
- if ($option[0] !== '$') {
- $value = [$option];
- return true;
- }
-
- return false;
- }
-
- /**
- * Bind variable when option is a super global
- *
- * @param string $option
- * @param mixed $value OUTPUT
- * @return boolean
- */
- protected function bindVarSuperGlobal($option, ServerRequestInterface $request, &$value)
- {
- if (preg_match('/^\$_(GET|POST|COOKIE)\[([^\[]*)\]$/i', $option, $matches)) {
- list(, $var, $key) = $matches;
-
- $var = strtolower($var);
- $data = null;
-
- if ($var === 'get') {
- $data = $request->getQueryParams();
- } elseif ($var === 'post') {
- $data = $request->getParsedBody();
- } elseif ($var === 'cookie') {
- $data = $request->getCookieParams();
- }
-
- $value = isset($data[$key]) ? [$data[$key]] : null;
- return true;
- }
-
- return false;
- }
-
- /**
- * Bind variable when option is a request header
- *
- * @param string $option
- * @param ServerRequestInterface $request
- * @param mixed $value OUTPUT
- * @return boolean
- */
- protected function bindVarRequestHeader($option, ServerRequestInterface $request, &$value)
- {
- if (preg_match('/^\$(?:HTTP_)?([A-Z_]+)$/', $option, $matches)) {
- $sentence = preg_replace('/[\W_]+/', ' ', $matches[1]);
- $name = str_replace(' ', '-', ucwords($sentence));
-
- $value = [$request->getHeaderLine($name)];
- return true;
- }
-
- return false;
- }
-
- /**
- * Bind variable when option contains multiple URL parts
- *
- * @param string $option
- * @param string $type 'assoc' or 'numeric'
- * @param array $parts Url parts
- * @param mixed $value OUTPUT
- * @return boolean
- */
- protected function bindVarMultipleUrlParts($option, $type, array $parts, &$value)
- {
- if (substr($option, -3) === '...' && ctype_digit(substr($option, 1, -3))) {
- $i = (int)substr($option, 1, -3);
-
- if ($type === 'assoc') {
- throw new \InvalidArgumentException("Binding multiple parts using '$option' is only allowed in numeric arrays");
- } else {
- $value = array_slice($parts, $i - 1);
- }
-
- return true;
- }
-
- return false;
- }
-
- /**
- * Bind variable when option contains a single URL part
- *
- * @param string $option
- * @param array $parts Url parts
- * @param mixed $value OUTPUT
- * @return boolean
- */
- protected function bindVarSingleUrlPart($option, array $parts, &$value)
- {
- if (ctype_digit(substr($option, 1))) {
- $i = (int)substr($option, 1);
- $part = array_slice($parts, $i - 1, 1);
-
- if (!empty($part)) {
- $value = $part;
- return true;
- }
- }
-
- return false;
- }
-
-
- /**
* Check if a route for the URL exists
*
* @param ServerRequestInterface $request
diff --git a/src/Router/Routes/RouteBinding.php b/src/Router/Routes/RouteBinding.php
new file mode 100644
index 0000000..91563c3
--- /dev/null
+++ b/src/Router/Routes/RouteBinding.php
@@ -0,0 +1,314 @@
+<?php
+
+namespace Jasny\Router\Routes;
+
+use Jasny\Router\Route;
+use Psr\Http\Message\ServerRequestInterface;
+
+/**
+ * Functionality to set route properties based on url parameters
+ */
+trait RouteBinding
+{
+ /**
+ * Fill out the routes variables based on the url parts.
+ *
+ * @param array|\stdClass $vars Route variables
+ * @param ServerRequestInterface $request
+ * @param array $parts URL parts
+ * @return array
+ */
+ protected function bind($vars, ServerRequestInterface $request, array $parts)
+ {
+ $type = is_array($vars) && array_keys($vars) === array_keys(array_keys($vars)) ? 'numeric' : 'assoc';
+
+ $values = $this->bindParts($vars, $type, $request, $parts);
+
+ if ($vars instanceof Route) {
+ $class = get_class($vars);
+ $values = new $class($values);
+ } elseif (is_object($vars) && $type === 'assoc') {
+ $values = (object)$values;
+ }
+
+ return $values;
+ }
+
+
+ /**
+ * Fill out the values based on the url parts.
+ *
+ * @param array|\stdClass $vars Route variables
+ * @param string $type
+ * @param ServerRequestInterface $request
+ * @param array $parts URL parts
+ * @return array
+ */
+ protected function bindParts($vars, $type, ServerRequestInterface $request, array $parts)
+ {
+ $values = [];
+
+ foreach ($vars as $key => $var) {
+ $part = null;
+
+ $bound =
+ $this->bindPartObject($var, $part) ||
+ $this->bindPartArray($var, $request, $parts, $part) ||
+ $this->bindPartVar($var, $type, $request, $parts, $part) ||
+ $this->bindPartConcat($var, $request, $parts, $part) ||
+ $this->bindPartValue($var, $part);
+
+ if (!$bound) continue;
+
+ if ($type === 'assoc') {
+ $values[$key] = $part[0];
+ } else {
+ $values = array_merge($values, $part);
+ }
+ }
+
+ return $values;
+ }
+
+ /**
+ * Bind part if it's an object
+ *
+ * @param mixed $var
+ * @param array $part OUTPUT
+ * @return boolean
+ */
+ protected function bindPartObject($var, &$part)
+ {
+ if (!is_object($var) || $var instanceof \stdClass) {
+ return false;
+ }
+
+ $part = [$var];
+ return true;
+ }
+
+ /**
+ * Bind part if it's an array
+ *
+ * @param mixed $var
+ * @param ServerRequestInterface $request
+ * @param array $parts
+ * @param array $part OUTPUT
+ * @return boolean
+ */
+ protected function bindPartArray($var, ServerRequestInterface $request, array $parts, &$part)
+ {
+ if (!is_array($var) && !$var instanceof \stdClass) {
+ return false;
+ }
+
+ $part = [$this->bind($var, $request, $parts)];
+ return true;
+ }
+
+ /**
+ * Bind part if it's an variable
+ *
+ * @param mixed $var
+ * @param string $type
+ * @param ServerRequestInterface $request
+ * @param array $parts
+ * @param array $part OUTPUT
+ * @return boolean
+ */
+ protected function bindPartVar($var, $type, ServerRequestInterface $request, array $parts, &$part)
+ {
+ if (!is_string($var) || $var[0] !== '$') {
+ return false;
+ }
+
+ $options = array_map('trim', explode('|', $var));
+ $part = $this->bindVar($type, $request, $parts, $options);
+ return true;
+ }
+
+ /**
+ * Bind part if it's an concatenation
+ *
+ * @param mixed $var
+ * @param ServerRequestInterface $request
+ * @param array $parts
+ * @param array $part OUTPUT
+ * @return boolean
+ */
+ protected function bindPartConcat($var, ServerRequestInterface $request, array $parts, &$part)
+ {
+ if (!is_string($var) || $var[0] !== '~' || substr($var, -1) !== '~') {
+ return false;
+ }
+
+ $pieces = array_map('trim', explode('~', substr($var, 1, -1)));
+ $bound = array_filter($this->bind($pieces, $request, $parts));
+ $part = [join('', $bound)];
+
+ return true;
+ }
+
+ /**
+ * Bind part if it's a normal value
+ *
+ * @param mixed $var
+ * @param array $part OUTPUT
+ * @return boolean
+ */
+ protected function bindPartValue($var, &$part)
+ {
+ if (!isset($var)) {
+ return false;
+ }
+
+ $part = [$var];
+ return true;
+ }
+
+ /**
+ * Bind variable
+ *
+ * @param string $type 'assoc' or 'numeric'
+ * @param ServerRequestInterface $request
+ * @param array $parts
+ * @param array $options
+ * @return array
+ */
+ protected function bindVar($type, ServerRequestInterface $request, array $parts, array $options)
+ {
+ foreach ($options as $option) {
+ $value = null;
+
+ $bound =
+ $this->bindVarString($option, $value) ||
+ $this->bindVarSuperGlobal($option, $request, $value) ||
+ $this->bindVarRequestHeader($option, $request, $value) ||
+ $this->bindVarMultipleUrlParts($option, $type, $parts, $value) ||
+ $this->bindVarSingleUrlPart($option, $parts, $value);
+
+ if ($bound && isset($value)) {
+ return $value;
+ }
+ }
+
+ return [null];
+ }
+
+ /**
+ * Bind variable when option is a normal string
+ *
+ * @param string $option
+ * @param mixed $value OUTPUT
+ * @return boolean
+ */
+ protected function bindVarString($option, &$value)
+ {
+ if ($option[0] !== '$') {
+ $value = [$option];
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Bind variable when option is a super global
+ *
+ * @param string $option
+ * @param mixed $value OUTPUT
+ * @return boolean
+ */
+ protected function bindVarSuperGlobal($option, ServerRequestInterface $request, &$value)
+ {
+ if (preg_match('/^\$_(GET|POST|COOKIE)\[([^\[]*)\]$/i', $option, $matches)) {
+ list(, $var, $key) = $matches;
+
+ $var = strtolower($var);
+ $data = null;
+
+ if ($var === 'get') {
+ $data = $request->getQueryParams();
+ } elseif ($var === 'post') {
+ $data = $request->getParsedBody();
+ } elseif ($var === 'cookie') {
+ $data = $request->getCookieParams();
+ }
+
+ $value = isset($data[$key]) ? [$data[$key]] : null;
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Bind variable when option is a request header
+ *
+ * @param string $option
+ * @param ServerRequestInterface $request
+ * @param mixed $value OUTPUT
+ * @return boolean
+ */
+ protected function bindVarRequestHeader($option, ServerRequestInterface $request, &$value)
+ {
+ if (preg_match('/^\$(?:HTTP_)?([A-Z_]+)$/', $option, $matches)) {
+ $sentence = preg_replace('/[\W_]+/', ' ', $matches[1]);
+ $name = str_replace(' ', '-', ucwords($sentence));
+
+ $value = [$request->getHeaderLine($name)];
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Bind variable when option contains multiple URL parts
+ *
+ * @param string $option
+ * @param string $type 'assoc' or 'numeric'
+ * @param array $parts Url parts
+ * @param mixed $value OUTPUT
+ * @return boolean
+ */
+ protected function bindVarMultipleUrlParts($option, $type, array $parts, &$value)
+ {
+ if (substr($option, -3) === '...' && ctype_digit(substr($option, 1, -3))) {
+ $i = (int)substr($option, 1, -3);
+
+ if ($type === 'assoc') {
+ throw new \InvalidArgumentException("Binding multiple parts using '$option' is only allowed in numeric arrays");
+ } else {
+ $value = array_slice($parts, $i - 1);
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * Bind variable when option contains a single URL part
+ *
+ * @param string $option
+ * @param array $parts Url parts
+ * @param mixed $value OUTPUT
+ * @return boolean
+ */
+ protected function bindVarSingleUrlPart($option, array $parts, &$value)
+ {
+ if (ctype_digit(substr($option, 1))) {
+ $i = (int)substr($option, 1);
+ $part = array_slice($parts, $i - 1, 1);
+
+ if (!empty($part)) {
+ $value = $part;
+ return true;
+ }
+ }
+
+ return false;
+ }
+}