-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* It's free open-source software released under the MIT License. | ||
* | ||
* @author Anatoly Nekhay <[email protected]> | ||
* @copyright Copyright (c) 2018, Anatoly Nekhay | ||
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE | ||
* @link https://github.com/sunrise-php/http-router | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sunrise\Http\Router\Annotation; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* @since 3.0.0 | ||
*/ | ||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] | ||
class Path | ||
{ | ||
public function __construct( | ||
public readonly string $value, | ||
) { | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* It's free open-source software released under the MIT License. | ||
* | ||
* @author Anatoly Nekhay <[email protected]> | ||
* @copyright Copyright (c) 2018, Anatoly Nekhay | ||
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE | ||
* @link https://github.com/sunrise-php/http-router | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sunrise\Http\Router\Annotation; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* @since 3.0.0 | ||
*/ | ||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] | ||
final class PathHome extends Path | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('/'); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/** | ||
* It's free open-source software released under the MIT License. | ||
* | ||
* @author Anatoly Nekhay <[email protected]> | ||
* @copyright Copyright (c) 2018, Anatoly Nekhay | ||
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE | ||
* @link https://github.com/sunrise-php/http-router | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sunrise\Http\Router\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
use function array_walk_recursive; | ||
use function is_array; | ||
use function is_string; | ||
use function trim; | ||
|
||
/** | ||
* @since 3.0.0 | ||
*/ | ||
final class WhitespaceTrimmingMiddleware implements MiddlewareInterface | ||
{ | ||
/** | ||
* @var callable | ||
* | ||
* @readonly | ||
*/ | ||
private $walker; | ||
|
||
/** | ||
* @param null|callable(string):string $trimmer | ||
*/ | ||
public function __construct(?callable $trimmer) | ||
{ | ||
$trimmer ??= trim(...); | ||
|
||
$this->walker = static function (mixed &$value) use ($trimmer): void { | ||
if (is_string($value)) { | ||
$value = $trimmer($value); | ||
} | ||
}; | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$parsedBody = $request->getParsedBody(); | ||
if ($parsedBody !== [] && is_array($parsedBody)) { | ||
array_walk_recursive($parsedBody, $this->walker); | ||
$request = $request->withParsedBody($parsedBody); | ||
} | ||
|
||
$queryParams = $request->getQueryParams(); | ||
if ($queryParams !== []) { | ||
array_walk_recursive($queryParams, $this->walker); | ||
$request = $request->withQueryParams($queryParams); | ||
} | ||
|
||
return $handler->handle($request); | ||
} | ||
} |