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: * @api
30: *
31: * @param list<mixed> $entities
32: * @param mixed[]|null $nextQuery
33: */
34: public function __construct(
35: array $entities,
36: ?RequestInterface $nextRequest = null,
37: ?array $nextQuery = null,
38: ?int $current = null,
39: ?int $total = null
40: ) {
41: $this->Entities = $entities;
42: $this->Current = $current;
43: $this->Total = $total;
44: $this->NextRequest = $nextRequest
45: ? new CurlerPageRequest($nextRequest, $nextQuery)
46: : null;
47: }
48:
49: /**
50: * @inheritDoc
51: */
52: public function getEntities(): array
53: {
54: return $this->Entities;
55: }
56:
57: /**
58: * @inheritDoc
59: */
60: public function hasNextRequest(): bool
61: {
62: return (bool) $this->NextRequest;
63: }
64:
65: /**
66: * @inheritDoc
67: */
68: public function getNextRequest()
69: {
70: if (!$this->NextRequest) {
71: throw new OutOfRangeException('No more pages');
72: }
73: return $this->NextRequest;
74: }
75:
76: /**
77: * Get the value of $current passed to the constructor
78: */
79: public function getCurrent(): ?int
80: {
81: return $this->Current;
82: }
83:
84: /**
85: * Get the value of $total passed to the constructor
86: */
87: public function getTotal(): ?int
88: {
89: return $this->Total;
90: }
91: }
92: