- Still WIP on Api's wrapper
This commit is contained in:
1 parent
7e69a09e7f
commit
5d9c0f0dbf
16 files changed
+548
-42
No files matched your search
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Api\Request;
|
||||
|
||||
use League\CommonMark\Block\Element\StringContainerInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use Ulmus\Api\Common\{MethodEnum, Message, Stream, Uri};
|
||||
|
||||
class Request extends Message implements RequestInterface
|
||||
{
|
||||
protected int $code;
|
||||
|
||||
protected string $message;
|
||||
|
||||
protected string $target;
|
||||
|
||||
protected UriInterface $uri;
|
||||
|
||||
protected MethodEnum $method;
|
||||
|
||||
public function __construct(string|UriInterface $uri, MethodEnum $method, null|StreamInterface $stream = null, array $headers = [])
|
||||
{
|
||||
$this->method = $method;
|
||||
$this->uri = Uri::from($uri);
|
||||
$this->stream = $stream ?? Stream::fromMemory();
|
||||
$this->setHeaders($headers );
|
||||
}
|
||||
|
||||
public function getRequestTarget(): string
|
||||
{
|
||||
if ( isset($this->target) ) {
|
||||
return $this->target;
|
||||
}
|
||||
|
||||
$query = $this->uri->getQuery() ?: null;
|
||||
|
||||
$target = $this->uri->getPath() . ( $query ? "?{$query}" : "" );
|
||||
|
||||
return $target ?: '/';
|
||||
}
|
||||
|
||||
public function withRequestTarget($requestTarget)
|
||||
{
|
||||
return (clone $this)->setRequestTarget($requestTarget);
|
||||
}
|
||||
|
||||
public function setRequestTarget(string $target) : static
|
||||
{
|
||||
$this->target = $target;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getMethod()
|
||||
{
|
||||
return $this->method->value;
|
||||
}
|
||||
|
||||
public function withMethod($method)
|
||||
{
|
||||
return (clone $this)->setMethod($method);
|
||||
}
|
||||
|
||||
protected function setMethod(string $method) : static
|
||||
{
|
||||
$this->method = MethodEnum::from($method);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUri()
|
||||
{
|
||||
return (string) $this->uri;
|
||||
}
|
||||
|
||||
public function withUri(UriInterface $uri, $preserveHost = false)
|
||||
{
|
||||
return (clone $this)->setUri($uri, $preserveHost);
|
||||
}
|
||||
|
||||
protected function setUri(UriInterface|string $uri, bool $preserveHost = false) : static
|
||||
{
|
||||
$this->uri = Uri::from($uri);
|
||||
|
||||
if ( ! $preserveHost || ! $this->hasHeader('Host') ) {
|
||||
$this->headers['Host'] = [ $this->renderHostHeader() ];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function renderHostHeader(UriInterface $fromUri = null) : string
|
||||
{
|
||||
$fromUri ??= $this->uri;
|
||||
|
||||
$port = $fromUri->getPort();
|
||||
|
||||
return implode('', [
|
||||
$fromUri->getHost(), $port && ! in_array($port, Uri::IGNORE_HTTP_PORT) ? ":{$port}" : ""
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user