From 99db0c06adc62042a83e3e2065df845d75c26f53 Mon Sep 17 00:00:00 2001 From: BMTmohammedtaha Date: Sat, 6 Jan 2024 12:45:06 -0800 Subject: [PATCH] add setter for each var in Message/Request/Response --- src/MessageTrait.php | 89 +++++++++++++++++++++++++++++++++++++++++--- src/Request.php | 40 ++++++++++++++++++++ src/Response.php | 24 ++++++++++++ 3 files changed, 148 insertions(+), 5 deletions(-) diff --git a/src/MessageTrait.php b/src/MessageTrait.php index db7ad61..ed77026 100644 --- a/src/MessageTrait.php +++ b/src/MessageTrait.php @@ -7,7 +7,8 @@ /** * Trait implementing functionality common to requests and responses. */ -trait MessageTrait { +trait MessageTrait +{ /** * @var string The HTTP protocol version. */ @@ -41,7 +42,7 @@ public function getProtocolVersion(): string */ public function getHeader(string $name): array { - if($this->hasHeader($name)){ + if ($this->hasHeader($name)) { $header = $this->headers[$name]; return is_array($header) ? $header : [$header]; @@ -79,6 +80,85 @@ public function getBody(): StreamInterface } return $this->body; } + /** + * Returns an instance with the specified protocol version. + * + * @param string $version The HTTP protocol version. + * @return self + */ + public function setProtocolVersion(string $version): self + { + $this->protocolVersion = $version; + return $this; + } + + /** + * Returns an instance with the specified header value. + * + * @param string $name The header name. + * @param string|string[] $value The header value. + * @return self + * @throws \InvalidArgumentException If the header value is not a string or an array of strings. + */ + public function setHeader(string $name, $value): self + { + if (!is_string($value) && !is_array($value)) { + throw new \InvalidArgumentException('Header value must be a string or an array of strings'); + } + + $normalizedValue = $this->normalizeHeaderValues($value); + + $this->headers[$name] = $normalizedValue; + return $this; + } + /** + * Returns an instance with the specified headers. + * + * @param array $headers An array of headers. + * @return self + */ + public function setHeaders(array $headers): self + { + foreach ($headers as $header) { + $this->headers[strtolower(key($header))] = (array) $header[key($header)]; + } + return $this; + } + /** + * Returns an instance with the specified appended header value. + * + * @param string $name The header name. + * @param string|string[] $value The header value. + * @return self + * @throws \InvalidArgumentException If the header value is not a string or an array of strings. + */ + public function AddHeader(string $name, $value): self + { + if (!is_string($value) && !is_array($value)) { + throw new \InvalidArgumentException('Header value must be a string or an array of strings'); + } + + $normalizedValue = $this->normalizeHeaderValues($value); + + if (isset($this->headers[$name])) { + $this->headers[$name] = array_merge($this->headers[$name], $normalizedValue); + } else { + $this->headers[$name] = $normalizedValue; + } + return $this; + } + + /** + * Returns an instance with the specified message body. + * + * @param StreamInterface|string $body The message body. + * @return self + */ + public function setBody(StreamInterface|string $body): self + { + $this->body = $body; + return $this; + } /** * Returns an instance with the specified protocol version. * @@ -87,9 +167,8 @@ public function getBody(): StreamInterface */ public function withProtocolVersion(string $version): self { - $new = clone $this; - $new->protocolVersion = $version; - return $new; + $this->protocolVersion = $version; + return $this; } /** * Checks if a header exists. diff --git a/src/Request.php b/src/Request.php index e24923c..525fd58 100644 --- a/src/Request.php +++ b/src/Request.php @@ -94,6 +94,19 @@ public function withRequestTarget($requestTarget): static return $clone; } + + /** + * Set the specific HTTP method. + * + * @param mixed $method The HTTP method. + * @return static + */ + public function setMethod($method): static + { + $this->method = strtoupper($method); + return $this; + } + /** * Returns an instance with the specific HTTP method. * @@ -133,4 +146,31 @@ public function withUri(UriInterface $uri, $preserveHost = false): static return $clone; } + + /** + * Returns an instance with the specific URI. + * + * @param UriInterface $uri The request URI. + * @param bool $preserveHost Whether to preserve the Host header. + * @return static + */ + public function setUri(UriInterface $uri, $preserveHost = false): static + { + $this->uri = $uri; + + if (!$preserveHost) { + $hostHeader = $this->getHeaderLine('Host'); + if ($hostHeader !== '') { + $this->withoutHeader('Host'); + } + if ($uri->getHost() !== '') { + $this->withHeader('Host', $uri->getHost()); + if ($uri->getPort() !== null) { + $this->withHeader('Host', $uri->getHost() . ':' . $uri->getPort()); + } + } + } + + return $this; + } } diff --git a/src/Response.php b/src/Response.php index 25ecb92..f57d42f 100644 --- a/src/Response.php +++ b/src/Response.php @@ -202,6 +202,30 @@ public function withStatus($code, $reasonPhrase = ''): static $clone->reasonPhrase = $reasonPhrase; return $clone; } + + /** + * set the specific status code and reason phrase. + * + * @param mixed $code The HTTP status code. + * @param string|null $reasonPhrase The reason phrase associated with the status code. + * @return static + * @throws \InvalidArgumentException If the status code is invalid. + */ + + public function setStatus($code, $reasonPhrase = ''): static + { + // Validate the status code. + if ($code < 100 || $code >= 600) { + throw new \InvalidArgumentException('Invalid status code.'); + } + if (empty($reasonPhrase)) { + $reasonPhrase = static::$statusTexts[$code] ?? ''; + } + + $this->statusCode = $code; + $this->reasonPhrase = $reasonPhrase; + return $this; + } /** * Retrieves the reason phrase associated with the status code. *