1 | <?php |
||
2 | |||
3 | /** |
||
4 | * It's free open-source software released under the MIT License. |
||
5 | * |
||
6 | * @author Anatoly Nekhay <[email protected]> |
||
7 | * @copyright Copyright (c) 2018, Anatoly Nekhay |
||
8 | * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
||
9 | * @link https://github.com/sunrise-php/http-router |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Sunrise\Http\Router\Middleware; |
||
15 | |||
16 | use Closure; |
||
17 | use Psr\Http\Message\ResponseInterface; |
||
18 | use Psr\Http\Message\ServerRequestInterface; |
||
19 | use Psr\Http\Server\MiddlewareInterface; |
||
20 | use Psr\Http\Server\RequestHandlerInterface; |
||
21 | |||
22 | use function array_walk_recursive; |
||
23 | use function function_exists; |
||
24 | use function is_array; |
||
25 | use function is_string; |
||
26 | use function mb_trim; |
||
27 | use function trim; |
||
28 | |||
29 | /** |
||
30 | * @since 3.0.0 |
||
31 | */ |
||
32 | final class StringTrimmingMiddleware implements MiddlewareInterface |
||
33 | { |
||
34 | /** |
||
35 | * @var Closure(string): string |
||
36 | */ |
||
37 | private readonly Closure $trimmer; |
||
38 | |||
39 | /** |
||
40 | * @param (Closure(string): string)|null $trimmer |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
41 | */ |
||
42 | 4 | public function __construct(?Closure $trimmer = null) |
|
43 | { |
||
44 | 4 | $this->trimmer = $trimmer ?? (function_exists('mb_trim') ? mb_trim(...) : trim(...)); |
|
0 ignored issues
–
show
The type
trim was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() The type
mb_trim was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @inheritDoc |
||
49 | */ |
||
50 | 4 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
51 | { |
||
52 | 4 | $queryParams = $request->getQueryParams(); |
|
53 | 4 | if ($queryParams !== []) { |
|
54 | 2 | $request = $request->withQueryParams( |
|
55 | 2 | self::trimArray($queryParams, $this->trimmer) |
|
56 | 2 | ); |
|
57 | } |
||
58 | |||
59 | 4 | $parsedBody = $request->getParsedBody(); |
|
60 | 4 | if ($parsedBody !== [] && is_array($parsedBody)) { |
|
61 | 2 | $request = $request->withParsedBody( |
|
62 | 2 | self::trimArray($parsedBody, $this->trimmer) |
|
63 | 2 | ); |
|
64 | } |
||
65 | |||
66 | 4 | return $handler->handle($request); |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param array<array-key, mixed> $array |
||
0 ignored issues
–
show
|
|||
71 | * @param Closure(string): string $trimmer |
||
72 | * |
||
73 | * @return array<array-key, mixed> |
||
0 ignored issues
–
show
|
|||
74 | */ |
||
75 | 2 | private static function trimArray(array $array, Closure $trimmer): array |
|
76 | { |
||
77 | 2 | $walker = static function (mixed &$value) use ($trimmer): void { |
|
78 | 2 | if (is_string($value)) { |
|
79 | 2 | $value = $trimmer($value); |
|
80 | } |
||
81 | 2 | }; |
|
82 | |||
83 | 2 | array_walk_recursive($array, $walker); |
|
84 | |||
85 | 2 | return $array; |
|
86 | } |
||
87 | } |
||
88 |