46 lines
1.2 KiB
PHP
46 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace DigiComp\FlowTranslationEndpoint\Http;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
class TransformTranslationRequestMiddleware implements MiddlewareInterface
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected string $headerName;
|
|
|
|
/**
|
|
* @var string|null
|
|
*/
|
|
protected ?string $reactOnPath;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected string $translateGetParam;
|
|
|
|
public function __construct(string $headerName, ?string $reactOnPath, string $translateGetParam)
|
|
{
|
|
$this->headerName = $headerName;
|
|
$this->reactOnPath = $reactOnPath;
|
|
$this->translateGetParam = $translateGetParam;
|
|
}
|
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
{
|
|
if ($this->reactOnPath !== null && $request->getUri()->getPath() === $this->reactOnPath) {
|
|
$request = $request->withAddedHeader(
|
|
$this->headerName,
|
|
$request->getQueryParams()[$this->translateGetParam] ?? ''
|
|
);
|
|
}
|
|
return $handler->handle($request);
|
|
}
|
|
}
|