DigiComp.FlowTranslationEnd.../Classes/Http/ReplaceGetTranslationRequest.php

52 lines
1.4 KiB
PHP
Raw Normal View History

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;
class ReplaceGetTranslationRequest implements MiddlewareInterface
{
/**
* @var string
*/
protected string $headerName;
/**
* @var string|null
*/
protected ?string $reactOnPath;
/**
* @var string
*/
protected string $translateGetParam;
/**
* @param string $headerName
* @param string|null $reactOnPath
* @param 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);
}
}