Created
May 12, 2020 00:53
-
-
Save erandirjunior/29cc0aabcc2185e8458e9082248311ce to your computer and use it in GitHub Desktop.
Diferença entre arquivos após a refatoração
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PlugRoute; | |
use PlugRoute\Helpers\PlugHelper; | |
class DynamicRoute implements Router | |
{ | |
private $route; | |
private $urlParameters; | |
private $indentifiers; | |
private $router; | |
public function __construct() | |
{ | |
$this->route = ''; | |
$this->urlParameters = []; | |
$this->indentifiers = []; | |
} | |
public function handle(string $route, string $url) | |
{ | |
$matches = PlugHelper::getMatchAll($route, '({.+?(?:\:.*?)?})'); | |
if ($matches) { | |
$this->manipulateRoute($route, $url, $matches); | |
return $this; | |
} | |
return $this->router->handle($route, $url); | |
} | |
public function getParameters() | |
{ | |
return $this->urlParameters; | |
} | |
public function next(Router $router) | |
{ | |
$this->router = $router; | |
} | |
public function route() | |
{ | |
return $this->route; | |
} | |
public function manipulateRoute($route, $url, $matches) | |
{ | |
$routeReplaced = $route; | |
$matchPrepared = []; | |
foreach ($matches as $key => $match) { | |
$identifiers[] = "|##{$key}##|"; | |
$getMatchRoute = PlugHelper::getMatchCase($match, ':(.*?)}'); | |
$getMatchRoute = $getMatchRoute ? $getMatchRoute[1] : null; | |
$matchPrepared[$key] = $this->getRegex($getMatchRoute, $match, $route); | |
$routeReplaced = str_replace($match, $identifiers[$key], $routeReplaced); | |
} | |
$route = $this->replaces($routeReplaced, $identifiers, $matchPrepared); | |
$finalMatch = PlugHelper::getMatchCase($url, "({$route})"); | |
$this->route = !empty($finalMatch) ? $finalMatch[1] : null; | |
$this->getDynamicValues($matches, $finalMatch); | |
} | |
private function getRegex($matchRoute, $index, $route) | |
{ | |
if (is_null($matchRoute)) { | |
$lengthHaystack = strstr($route, $index); | |
return strlen($lengthHaystack) > strlen($index) ? '(.+?)' : '(.+)'; | |
} | |
return $matchRoute === '?' ? '((?:.+)?)' : "({$matchRoute})"; | |
} | |
private function getDynamicValues($dynamicParameters, $matches) | |
{ | |
$matches = PlugHelper::removeValuesByIndex($matches, [0, 1]); | |
foreach ($dynamicParameters as $k => $v) { | |
$v = PlugHelper::replace(['{', '}'], '', $v); | |
$strToArray = PlugHelper::stringToArray($v, ':'); | |
$value = $strToArray ? $strToArray[0] : $v; | |
if (isset($matches[$k])) { | |
$this->urlParameters[$value] = $matches[$k]; | |
} | |
} | |
} | |
private function replaces($routePrepared, $identifiers, $matchPrepared) | |
{ | |
$routePrepared = PlugHelper::replace('/', '\/', $routePrepared); | |
$routePrepared = PlugHelper::replace($identifiers, $matchPrepared, $routePrepared); | |
$routePrepared = preg_replace('/(\.\+)(\d|\a|\[)/', '$1?$2', $routePrepared); | |
return $routePrepared; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace PlugRoute; | |
use PlugRoute\Helpers\MatchHelper; | |
class DynamicRoute extends RouteAnalyzer | |
{ | |
private $matches; | |
private $indentifiers; | |
protected function checkIfCanHandleRoute(string $route, string $url) | |
{ | |
$pattern = '({.+?(?:\:.*?)?})'; | |
$this->matches = MatchHelper::getMatchAll($route, $pattern, 0); | |
return $this->matches; | |
} | |
/** | |
* Replace all dynamic values by regex. | |
* @see organizeMatches | |
* @see replace | |
* | |
* set route | |
* @see setRoute | |
* | |
* set dynamic values. | |
* @see getDynamicValues | |
* | |
* @param $route | |
* @param $url | |
* @param $matches | |
*/ | |
protected function handleRoute(string $route, string $url) | |
{ | |
$this->indentifiers = []; | |
$this->route = $route; | |
$organizedMatches = $this->organizeMatches($this->matches); | |
$route = $this->prepareRoute($route, $organizedMatches); | |
$matchCase = MatchHelper::getMatchCase($url, $route); | |
$this->setRouteAndDynamicValuesIfMatchCase($matchCase); | |
} | |
private function organizeMatches($matches) | |
{ | |
$matchesOrganized = $this->getArrayMatch(); | |
foreach ($matches as $value) { | |
$valueWithoutKeys = str_replace(['{', '}'], '', $value); | |
$arrayMatch = explode(':', $valueWithoutKeys); | |
$this->indentifiers[] = $arrayMatch[0]; | |
$this->setRegexIfValueIsOptional( | |
$matchesOrganized, | |
$value, | |
$arrayMatch[1] | |
); | |
} | |
return $matchesOrganized; | |
} | |
protected function prepareRoute(string $route, $organizedMatches) | |
{ | |
$route = $this->replace('regex', $organizedMatches, $route); | |
$route = $this->replace('all', $organizedMatches, $route); | |
$route = $this->replace('optional', $organizedMatches, $route); | |
$route = str_replace('/', '\/', $route); | |
return $route; | |
} | |
private function replace($key, $data, $route) | |
{ | |
return str_replace($data[$key]['value'], $data[$key]['match'], $route); | |
} | |
private function setRoute(&$matchCase) | |
{ | |
$this->route = array_shift($matchCase); | |
} | |
private function getDynamicValues($matchCase): void | |
{ | |
foreach ($this->indentifiers as $key => $value) { | |
$this->parameters[$value] = $matchCase[$key]; | |
} | |
} | |
private function getRegexForValuesWithoutRegex($index) | |
{ | |
$lengthHaystack = strstr($this->route, $index); | |
return strlen($lengthHaystack) > strlen($index) ? '(.+?)' : '(.+)'; | |
} | |
private function setRegexIfValueIsOptional(&$matchesOrganized, $value, $match) | |
{ | |
if ($match !== '?') { | |
return $this->setRegexIfValueNotHasRegex($matchesOrganized, $value, $match); | |
} | |
$matchesOrganized['optional']['value'][] = $value; | |
$matchesOrganized['optional']['match'][] = '((?:.+)?)'; | |
} | |
private function setRegexIfValueNotHasRegex(&$matchesOrganized, $value, $match) | |
{ | |
if (!empty($match)) { | |
return $this->setRegexIfValueHasRegex($matchesOrganized, $value, $match); | |
} | |
$matchesOrganized['all']['value'][] = $value; | |
$matchesOrganized['all']['match'][] = $this->getRegexForValuesWithoutRegex($value); | |
} | |
private function setRegexIfValueHasRegex(&$matchesOrganized, $value, $match) | |
{ | |
$matchesOrganized['regex']['value'][] = $value; | |
$matchesOrganized['regex']['match'][] = "({$match})"; | |
} | |
private function getArrayMatch() | |
{ | |
foreach (['optional', 'regex', 'all'] as $value) { | |
$matchesOrganized[$value] = [ | |
'match' => [], | |
'value' => [], | |
]; | |
} | |
return $matchesOrganized; | |
} | |
protected function setRouteAndDynamicValuesIfMatchCase($matchCase) | |
{ | |
if ($matchCase) { | |
$this->setRoute($matchCase); | |
$this->getDynamicValues($matchCase); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment