2023-08-05 15:49:46 +02:00
|
|
|
<?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;
|
|
|
|
|
2023-08-08 12:11:46 +02:00
|
|
|
class TransformTranslationRequestMiddleware implements MiddlewareInterface
|
2023-08-05 15:49:46 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
}
|