Skip to content
Permalink
Browse files

Route ns

  • Loading branch information
rodolfoberrios committed Mar 6, 2020
1 parent 4dd643d commit c74314404ac135f3f4e1186ff6dfb5b8b89632bb
@@ -18,7 +18,7 @@
use Chevere\Components\Http\Methods\GetMethod;
use Chevere\Components\Route\RoutePath;
use Chevere\Components\Route\RouteName;
use Chevere\Components\Route\Wildcard;
use Chevere\Components\Route\RouteWildcard;

// return [
// (new Route(new RouteName('test'), new PathUri('/test')))
@@ -21,7 +21,7 @@
use Chevere\Components\Message\Message;
use Chevere\Components\Route\Exceptions\EndpointException;
use Chevere\Components\Route\Interfaces\EndpointInterface;
use Chevere\Components\Route\Interfaces\WildcardCollectionInterface;
use Chevere\Components\Route\Interfaces\RouteWildcardsInterface;
use Chevere\Components\Str\Str;
use ReflectionClass;

@@ -33,7 +33,7 @@ abstract class Endpoint implements EndpointInterface

private DirInterface $root;

private WildcardCollectionInterface $wildcardCollection;
private RouteWildcardsInterface $routeWildcards;

abstract public function getController(): ControllerInterface;

@@ -54,7 +54,7 @@ abstract class Endpoint implements EndpointInterface
}
$this->handleSetPath();
$this->method = new $method;
$this->wildcardCollection = new WildcardCollection();
$this->routeWildcards = new RouteWildcards();
}

final public function whereIs(): string
@@ -72,9 +72,9 @@ abstract class Endpoint implements EndpointInterface
return $this->method;
}

public function wildcardCollection(): WildcardCollectionInterface
public function routeWildcards(): RouteWildcardsInterface
{
return $this->wildcardCollection;
return $this->routeWildcards;
}

final public function withRootDir(DirInterface $root): EndpointInterface
@@ -16,7 +16,7 @@
use Exception;

/**
* Exception thrown when a wildcard is repeated like in a WildcardCollectionInterface or in a PathUriWildcardsInterface.
* Exception thrown when a wildcard is duplicated.
*/
final class WildcardRepeatException extends Exception
{
@@ -20,7 +20,7 @@ interface RoutePathInterface extends ToStringInterface
const REGEX_DELIMITER_CHAR = '/';

/** Regex pattern used to catch {wildcard} */
const REGEX_WILDCARD_SEARCH = self::REGEX_DELIMITER_CHAR . '{' . WildcardInterface::ACCEPT_CHARS . '}' . self::REGEX_DELIMITER_CHAR . 'i';
const REGEX_WILDCARD_SEARCH = self::REGEX_DELIMITER_CHAR . '{' . RouteWildcardInterface::ACCEPT_CHARS . '}' . self::REGEX_DELIMITER_CHAR . 'i';

public function __construct(string $path);

@@ -41,22 +41,22 @@ public function key(): string;
public function regex(): string;

/**
* Returns a boolean indicating whether the instance has a WildcardCollectionInterface.
* Returns a boolean indicating whether the instance has a RouteWildcardsInterface.
*/
public function hasWildcardCollection(): bool;
public function hasRouteWildcards(): bool;

/**
* Provides access to the WildcardCollectionInterface instance.
* Provides access to the RouteWildcardsInterface instance.
*/
public function wildcardCollection(): WildcardCollectionInterface;
public function routeWildcards(): RouteWildcardsInterface;

/**
* Return an instance with the specified WildcardInterface.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified WildcardInterface.
*/
public function withWildcard(WildcardInterface $wildcard): RoutePathInterface;
public function withWildcard(RouteWildcardInterface $wildcard): RoutePathInterface;

/**
* Provides an array matching wildcards for the given request uri.
@@ -16,7 +16,7 @@
use Chevere\Components\Route\Exceptions\WildcardNotFoundException;
use Chevere\Components\Route\Exceptions\WildcardInvalidRegexException;

interface WildcardInterface
interface RouteWildcardInterface
{
/** Regex pattern used by default (no explicit where). */
const REGEX_MATCH_DEFAULT = '[A-z0-9\\_\\-\\%]+';
@@ -34,7 +34,7 @@ public function __construct(string $name);
*
* @throws WildcardInvalidRegexException if $match is an invalid regex match
*/
public function withMatch(WildcardMatchInterface $regexMatch): WildcardInterface;
public function withMatch(RouteWildcardMatchInterface $regexMatch): RouteWildcardInterface;

/**
* Provides access to the name.
@@ -44,7 +44,7 @@ public function name(): string;
/**
* Provides access to the WildcardMatchInterface instance.
*/
public function match(): WildcardMatchInterface;
public function match(): RouteWildcardMatchInterface;

/**
* Asserts that a given RoutePathInterface contains the wildcard.
@@ -15,12 +15,16 @@

use Chevere\Components\Common\Interfaces\ToStringInterface;

interface WildcardMatchInterface extends ToStringInterface
interface RouteWildcardMatchInterface extends ToStringInterface
{
/**
* @param string Regex match (without delimiters).
* @throws RegexException if $match is an invalid regex matcher
*/
public function __construct(string $match);

/**
* @return string Match.
* @return string Regex match (without delimiters).
*/
public function toString(): string;
}
@@ -18,17 +18,17 @@
/**
* @method public array toArray() Provides access to the collection array.
*/
interface WildcardCollectionInterface extends ToArrayInterface
interface RouteWildcardsInterface extends ToArrayInterface
{
public function __construct(WildcardInterface ...$wildcards);
public function __construct(RouteWildcardInterface ...$wildcards);

/**
* Return an instance with the specified WildcardInterface.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified WildcardInterface.
*/
public function withAddedWildcard(WildcardInterface $wildcard): WildcardCollectionInterface;
public function withAddedWildcard(RouteWildcardInterface $wildcard): RouteWildcardsInterface;

/**
* Returns a boolean indicating whether the instance has any WildcardInterface.
@@ -38,12 +38,12 @@ public function hasAny(): bool;
/**
* Returns a boolean indicating whether the instance has a given WildcardInterface.
*/
public function has(WildcardInterface $wildcard): bool;
public function has(RouteWildcardInterface $wildcard): bool;

/**
* Provides access to the target WildcardInterface instance.
*/
public function get(WildcardInterface $wildcard): WildcardInterface;
public function get(RouteWildcardInterface $wildcard): RouteWildcardInterface;

/**
* Returns a boolean indicating whether the instance has WildcardInterface in the given pos.
@@ -53,5 +53,5 @@ public function hasPos(int $pos): bool;
/**
* Provides access to the target WildcardInterface instance in the given pos.
*/
public function getPos(int $pos): WildcardInterface;
public function getPos(int $pos): RouteWildcardInterface;
}
@@ -13,20 +13,15 @@

