[ROUTES] PSR2-format

This commit is contained in:
brunoccast 2019-07-11 01:35:54 +01:00 committed by Diogo Cordeiro
parent d295d8b43c
commit 2032c7c1f7

View File

@ -55,12 +55,12 @@ class URLMapper
{ {
const ACTION = 'action'; const ACTION = 'action';
protected $statics = array(); protected $statics = [];
protected $variables = array(); protected $variables = [];
protected $reverse = array(); protected $reverse = [];
protected $allpaths = array(); protected $allpaths = [];
function connect($path, $args, $paramPatterns=array()) public function connect($path, $args, $paramPatterns = array())
{ {
if (!array_key_exists(self::ACTION, $args)) { if (!array_key_exists(self::ACTION, $args)) {
throw new Exception(sprintf("Can't connect %s; path has no action.", $path)); throw new Exception(sprintf("Can't connect %s; path has no action.", $path));
@ -75,9 +75,9 @@ class URLMapper
if (empty($paramNames)) { if (empty($paramNames)) {
$this->statics[$path] = $args; $this->statics[$path] = $args;
if (array_key_exists($action, $this->reverse)) { if (array_key_exists($action, $this->reverse)) {
$this->reverse[$args[self::ACTION]][] = array($args, $path); $this->reverse[$args[self::ACTION]][] = [$args, $path];
} else { } else {
$this->reverse[$args[self::ACTION]] = array(array($args, $path)); $this->reverse[$args[self::ACTION]] = [[$args, $path]];
} }
} else { } else {
@ -94,19 +94,19 @@ class URLMapper
$regex = self::makeRegex($path, $paramPatterns); $regex = self::makeRegex($path, $paramPatterns);
$this->variables[] = array($args, $regex, $paramNames); $this->variables[] = [$args, $regex, $paramNames];
$format = $this->makeFormat($path, $paramPatterns); $format = $this->makeFormat($path, $paramPatterns);
if (array_key_exists($action, $this->reverse)) { if (array_key_exists($action, $this->reverse)) {
$this->reverse[$args[self::ACTION]][] = array($args, $format, $paramNames); $this->reverse[$args[self::ACTION]][] = [$args, $format, $paramNames];
} else { } else {
$this->reverse[$args[self::ACTION]] = array(array($args, $format, $paramNames)); $this->reverse[$args[self::ACTION]] = [[$args, $format, $paramNames]];
} }
} }
} }
function match($path) public function match($path)
{ {
if (array_key_exists($path, $this->statics)) { if (array_key_exists($path, $this->statics)) {
return $this->statics[$path]; return $this->statics[$path];
@ -126,7 +126,7 @@ class URLMapper
throw new NoRouteMapException($path); throw new NoRouteMapException($path);
} }
function generate($args, $qstring, $fragment) public function generate($args, $qstring, $fragment)
{ {
if (!array_key_exists(self::ACTION, $args)) { if (!array_key_exists(self::ACTION, $args)) {
throw new Exception("Every path needs an action."); throw new Exception("Every path needs an action.");
@ -163,7 +163,7 @@ class URLMapper
// success // success
$toFormat = array(); $toFormat = [];
foreach ($paramNames as $name) { foreach ($paramNames as $name) {
if (!array_key_exists($name, $args)) { if (!array_key_exists($name, $args)) {
@ -207,12 +207,12 @@ class URLMapper
return $match['name']; return $match['name'];
} }
static function makeRegex($path, $paramPatterns) public static function makeRegex($path, $paramPatterns)
{ {
$pr = new PatternReplacer($paramPatterns); $pr = new PatternReplacer($paramPatterns);
$regex = preg_replace_callback('/:(\w+)/', $regex = preg_replace_callback('/:(\w+)/',
array($pr, 'toPattern'), [$pr, 'toPattern'],
$path); $path);
$regex = '#^' . str_replace('#', '\#', $regex) . '$#'; $regex = '#^' . str_replace('#', '\#', $regex) . '$#';
@ -237,12 +237,12 @@ class PatternReplacer
{ {
private $patterns; private $patterns;
function __construct($patterns) public function __construct($patterns)
{ {
$this->patterns = $patterns; $this->patterns = $patterns;
} }
function toPattern($matches) public function toPattern($matches)
{ {
// trim out the : // trim out the :
$name = $matches[1]; $name = $matches[1];