Files
kiri-router/src/Constrict/Message.php
T

267 lines
7.6 KiB
PHP
Raw Normal View History

2023-04-15 23:29:27 +08:00
<?php
2023-04-15 23:31:16 +08:00
namespace Kiri\Router\Constrict;
2023-04-15 23:29:27 +08:00
2023-04-15 23:31:16 +08:00
use InvalidArgumentException;
2023-04-15 23:29:27 +08:00
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;
class Message implements MessageInterface
{
2023-04-15 23:31:16 +08:00
/**
* @var string
*/
private string $version = '1.1';
/**
* @var array
*/
private array $headers = [];
/**
* @var StreamInterface
*/
private StreamInterface $stream;
2023-04-15 23:29:27 +08:00
/**
* Retrieves the HTTP protocol version as a string.
*
* The string MUST contain only the HTTP version number (e.g., "1.1", "1.0").
*
* @return string HTTP protocol version.
*/
2023-04-15 23:31:16 +08:00
public function getProtocolVersion(): string
2023-04-15 23:29:27 +08:00
{
// TODO: Implement getProtocolVersion() method.
2023-04-15 23:31:16 +08:00
return $this->version;
2023-04-15 23:29:27 +08:00
}
/**
* Return an instance with the specified HTTP protocol version.
*
* The version string MUST contain only the HTTP version number (e.g.,
* "1.1", "1.0").
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* new protocol version.
*
* @param string $version HTTP protocol version
* @return static
*/
2023-04-15 23:31:16 +08:00
public function withProtocolVersion(string $version): static
2023-04-15 23:29:27 +08:00
{
// TODO: Implement withProtocolVersion() method.
2023-04-15 23:31:16 +08:00
$this->version = $version;
return $this;
2023-04-15 23:29:27 +08:00
}
/**
* Retrieves all message header values.
*
* The keys represent the header name as it will be sent over the wire, and
* each value is an array of strings associated with the header.
*
* // Represent the headers as a string
* foreach ($message->getHeaders() as $name => $values) {
* echo $name . ": " . implode(", ", $values);
* }
*
* // Emit headers iteratively:
* foreach ($message->getHeaders() as $name => $values) {
* foreach ($values as $value) {
* header(sprintf('%s: %s', $name, $value), false);
* }
* }
*
* While header names are not case-sensitive, getHeaders() will preserve the
* exact case in which headers were originally specified.
*
* @return string[][] Returns an associative array of the message's headers. Each
* key MUST be a header name, and each value MUST be an array of strings
* for that header.
*/
2023-04-15 23:31:16 +08:00
public function getHeaders(): array
2023-04-15 23:29:27 +08:00
{
// TODO: Implement getHeaders() method.
2023-04-15 23:31:16 +08:00
return $this->headers;
2023-04-15 23:29:27 +08:00
}
/**
* Checks if a header exists by the given case-insensitive name.
*
* @param string $name Case-insensitive header field name.
* @return bool Returns true if any header names match the given header
* name using a case-insensitive string comparison. Returns false if
* no matching header name is found in the message.
*/
2023-04-15 23:31:16 +08:00
public function hasHeader(string $name): bool
2023-04-15 23:29:27 +08:00
{
// TODO: Implement hasHeader() method.
2023-04-15 23:31:16 +08:00
return isset($this->headers[$name]) && $this->headers[$name] !== null;
2023-04-15 23:29:27 +08:00
}
/**
* Retrieves a message header value by the given case-insensitive name.
*
* This method returns an array of all the header values of the given
* case-insensitive header name.
*
* If the header does not appear in the message, this method MUST return an
* empty array.
*
* @param string $name Case-insensitive header field name.
* @return string[] An array of string values as provided for the given
* header. If the header does not appear in the message, this method MUST
* return an empty array.
*/
2023-04-15 23:31:16 +08:00
public function getHeader(string $name): array
2023-04-15 23:29:27 +08:00
{
// TODO: Implement getHeader() method.
2023-04-15 23:31:16 +08:00
if (isset($this->headers[$name])) {
$header = $this->headers[$name];
return is_string($header) ? [$header] : $header;
}
return [];
2023-04-15 23:29:27 +08:00
}
/**
* Retrieves a comma-separated string of the values for a single header.
*
* This method returns all of the header values of the given
* case-insensitive header name as a string concatenated together using
* a comma.
*
* NOTE: Not all header values may be appropriately represented using
* comma concatenation. For such headers, use getHeader() instead
* and supply your own delimiter when concatenating.
*
* If the header does not appear in the message, this method MUST return
* an empty string.
*
* @param string $name Case-insensitive header field name.
* @return string A string of values as provided for the given header
* concatenated together using a comma. If the header does not appear in
* the message, this method MUST return an empty string.
*/
2023-04-15 23:31:16 +08:00
public function getHeaderLine(string $name): string
2023-04-15 23:29:27 +08:00
{
// TODO: Implement getHeaderLine() method.
2023-04-15 23:31:16 +08:00
return implode(';', $this->getHeader($name));
}
/**
* @param string $name
* @param string|null $default
* @return string|null
*/
public function header(string $name, ?string $default = null): ?string
{
if (!$this->hasHeader($name)) {
return $default;
}
return $this->getHeaderLine($name);
2023-04-15 23:29:27 +08:00
}
/**
* Return an instance with the provided value replacing the specified header.
*
* While header names are case-insensitive, the casing of the header will
* be preserved by this function, and returned from getHeaders().
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* new and/or updated header and value.
*
* @param string $name Case-insensitive header field name.
* @param string|string[] $value Header value(s).
* @return static
2023-04-15 23:31:16 +08:00
* @throws InvalidArgumentException for invalid header names or values.
2023-04-15 23:29:27 +08:00
*/
2023-04-15 23:31:16 +08:00
public function withHeader(string $name, $value): static
2023-04-15 23:29:27 +08:00
{
2023-04-15 23:31:16 +08:00
// TODO: Implement withAddedHeader() method.
$this->headers[$name] = $value;
return $this;
2023-04-15 23:29:27 +08:00
}
/**
* Return an instance with the specified header appended with the given value.
*
* Existing values for the specified header will be maintained. The new
* value(s) will be appended to the existing list. If the header did not
* exist previously, it will be added.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that has the
* new header and/or value.
*
* @param string $name Case-insensitive header field name to add.
* @param string|string[] $value Header value(s).
* @return static
2023-04-15 23:31:16 +08:00
* @throws InvalidArgumentException for invalid header names or values.
2023-04-15 23:29:27 +08:00
*/
2023-04-15 23:31:16 +08:00
public function withAddedHeader(string $name, $value): static
2023-04-15 23:29:27 +08:00
{
// TODO: Implement withAddedHeader() method.
2023-04-15 23:31:16 +08:00
$this->headers[$name] = $value;
return $this;
2023-04-15 23:29:27 +08:00
}
/**
* Return an instance without the specified header.
*
* Header resolution MUST be done without case-sensitivity.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return an instance that removes
* the named header.
*
* @param string $name Case-insensitive header field name to remove.
* @return static
*/
2023-04-15 23:31:16 +08:00
public function withoutHeader(string $name): static
2023-04-15 23:29:27 +08:00
{
// TODO: Implement withoutHeader() method.
2023-04-15 23:31:16 +08:00
unset($this->headers[$name]);
return $this;
2023-04-15 23:29:27 +08:00
}
/**
* Gets the body of the message.
*
* @return StreamInterface Returns the body as a stream.
*/
2023-04-15 23:31:16 +08:00
public function getBody(): StreamInterface
2023-04-15 23:29:27 +08:00
{
// TODO: Implement getBody() method.
2023-04-15 23:31:16 +08:00
return $this->stream;
2023-04-15 23:29:27 +08:00
}
/**
* Return an instance with the specified message body.
*
* The body MUST be a StreamInterface object.
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return a new instance that has the
* new body stream.
*
* @param StreamInterface $body Body.
* @return static
2023-04-15 23:31:16 +08:00
* @throws InvalidArgumentException When the body is not valid.
2023-04-15 23:29:27 +08:00
*/
2023-04-15 23:31:16 +08:00
public function withBody(StreamInterface $body): static
2023-04-15 23:29:27 +08:00
{
// TODO: Implement withBody() method.
2023-04-15 23:31:16 +08:00
$this->stream = $body;
return $this;
2023-04-15 23:29:27 +08:00
}
}