Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Remove deprecated hints and cast code-style to PSR-12
Browse files Browse the repository at this point in the history
  • Loading branch information
evgsavosin committed Aug 28, 2022
1 parent 3869719 commit 54c7f4e
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 192 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use SimpleRouting\Router;
use SimpleRouting\Exception\HttpException;

// Create router instance
$router = new Router;
$router = new Router();

// Add route with GET method
$router->get('/foo', 'foo');
Expand Down
4 changes: 3 additions & 1 deletion src/Exception/BadRouteException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SimpleRouting\Exception;

Expand Down
4 changes: 3 additions & 1 deletion src/Exception/HttpException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SimpleRouting\Exception;

Expand Down
50 changes: 5 additions & 45 deletions src/Route.php
Original file line number Diff line number Diff line change
@@ -1,79 +1,39 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SimpleRouting;

class Route
{
/**
* HTTP Method (GET/PUT/POST/DELETE)
*
* @var string $httpMethod
*/
protected string $httpMethod;

/**
* @var string string $uri
*/
protected string $uri;

/**
* Handler as callback or function
*
* @var string|callable $handler
*/
protected $handler;

/**
* Regex rules
*
* @var array $regex
*/
protected array $regex = [];

/**
* Constructor
*
* @param string $httpMethod
* @param string $uri
* @param string|callable $handler
* @param array|null $regex
* @return void
*/
public function __construct(string $httpMethod, string $uri, $handler, ?array $regex = null)
public function __construct(string $httpMethod, string|callable $uri, $handler, ?array $regex = null)
{
$this->httpMethod = $httpMethod;
$this->uri = $uri;
$this->handler = $handler;
$this->regex = $regex;
}

/**
* @return string
*/
public function getHttpMethod(): string
{
return $this->httpMethod;
}

/**
* @return string
*/
public function getUri(): string
{
return $this->uri;
}

/**
* @return callable|string
*/
public function getHandler()
public function getHandler(): callable|string
{
return $this->handler;
}

/**
* @return array
*/
public function getRegex(): array
{
return $this->regex;
Expand Down
62 changes: 5 additions & 57 deletions src/RouteCollection.php
Original file line number Diff line number Diff line change
@@ -1,97 +1,45 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SimpleRouting;

class RouteCollection
{
/**
* @var array $route
*/
protected array $routes = [];

/**
* @var string $groupPrefix
*/
protected string $groupPrefix;

/**
* @var RouteParser $routeParser
*/
protected RouteParser $routeParser;

/**
* Constructor
*
* @return void
*/
public function __construct(?RouteParser $routeParser = null)
{
$this->groupPrefix = '';
$this->routeParser = $routeParser ?? new RouteParser;
$this->routeParser = $routeParser ?? new RouteParser();
}

/**
* Get route collection
*
* @return array
*/
public function getRoutes(): array
{
return $this->routes;
}

/**
* Set group prefix
*
* @param string $prefix
* @return void
*/
public function setGroupPrefix(string $prefix): void
{
$this->groupPrefix = $prefix;
}

/**
* Get group prefix
*
* @return string
*/
public function getGroupPrefix(): string
{
return $this->groupPrefix;
}

/**
* Add group prefix
*
* @param string $prefix
* @param callable $callback
* @return void
*/
public function addGroup(string $prefix, callable $callback): void
{
$oldGroupPrefix = $this->getGroupPrefix();

// Change prefix

$this->setGroupPrefix($oldGroupPrefix . $prefix);

// Call function
$callback($this);

// Return old prefix
$this->setGroupPrefix($oldGroupPrefix);
}

/**
* Adding route to collection
*
* @param string $httpMethod
* @param string $uri
* @param string|callable $handler
* @param array|null $regex
*
* @return void
*/
public function addRoute(string $httpMethod, string $uri, $handler, ?array $regex = null): void
{
$uri = $this->getGroupPrefix() . $uri;
Expand Down
22 changes: 3 additions & 19 deletions src/RouteDispatcher.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SimpleRouting;

class RouteDispatcher
{
/**
* @var array $routeCollection
*/
protected array $routes;

/**
* Constructor
*/
public function __construct(array $routes)
{
$this->routes = $routes;
}

/**
* @param string $httpMethod
* @param string $uri
*
* @return array|null
*/
public function handle(string $httpMethod, string $uri): ?array
{
$routes = $this->routes;
Expand Down Expand Up @@ -59,12 +49,6 @@ public function handle(string $httpMethod, string $uri): ?array
];
}

/**
* @param array|null $matches
* @param array|null $regex
*
* @return array
*/
private function processArgs(?array $matches, ?array $regex): array
{
$i = 1;
Expand Down
9 changes: 3 additions & 6 deletions src/RouteParser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SimpleRouting;

Expand All @@ -11,12 +13,7 @@ final class RouteParser
* Example:
* Uri: /test/{simple}, regex array ['simple' => ['[a-zA-Z]']], finalize - ~\/test\/[a-zA-Z]~
*
* @param string $uri
* @param array|null $regex
*
* @throws BadRouteException
*
* @return string
*/
public function make(string $uri, ?array &$regex = null): string
{
Expand Down
59 changes: 4 additions & 55 deletions src/Router.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,28 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace SimpleRouting;

use SimpleRouting\Exception\HttpException;

final class Router
{
/**
* @var RouteCollection $routeCollection
*/
protected RouteCollection $routeCollection;

/**
* Constructor
*/
public function __construct()
{
$routeParser = new RouteParser;
$routeParser = new RouteParser();
$this->routeCollection = new RouteCollection($routeParser);
}

/**
* Short function of adding group
*
* @param string $prefix
* @param callable $callback
*
* @return void
*/
public function group(string $prefix, callable $callback): void
{
$this->routeCollection->addGroup($prefix, $callback);
}

/**
* Adding route with multiple HTTP methods to collection
*
* @param array $httpMethods
* @param string $uri
* @param string|callable $handler
* @param array|null $regex
*
* @return void
*/
public function map(array $httpMethods, string $uri, $handler, ?array $regex = null): void
{
Expand All @@ -52,13 +33,6 @@ public function map(array $httpMethods, string $uri, $handler, ?array $regex = n

/**
* Adding route as GET to collection
*
* @param array $httpMethods
* @param string $uri
* @param string|callable $handler
* @param array|null $regex
*
* @return void
*/
public function get(string $uri, $handler, ?array $regex = null): void
{
Expand All @@ -67,13 +41,6 @@ public function get(string $uri, $handler, ?array $regex = null): void

/**
* Adding route as POST to collection
*
* @param array $httpMethods
* @param string $uri
* @param string|callable $handler
* @param array|null $regex
*
* @return void
*/
public function post(string $uri, $handler, ?array $regex = null): void
{
Expand All @@ -82,13 +49,6 @@ public function post(string $uri, $handler, ?array $regex = null): void

/**
* Adding route as DELETE to collection
*
* @param array $httpMethods
* @param string $uri
* @param string|callable $handler
* @param array|null $regex
*
* @return void
*/
public function delete(string $uri, $handler, ?array $regex = null): void
{
Expand All @@ -97,13 +57,6 @@ public function delete(string $uri, $handler, ?array $regex = null): void

/**
* Adding route as PUT to collection
*
* @param array $httpMethods
* @param string $uri
* @param string|callable $handler
* @param array|null $regex
*
* @return void
*/
public function put(string $uri, $handler, ?array $regex = null): void
{
Expand All @@ -113,10 +66,6 @@ public function put(string $uri, $handler, ?array $regex = null): void
/**
* @param string $httpMethod
* @param string $uri
*
* @throws HttpException
*
* @return array
*/
public function dispatch(?string $httpMethod = null, ?string $uri = null): array
{
Expand Down
Loading

0 comments on commit 54c7f4e

Please sign in to comment.