Skip to content

Commit

Permalink
Merge pull request #9 from marussoft/fix-placeholders-types
Browse files Browse the repository at this point in the history
Fix placeholders types
  • Loading branch information
ifedko committed Nov 20, 2019
2 parents 5e16b7c + f4bab42 commit d7c01bf
Show file tree
Hide file tree
Showing 18 changed files with 365 additions and 165 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: php

php:
- "7.0"
- "7.1"
- "7.2"
- "7.3"

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Router

![](https://travis-ci.org/marussoft/router.svg?branch=master)
2 changes: 1 addition & 1 deletion phpunit
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1 +1 @@
./vendor/bin/phpunit $*
php ./vendor/bin/phpunit $*
28 changes: 22 additions & 6 deletions src/AbstractRouteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Marussia\Router\Exceptions\PlaceholdersForPatternNotFoundException;
use Marussia\Router\Exceptions\HandlerIsNotSetException;
use Marussia\Router\Exceptions\ActionIsNotSetException;
use Marussia\Router\Contracts\RequestInterface;

abstract class AbstractRouteHandler
{
Expand All @@ -17,6 +16,23 @@ abstract class AbstractRouteHandler

protected $fillable = [];

protected $attributeTypes = [
'STRING' => '([a-z0-9\-]+)',
'INTEGER' => '([0-9]+)',
'ARRAY' => '([a-z0-9]+)/(([a-z0-9\-]+/)+|([a-z0-9\-_]+)+)($)',
];

const PLACEHOLDER_TYPE_STRING = 'STRING';

const PLACEHOLDER_TYPE_INTEGER = 'INTEGER';

const PLACEHOLDER_TYPE_ARRAY = 'ARRAY';

public function __construct(Request $request)
{
$this->request = $request;
}

public function route(string $method, string $pattern) : self
{
if (!is_null($this->matched)) {
Expand Down Expand Up @@ -84,14 +100,14 @@ public function isMatched() : bool
return true;
}

public function setRequest(RequestInterface $request)
public function getPlaceholderRegExp(string $type) : string
{
$this->request = $request;
return $this->attributeTypes[strtoupper($type)];
}
public function __call(string $name , array $arguments = []) : self

public function hasPlaceholderType(string $type) : bool
{
return $this;
return array_key_exists(strtoupper($type), $this->attributeTypes);
}

protected function checkErrors() : void
Expand Down
14 changes: 0 additions & 14 deletions src/Contracts/RequestInterface.php

This file was deleted.

13 changes: 13 additions & 0 deletions src/Exceptions/RouteFileAliasIsNotStringException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Marussia\Router\Exceptions;

class RouteFileAliasIsNotStringException extends \Exception
{
public function __construct(string $aliasFor, string $type)
{
parent::__construct('Routes file alias for ' . $aliasFor . ' will be type string. Type ' . $type . ' given.');
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/RouterIsNotInitializedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Marussia\Router\Exceptions;

class RouterIsNotInitializedException extends \Exception
{
public function __construct()
{
parent::__construct('Router is not initialized');
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/RoutesDirPathIsNotSetException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Marussia\Router\Exceptions;

class RoutesDirPathIsNotSetException extends \Exception
{
public function __construct()
{
parent::__construct('Routes directory path is not set');
}
}
4 changes: 4 additions & 0 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public function match() : void
// @todo добавить проверку на существование плейсхолдера с выбросом исключения
if (!empty($this->fillable['where'])) {
foreach($this->fillable['where'] as $key => $condition) {
if ($this->hasPlaceholderType($condition)) {
$pattern = str_replace('{$' . $key . '}', $this->getPlaceholderRegExp($condition), $pattern);
continue;
}
$pattern = str_replace('{$' . $key . '}', $condition, $pattern);
}
}
Expand Down
24 changes: 11 additions & 13 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@

namespace Marussia\Router;

use Marussia\Router\Contracts\RequestInterface;

class Request implements RequestInterface
class Request
{
private $method;

private $uri = '/';

public function __construct(string $uri, string $method, string $host, string $protocol)
{
$this->method = strtoupper($method);

$this->host = $host;

$this->protocol = $protocol;

if (!empty($uri) && $uri !== '/') {
$this->uri = preg_replace('(\?.*$)', '', trim($uri, '/'));
}
Expand All @@ -29,27 +27,27 @@ public function getUri() : string
{
return $this->uri;
}
public function setUri(string $uri)

public function replaceUri(string $uri)
{
$this->uri = $uri;
}

public function getMethod() : string
{
return $this->method;
}

public function isMethod(string $method) : bool
{
return $this->method === strtoupper($method);
}

public function getHost() : string
{
return $this->host;
}

public function getProtocol() : string
{
return $this->protocol;
Expand Down
Loading

0 comments on commit d7c01bf

Please sign in to comment.