1: <?php declare(strict_types=1);
2:
3: namespace Salient\Curler;
4:
5: use Psr\Http\Message\RequestInterface;
6: use Salient\Contract\Curler\CurlerPageInterface;
7: use OutOfRangeException;
8:
9: /**
10: * A page of data returned by an HTTP endpoint
11: *
12: * @api
13: */
14: class CurlerPage implements CurlerPageInterface
15: {
16: /** @var list<mixed> */
17: protected array $Entities;
18: protected ?int $Current;
19: protected ?int $Total;
20: protected ?CurlerPageRequest $NextRequest;
21:
22: /**
23: * Creates a new CurlerPage object
24: *
25: * `$current` and `$total`, together with {@see CurlerPage::getCurrent()}
26: * and {@see CurlerPage::getTotal()}, allow pagers to track progress across
27: * responses if necessary.
28: *
29: * @param list<mixed> $entities
30: * @param mixed[]|null $nextQuery
31: */
32: public function __construct(
33: array $entities,
34: ?RequestInterface $nextRequest = null,
35: ?array $nextQuery = null,
36: ?int $current = null,
37: ?int $total = null
38: ) {
39: $this->Entities = $entities;
40: $this->Current = $current;
41: $this->Total = $total;
42: $this->NextRequest = $nextRequest
43: ? new CurlerPageRequest($nextRequest, $nextQuery)
44: : null;
45: }
46:
47: /**
48: * @inheritDoc
49: */
50: public function getEntities(): array
51: {
52: return $this->Entities;
53: }
54:
55: /**
56: * @inheritDoc
57: */
58: public function hasNextRequest(): bool
59: {
60: return (bool) $this->NextRequest;
61: }
62:
63: /**
64: * @inheritDoc
65: */
66: public function getNextRequest()
67: {
68: if (!$this->NextRequest) {
69: throw new OutOfRangeException('No more pages');
70: }
71: return $this->NextRequest;
72: }
73:
74: /**
75: * Get the value of $current passed to the constructor
76: */
77: public function getCurrent(): ?int
78: {
79: return $this->Current;
80: }
81:
82: /**
83: * Get the value of $total passed to the constructor
84: */
85: public function getTotal(): ?int
86: {
87: return $this->Total;
88: }
89: }
90: