Skip to content

Commit

Permalink
Implement the HTTP response value object
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Jan 2, 2024
1 parent a835702 commit 7472d23
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/ValueObjects/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Cerbero\LazyJsonPages\ValueObjects;

use Cerbero\JsonParser\JsonParser;

/**
* The HTTP response.
*/
final class Response
{
/**
* The headers of the HTTP response.
*
* @var array<string, string> $headers
*/
public readonly array $headers;

/**
* Instantiate the class.
*
* @param array<string, string> $headers
*/
public function __construct(public readonly string $json, array $headers)
{
$this->headers = $this->normalizeHeaders($headers);
}

/**
* Normalize the given headers.
*
* @param array<string, string> $headers
* @return array<string, string>
*/
private function normalizeHeaders(array $headers): array
{
$normalizedHeaders = [];

foreach ($headers as $name => $value) {
$normalizedHeaders[strtolower($name)] = $value;
}

return $normalizedHeaders;
}

/**
* Retrieve a value from the body or a header.
*/
public function get(string $key): mixed
{
return $this->hasHeader($key) ? $this->header($key) : $this->json($key);
}

/**
* Determine whether the given header is set.
*/
public function hasHeader(string $header): bool
{
return isset($this->headers[strtolower($header)]);
}

/**
* Retrieve the given header.
*/
public function header(string $header): ?string
{
return $this->headers[strtolower($header)] ?? null;
}

/**
* Retrieve a value from the body.
*/
public function json(string $key): mixed
{
$array = JsonParser::parse($this->json)->pointer($key)->toArray();

return empty($array) ? null : current($array);
}
}

0 comments on commit 7472d23

Please sign in to comment.