Skip to content

Commit

Permalink
Add mutators to Response, drop header case map from Request
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed May 31, 2019
1 parent 8ebbeea commit d7ae8d6
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 42 deletions.
33 changes: 4 additions & 29 deletions lib/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ final class Request
/** @var array headers with lowercase keys */
private $headers = [];

/** @var array lowercase header to actual case map */
private $headerCaseMap = [];

/** @var RequestBody */
private $body;

Expand Down Expand Up @@ -199,7 +196,6 @@ public function withHeader(string $field, string $value): self
$clone = clone $this;

$clone->headers[$lower] = [\trim($value)];
$clone->headerCaseMap[$lower] = $field;

return $clone;
}
Expand All @@ -223,7 +219,6 @@ public function withAddedHeader(string $field, string $value): self
$headers[] = \trim($value);

$clone->headers[$lower] = $headers;
$clone->headerCaseMap[$lower] = $field;

return $clone;
}
Expand Down Expand Up @@ -264,10 +259,8 @@ public function withHeaders(array $headers): self
$clone->headers[$lower][] = \trim($value);
}

$clone->headerCaseMap[$lower] = $field;

if (empty($clone->headers[$lower])) {
unset($clone->headers[$lower], $clone->headerCaseMap[$lower]);
unset($clone->headers[$lower]);
}
}

Expand All @@ -277,23 +270,11 @@ public function withHeaders(array $headers): self
/**
* Retrieve an associative array of headers matching field names to an array of field values.
*
* @param bool $originalCase If true, headers are returned in the case of the last set header with that name.
*
* @return array
*/
public function getHeaders(bool $originalCase = false): array
public function getHeaders(): array
{
if (!$originalCase) {
return $this->headers;
}

$headers = [];

foreach ($this->headers as $header => $values) {
$headers[$this->headerCaseMap[$header]] = $values;
}

return $headers;
return $this->headers;
}

/**
Expand All @@ -305,14 +286,8 @@ public function getHeaders(bool $originalCase = false): array
*/
public function withoutHeader(string $field): self
{
$lower = \strtolower($field);

$clone = clone $this;

unset(
$clone->headerCaseMap[$lower],
$clone->headers[$lower]
);
unset($clone->headers[\strtolower($field)]);

return $clone;
}
Expand Down
154 changes: 141 additions & 13 deletions lib/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public function getProtocolVersion(): string
return $this->protocolVersion;
}

public function withProtocolVersion(string $protocolVersion): self
{
$clone = clone $this;
$clone->protocolVersion = $protocolVersion;

return $clone;
}

/**
* Retrieve the response's three-digit HTTP status code.
*
Expand All @@ -63,6 +71,14 @@ public function getStatus(): int
return $this->status;
}

public function withStatus(string $status): self
{
$clone = clone $this;
$clone->status = $status;

return $clone;
}

/**
* Retrieve the response's (possibly empty) reason phrase.
*
Expand All @@ -73,6 +89,14 @@ public function getReason(): string
return $this->reason;
}

public function withReason(string $reason): self
{
$clone = clone $this;
$clone->reason = $reason;

return $clone;
}

/**
* Retrieve the Request instance that resulted in this Response instance.
*
Expand All @@ -83,6 +107,14 @@ public function getRequest(): Request
return $this->request;
}

public function withRequest(Request $request): self
{
$clone = clone $this;
$clone->request = $request;

return $clone;
}

/**
* Retrieve the original Request instance associated with this Response instance.
*
Expand Down Expand Up @@ -154,21 +186,94 @@ public function getHeaderArray(string $field): array
}

/**
* Retrieve an associative array of headers matching field names to an array of field values.
* Assign a value for the specified header field by replacing any existing values for that field.
*
* @param string $field Header name.
* @param string $value Header value.
*
* @return Response
*/
public function withHeader(string $field, string $value): self
{
$field = \trim($field);
$lower = \strtolower($field);

$clone = clone $this;

$clone->headers[$lower] = [\trim($value)];

return $clone;
}

/**
* Assign a value for the specified header field by adding an additional header line.
*
* **Format**
* @param string $field Header name.
* @param string $value Header value.
*
* ```php
* [
* "header-1" => [
* "value-1",
* "value-2",
* ],
* "header-2" => [
* "value-1",
* ],
* ]
* ```
* @return Response
*/
public function withAddedHeader(string $field, string $value): self
{
$field = \trim($field);
$lower = \strtolower($field);

$clone = clone $this;

$headers = $clone->headers[$lower] ?? [];
$headers[] = \trim($value);

$clone->headers[$lower] = $headers;

return $clone;
}

public function withHeaders(array $headers): self
{
$clone = clone $this;

foreach ($headers as $field => $values) {
if (!\is_string($field) && !\is_int($field)) {
// PHP converts integer strings automatically to integers.
// Later versions of PHP might allow other key types.
// @codeCoverageIgnoreStart
/** @noinspection PhpUndefinedClassInspection */
throw new \TypeError("All array keys for withHeaders must be strings");
// @codeCoverageIgnoreEnd
}

$field = \trim($field);
$lower = \strtolower($field);

if (!\is_array($values)) {
$values = [$values];
}

$clone->headers[$lower] = [];

foreach ($values as $value) {
if (!\is_string($value)
&& !\is_int($value)
&& !\is_float($value)
&& !(\is_object($value) && \method_exists($value, '__toString'))
) {
/** @noinspection PhpUndefinedClassInspection */
throw new \TypeError("All values for withHeaders must be string or an array of strings");
}

$clone->headers[$lower][] = \trim($value);
}

if (empty($clone->headers[$lower])) {
unset($clone->headers[$lower]);
}
}

return $clone;
}

/**
* Retrieve an associative array of headers matching field names to an array of field values.
*
* @return array
*/
Expand All @@ -177,6 +282,21 @@ public function getHeaders(): array
return $this->headers;
}

/**
* Remove the specified header field from the message.
*
* @param string $field Header name.
*
* @return Response
*/
public function withoutHeader(string $field): self
{
$clone = clone $this;
unset($clone->headers[\strtolower($field)]);

return $clone;
}

/**
* Retrieve the response body.
*
Expand All @@ -194,6 +314,14 @@ public function getConnectionInfo(): ConnectionInfo
return $this->connectionInfo;
}

public function withConnectionInfo(ConnectionInfo $connectionInfo): self
{
$clone = clone $this;
$clone->connectionInfo = $connectionInfo;

return $clone;
}

public function withPreviousResponse(?Response $previousResponse): self
{
$clone = clone $this;
Expand Down

0 comments on commit d7ae8d6

Please sign in to comment.