namespace Chevere\Components\Route;

use Chevere\Components\Controller\ControllerName;
use Chevere\Components\Controller\Interfaces\ControllerInterface;
use Chevere\Components\Controller\Interfaces\ControllerNameInterface;
use Chevere\Components\Http\Exceptions\MethodNotFoundException;
use Chevere\Components\Http\Interfaces\MethodControllerNameCollectionInterface;
use Chevere\Components\Http\Interfaces\MethodControllerNameInterface;
use Chevere\Components\Http\Interfaces\MethodInterface;
use Chevere\Components\Http\MethodControllerName;
use Chevere\Components\Http\MethodControllerNameCollection;
use Chevere\Components\Message\Message;
use Chevere\Components\Middleware\Interfaces\MiddlewareInterface;
use Chevere\Components\Middleware\Interfaces\MiddlewareNameCollectionInterface;
use Chevere\Components\Middleware\Interfaces\MiddlewareNameInterface;
use Chevere\Components\Middleware\MiddlewareName;
use Chevere\Components\Middleware\MiddlewareNameCollection;
use Chevere\Components\Route\Interfaces\RoutePathInterface;
use Chevere\Components\Route\Interfaces\RouteInterface;
@@ -56,6 +51,11 @@ public function __construct(RouteNameInterface $name, RoutePathInterface $routeP
$this->middlewareNameCollection = new MiddlewareNameCollection();
}

public function name(): RouteNameInterface
{
return $this->name;
}

public function path(): RoutePathInterface
{
return $this->routePath;
@@ -66,11 +66,6 @@ public function maker(): array
return $this->maker;
}

public function name(): RouteNameInterface
{
return $this->name;
}

public function withAddedMethodControllerName(MethodControllerNameInterface $methodControllerName): RouteInterface
{
$new = clone $this;
@@ -23,8 +23,8 @@
use Chevere\Components\Route\Exceptions\WildcardRepeatException;
use Chevere\Components\Route\Exceptions\WildcardReservedException;
use Chevere\Components\Route\Interfaces\RoutePathInterface;
use Chevere\Components\Route\Interfaces\WildcardCollectionInterface;
use Chevere\Components\Route\Interfaces\WildcardInterface;
use Chevere\Components\Route\Interfaces\RouteWildcardsInterface;
use Chevere\Components\Route\Interfaces\RouteWildcardInterface;
use Chevere\Components\Str\Str;
use Chevere\Components\Str\StrBool;
use LogicException;
@@ -45,7 +45,7 @@ final class RoutePath implements RoutePathInterface

private array $wildcardsMatch;

private WildcardCollectionInterface $wildcardCollection;
private RouteWildcardsInterface $routeWildcards;

/** @var array string[] */
private array $wildcards;
@@ -77,7 +77,7 @@ public function __construct(string $path)
$this->assertReservedWildcards();
$this->assertMatchingWildcards();
$this->handleWildcards();
$this->handleSetWildcardCollection();
$this->handleSetRouteWildcards();
}
$this->handleSetRegex();
}
@@ -97,14 +97,14 @@ public function regex(): string
return $this->regex;
}

