mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-06 23:33:13 +08:00
Created middleware which injects default short code length from config when a value was not explicitly provided
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\Middleware\ShortUrl;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||
|
||||
class DefaultShortCodesLengthMiddleware implements MiddlewareInterface
|
||||
{
|
||||
private int $defaultShortCodesLength;
|
||||
|
||||
public function __construct(int $defaultShortCodesLength)
|
||||
{
|
||||
$this->defaultShortCodesLength = $defaultShortCodesLength;
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
if (! isset($body[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH])) {
|
||||
$body[ShortUrlMetaInputFilter::SHORT_CODE_LENGTH] = $this->defaultShortCodesLength;
|
||||
}
|
||||
|
||||
return $handler->handle($request->withParsedBody($body));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user