Skip to content

Commit

Permalink
Gather data from responses
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Dec 19, 2023
1 parent 12c977d commit 333facb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/Services/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

namespace Cerbero\LazyJsonPages\Services;

use Cerbero\JsonParser\Concerns\DetectsEndpoints;
use Cerbero\LazyJsonPages\Dtos\Config;
use Cerbero\LazyJsonPages\Sources\AnySource;
use Closure;

final class ConfigFactory
{
use DetectsEndpoints;

/**
* The dot to extract items from.
*/
Expand Down Expand Up @@ -159,7 +162,7 @@ private function integerFromResponse(Closure|string $key, int $minimum = 0): int
{
return (int) max($minimum, match (true) {
$key instanceof Closure => $key($this->source->response()),
default => $this->source->response()->json($key) ?? $this->source->response()->header($key),
default => $this->source->response($key),
});
}

Expand All @@ -185,7 +188,7 @@ protected function resolvePage($value)
{
if (is_numeric($value)) {
return (int) $value;
} elseif ($this->isEndpoint($value = $this->source->json($value))) {
} elseif (is_string($value) && $this->isEndpoint($value = $this->source->response($value))) {
parse_str(parse_url($value, PHP_URL_QUERY), $query);
$value = $query[$this->pageName];
}
Expand Down Expand Up @@ -230,7 +233,7 @@ private function pageFromResponse(Closure|string $key, int $minimum = 0): string
{
return (int) max($minimum, match (true) {
$key instanceof Closure => $key($this->source->response()),
default => $this->source->response()->json($key) ?? $this->source->response()->header($key),
default => $this->source->response($key),
});
}

Expand Down
14 changes: 10 additions & 4 deletions src/Sources/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
namespace Cerbero\LazyJsonPages\Sources;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

interface Source
abstract class Source
{
public function request(): RequestInterface;
public final function __construct(
protected readonly mixed $source,
) {
}

public function response(): ResponseInterface;
abstract public function matches(): bool;

abstract public function request(): RequestInterface;

abstract public function response(?string $key = null): mixed;
}

0 comments on commit 333facb

Please sign in to comment.