public function hasWildcardCollection(): bool
public function hasRouteWildcards(): bool
{
return isset($this->wildcardCollection);
return isset($this->routeWildcards);
}

public function wildcardCollection(): WildcardCollectionInterface
public function routeWildcards(): RouteWildcardsInterface
{
return $this->wildcardCollection;
return $this->routeWildcards;
}

/**
@@ -115,12 +115,12 @@ public function wildcardCollection(): WildcardCollectionInterface
*
* @throws WildcardNotFoundException if the wildcard doesn't exists in the instance
*/
public function withWildcard(WildcardInterface $wildcard): RoutePathInterface
public function withWildcard(RouteWildcardInterface $routeWildcard): RoutePathInterface
{
$new = clone $this;
$wildcard->assertRoutePath($new);
$new->wildcardCollection = $new->wildcardCollection
->withAddedWildcard($wildcard);
$routeWildcard->assertRoutePath($new);
$new->routeWildcards = $new->routeWildcards
->withAddedWildcard($routeWildcard);
$new->handleSetRegex();

return $new;
@@ -292,21 +292,21 @@ private function hasHandlebars(): bool
return false !== strpos($this->path, '{') || false !== strpos($this->path, '}');
}

private function handleSetWildcardCollection(): void
private function handleSetRouteWildcards(): void
{
$this->wildcardCollection = new WildcardCollection();
$this->routeWildcards = new RouteWildcards();
foreach ($this->wildcards as $wildcardName) {
$this->wildcardCollection = $this->wildcardCollection
->withAddedWildcard(new Wildcard($wildcardName));
$this->routeWildcards = $this->routeWildcards
->withAddedWildcard(new RouteWildcard($wildcardName));
}
}

private function handleSetRegex(): void
{
$pseudoRegex = str_replace('/', '\/', $this->key);
$regex = self::REGEX_DELIMITER_CHAR . '^' . $pseudoRegex . '$' . self::REGEX_DELIMITER_CHAR;
if (isset($this->wildcardCollection)) {
foreach ($this->wildcardCollection->toArray() as $pos => $wildcard) {
if (isset($this->routeWildcards)) {
foreach ($this->routeWildcards->toArray() as $pos => $wildcard) {
$regex = str_replace(
"{{$pos}}",
'(' . $wildcard->match()->toString() . ')',
@@ -18,20 +18,20 @@
use Chevere\Components\Message\Message;
use Chevere\Components\Route\Exceptions\WildcardNotFoundException;
use Chevere\Components\Route\Exceptions\WildcardStartWithNumberException;
use Chevere\Components\Route\Interfaces\WildcardMatchInterface;
use Chevere\Components\Route\Interfaces\RouteWildcardMatchInterface;
use Chevere\Components\Route\Interfaces\RoutePathInterface;
use Chevere\Components\Route\Interfaces\WildcardInterface;
use Chevere\Components\Route\Interfaces\RouteWildcardInterface;
use Chevere\Components\Str\StrAssert;

final class Wildcard implements WildcardInterface
final class RouteWildcard implements RouteWildcardInterface
{
/** @var string */
private string $name;

/** @var string */
private string $string;

private WildcardMatchInterface $match;
private RouteWildcardMatchInterface $match;

/**
* Creates a new instance.
@@ -46,13 +46,13 @@ public function __construct(string $name)
$this->name = $name;
$this->string = "{{$this->name}}";
$this->assertName();
$this->match = new WildcardMatch(WildcardInterface::REGEX_MATCH_DEFAULT);
$this->match = new RouteWildcardMatch(RouteWildcardInterface::REGEX_MATCH_DEFAULT);
}

public function withMatch(WildcardMatchInterface $regexMatch): WildcardInterface
public function withMatch(RouteWildcardMatchInterface $wildcardMatch): RouteWildcardInterface
{
$new = clone $this;
$new->match = $regexMatch;
$new->match = $wildcardMatch;

return $new;
}
@@ -67,7 +67,7 @@ public function toString(): string
return $this->string;
}

public function match(): WildcardMatchInterface
public function match(): RouteWildcardMatchInterface
{
return $this->match;
}
@@ -96,7 +96,7 @@ private function assertName(): void
->toString()
);
}
if (!preg_match(WildcardInterface::ACCEPT_CHARS_REGEX, $this->name)) {
if (!preg_match(RouteWildcardInterface::ACCEPT_CHARS_REGEX, $this->name)) {
throw new WildcardInvalidCharsException(
(new Message('String %string% must contain only alphanumeric and underscore characters'))
->code('%string%', $this->name)

0 comments on commit c743144

Please sign in to comment.
You can’t perform that action at this time.