Skip to content

Commit

Permalink
add setter for each var in Message/Request/Response
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Jan 6, 2024
1 parent 0c4e0b8 commit 99db0c0
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 5 deletions.
89 changes: 84 additions & 5 deletions src/MessageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
/**
* Trait implementing functionality common to requests and responses.
*/
trait MessageTrait {
trait MessageTrait
{
/**
* @var string The HTTP protocol version.
*/
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
24 changes: 24 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 99db0c0

Please # to comment.