From ee100c9c00096994bbc6e03b51a6e8376fbe5991 Mon Sep 17 00:00:00 2001 From: Andrea Marco Sartori Date: Sat, 20 Jan 2024 21:35:39 +1000 Subject: [PATCH] Remove the response value object --- src/LazyJsonPages.php | 3 +- src/ValueObjects/Response.php | 97 ----------------------------------- 2 files changed, 1 insertion(+), 99 deletions(-) delete mode 100644 src/ValueObjects/Response.php diff --git a/src/LazyJsonPages.php b/src/LazyJsonPages.php index f47d793..8ee8116 100644 --- a/src/LazyJsonPages.php +++ b/src/LazyJsonPages.php @@ -9,7 +9,6 @@ use Cerbero\LazyJsonPages\Paginations\AnyPagination; use Cerbero\LazyJsonPages\Services\Client; use Cerbero\LazyJsonPages\Sources\AnySource; -use Cerbero\LazyJsonPages\ValueObjects\Response; use Closure; use GuzzleHttp\RequestOptions; use Illuminate\Support\LazyCollection; @@ -129,7 +128,7 @@ public function perPage(int $items, ?string $key = null, int $firstPageItems = 1 */ public function nextPage(Closure|string $key): self { - $this->config['nextPage'] = $key instanceof Closure ? $key : fn(Response $response) => $response->get($key); + $this->config['nextPage'] = $this->valueFromResponse($key); return $this; } diff --git a/src/ValueObjects/Response.php b/src/ValueObjects/Response.php deleted file mode 100644 index 9523604..0000000 --- a/src/ValueObjects/Response.php +++ /dev/null @@ -1,97 +0,0 @@ - $headers - */ - public readonly array $headers; - - /** - * Instantiate the class. - * - * @param array $headers - */ - public function __construct(public readonly string $json, array $headers) - { - $this->headers = $this->normalizeHeaders($headers); - } - - /** - * Normalize the given headers. - * - * @param array $headers - * @return array - */ - 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. - * - * @return ?string[] - */ - public function header(string $header): ?array - { - return $this->headers[strtolower($header)] ?? null; - } - - /** - * Retrieve a value from the body. - */ - public function json(string $key): mixed - { - $pointer = DotsConverter::toPointer($key); - $array = JsonParser::parse($this->json)->pointer($pointer)->toArray(); - - return empty($array) ? null : current($array); - } - - /** - * Retrieve an iterator with the given JSON pointer. - * - * @return Traversable - */ - public function pointer(string $pointer): Traversable - { - /** @var Traversable */ - return JsonParser::parse($this->json)->pointer($pointer); - } -}