1 | <?php declare (strict_types=1); |
||
7 | class Iterator |
||
8 | { |
||
9 | private $requestFn; |
||
10 | private $resourceFn; |
||
11 | private $limit; |
||
12 | private $count; |
||
13 | private $resourcesKey; |
||
14 | private $markerKey; |
||
15 | private $mapFn; |
||
16 | private $currentMarker; |
||
17 | |||
18 | 33 | public function __construct(array $options, callable $requestFn, callable $resourceFn) |
|
19 | { |
||
20 | 33 | $this->limit = isset($options['limit']) ? $options['limit'] : false; |
|
21 | 33 | $this->count = 0; |
|
22 | |||
23 | 33 | if (isset($options['resourcesKey'])) { |
|
24 | 33 | $this->resourcesKey = $options['resourcesKey']; |
|
25 | 33 | } |
|
26 | |||
27 | 33 | if (isset($options['markerKey'])) { |
|
28 | 8 | $this->markerKey = $options['markerKey']; |
|
29 | 8 | } |
|
30 | |||
31 | 33 | if (isset($options['mapFn']) && is_callable($options['mapFn'])) { |
|
32 | 1 | $this->mapFn = $options['mapFn']; |
|
33 | 1 | } |
|
34 | |||
35 | 33 | $this->requestFn = $requestFn; |
|
36 | 33 | $this->resourceFn = $resourceFn; |
|
37 | 33 | } |
|
38 | |||
39 | 33 | private function fetchResources() |
|
55 | |||
56 | 33 | private function assembleResource(array $data) |
|
57 | { |
||
58 | 33 | $resource = call_user_func($this->resourceFn, $data); |
|
59 | |||
60 | // Invoke user-provided fn if provided |
||
61 | 33 | if ($this->mapFn) { |
|
62 | 1 | call_user_func_array($this->mapFn, [&$resource]); |
|
63 | 1 | } |
|
64 | |||
65 | // Update marker if operation supports it |
||
66 | 33 | if ($this->markerKey) { |
|
67 | 8 | $this->currentMarker = $resource->{$this->markerKey}; |
|
68 | 8 | } |
|
69 | |||
70 | 33 | return $resource; |
|
71 | } |
||
72 | |||
73 | 33 | private function totalReached() |
|
77 | |||
78 | 33 | private function shouldNotSendAnotherRequest() |
|
82 | |||
83 | 33 | public function __invoke() |
|
97 | } |
||
98 |