Skip to content

Commit

Permalink
Merge pull request #24 from lohanidamodar/fix-route-path-getter
Browse files Browse the repository at this point in the history
refactor - rename URL to path
  • Loading branch information
eldadfux authored Aug 19, 2021
2 parents 0274f6b + 83928c2 commit f577522
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 51 deletions.
10 changes: 5 additions & 5 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public function match(Request $request)
\array_shift($this->matches);
$this->route = $route;

if($routeUrl == $route->getAliasURL()) {
if($routeUrl == $route->getAliasPath()) {
$this->route->setIsAlias(true);
} else {
$this->route->setIsAlias(false);
Expand All @@ -515,7 +515,7 @@ public function match(Request $request)
break;
}

if (!empty($this->route) && ('/' === $this->route->getURL()) && ($url != $this->route->getURL())) {
if (!empty($this->route) && ('/' === $this->route->getPath()) && ($url != $this->route->getPath())) {
return null;
}

Expand All @@ -535,7 +535,7 @@ public function execute(Route $route, array $args = []): self
$groups = $route->getGroups();

// Extract keys from URL
$url = $route->getIsAlias() ? $route->getAliasURL() : $route->getURL();
$url = $route->getIsAlias() ? $route->getAliasPath() : $route->getPath();
$keyRegex = '@^' . \preg_replace('@:[^/]+@', ':([^/]+)', $url) . '$@';
\preg_match($keyRegex, $url, $keys);

Expand Down Expand Up @@ -646,8 +646,8 @@ public function run(Request $request, Response $response): self
if (!self::$sorted) {
foreach (self::$routes as $method => $list) { //adding route alias in $routes
foreach ($list as $key => $route) {
if($route->getAliasURL()) {
self::$routes[$method][$route->getAliasURL()] = $route;
if($route->getAliasPath()) {
self::$routes[$method][$route->getAliasPath()] = $route;
}
}
}
Expand Down
79 changes: 39 additions & 40 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ class Route
protected $middleware = true;

/**
* URL
* Path
*
* @var string
*/
protected $URL = '';
protected $path = '';

/**
* Alias URL
* Alias path
*
* @var string
*/
protected $aliasURL = '';
protected $aliasPath = '';

/**
* Alias Params
*
Expand Down Expand Up @@ -117,40 +117,40 @@ class Route

/**
* @param string $method
* @param string $URL
* @param string $path
*/
public function __construct(string $method, string $URL)
public function __construct(string $method, string $path)
{
self::$counter++;

$this->URL($URL);
$this->path($path);
$this->method = $method;
$this->order = self::$counter;
$this->action = function(): void {};
$this->action = function (): void {};
}

/**
* Add URL
* Add path
*
* @param string $URL
* @param string $path
* @return $this
*/
public function URL($URL): self
public function path($path): self
{
$this->URL = $URL;
$this->path = $path;
return $this;
}

/**
* Add alias
*
* @param string $URL
* @param string $path
* @param array $params
* @return $this
*/
public function alias($URL, $params = []): self
public function alias($path, $params = []): self
{
$this->aliasURL = $URL;
$this->aliasPath = $path;
$this->aliasParams = $params;
return $this;
}
Expand All @@ -167,7 +167,6 @@ public function setIsAlias($isAlias): void
$this->isAlias = $isAlias;
}


/**
* Add Description
*
Expand Down Expand Up @@ -219,23 +218,23 @@ public function action(callable $action): self
public function param($key, $default, $validator, $description = '', $optional = false, array $injections = []): self
{
$this->params[$key] = [
'default' => $default,
'validator' => $validator,
'description' => $description,
'optional' => $optional,
'injections' => $injections,
'value' => null,
'order' => count($this->params) + count($this->injections),
'default' => $default,
'validator' => $validator,
'description' => $description,
'optional' => $optional,
'injections' => $injections,
'value' => null,
'order' => count($this->params) + count($this->injections),
];

return $this;
}

/**
* Set middleware status
*
*
* @param boolean $middleware
*
*
* @return self
*/
public function middleware($middleware = true): self
Expand All @@ -254,15 +253,15 @@ public function middleware($middleware = true): self
*/
public function inject($injection): self
{
if(array_key_exists($injection, $this->injections)) {
throw new Exception('Injection already declared for '.$injection);
if (array_key_exists($injection, $this->injections)) {
throw new Exception('Injection already declared for ' . $injection);
}

$this->injections[$injection] = [
'name' => $injection,
'name' => $injection,
'order' => count($this->params) + count($this->injections),
];

return $this;
}

Expand Down Expand Up @@ -291,25 +290,25 @@ public function getMethod(): string
}

/**
* Get URL
* Get path
*
* @return string
*/
public function getURL(): string
public function getPath(): string
{
return $this->URL;
return $this->path;
}

/**
* Get Alias URL
* Get Alias path
*
* @return string
*/
public function getAliasURL(): string
public function getAliasPath(): string
{
return $this->aliasURL;
return $this->aliasPath;
}

/**
* Get Alias Params
*
Expand All @@ -319,7 +318,7 @@ public function getAliasParams(): array
{
return $this->aliasParams;
}

/**
* Get is Alias
*
Expand Down
12 changes: 6 additions & 6 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ public function testMethod()
$this->assertEquals('GET', $this->route->getMethod());
}

public function testURL()
public function testPath()
{
$this->assertEquals('/', $this->route->getURL());
$this->assertEquals('/', $this->route->getPath());

$this->route->URL('/path');
$this->route->path('/path');

$this->assertEquals('/path', $this->route->getURL());
$this->assertEquals('/path', $this->route->getPath());
}

public function testAlias()
{
$this->assertEquals('', $this->route->getAliasURL());
$this->assertEquals('', $this->route->getAliasPath());
$this->assertEquals([], $this->route->getAliasParams());

$params = [
'pathId' => 'hello'
];
$this->route->alias('/path1',$params);

$this->assertEquals('/path1', $this->route->getAliasURL());
$this->assertEquals('/path1', $this->route->getAliasPath());
$this->assertEquals($params, $this->route->getAliasParams());
}

Expand Down

0 comments on commit f577522

Please sign in to comment.