|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace prgTW\BaseCRM\Resource; |
|
4
|
|
|
|
|
5
|
|
|
class PagingIterator implements \Iterator, \Countable |
|
6
|
|
|
{ |
|
7
|
|
|
/** @var callable */ |
|
8
|
|
|
protected $generator; |
|
9
|
|
|
|
|
10
|
|
|
/** @var array */ |
|
11
|
|
|
protected $args; |
|
12
|
|
|
|
|
13
|
|
|
/** @var int */ |
|
14
|
|
|
protected $page; |
|
15
|
|
|
|
|
16
|
|
|
/** @var array */ |
|
17
|
|
|
protected $query; |
|
18
|
|
|
|
|
19
|
|
|
/** @var ResourceCollection */ |
|
20
|
|
|
protected $resourceCollection; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param callable $generator |
|
24
|
|
|
* @param int $page |
|
25
|
|
|
* @param array $query |
|
26
|
|
|
* |
|
27
|
|
|
* @throws \InvalidArgumentException when generator is not callable |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($generator, $page, array $query = []) |
|
30
|
|
|
{ |
|
31
|
|
|
if (false === is_callable($generator)) |
|
32
|
|
|
{ |
|
33
|
|
|
//@codeCoverageIgnoreStart |
|
34
|
|
|
throw new \InvalidArgumentException('Generator is not callable'); |
|
35
|
|
|
//@codeCoverageIgnoreEnd |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->generator = $generator; |
|
39
|
|
|
$this->args = array( |
|
40
|
|
|
'page' => $page, |
|
41
|
|
|
'query' => $query, |
|
42
|
|
|
); |
|
43
|
|
|
$this->reset(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function reset() |
|
47
|
|
|
{ |
|
48
|
|
|
$this->page = $this->args['page']; |
|
49
|
|
|
$this->query = $this->args['query']; |
|
50
|
|
|
$this->resourceCollection = null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** {@inheritdoc} */ |
|
54
|
|
|
public function current() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->resourceCollection->current(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** {@inheritdoc} */ |
|
60
|
|
|
public function key() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->resourceCollection->key(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** {@inheritdoc} */ |
|
66
|
|
|
public function next() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->loadIfNecessary(); |
|
69
|
|
|
|
|
70
|
|
|
return $this->resourceCollection->next(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** {@inheritdoc} */ |
|
74
|
|
|
public function valid() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->loadIfNecessary(); |
|
77
|
|
|
|
|
78
|
|
|
return null !== $this->key(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** {@inheritdoc} */ |
|
82
|
|
|
public function rewind() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->reset(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** {@inheritdoc} */ |
|
88
|
|
|
public function count() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->resourceCollection->count(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function loadIfNecessary() |
|
94
|
|
|
{ |
|
95
|
|
|
if (false === $this->isLoadNecessary()) |
|
96
|
|
|
{ |
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** @var Page $page */ |
|
101
|
|
|
$page = call_user_func_array($this->generator, array( |
|
102
|
|
|
$this->page, |
|
103
|
|
|
$this->query, |
|
104
|
|
|
)); |
|
105
|
|
|
|
|
106
|
|
|
$this->resourceCollection = $page->getResourceCollection(); |
|
107
|
|
|
++$this->page; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function isLoadNecessary() |
|
114
|
|
|
{ |
|
115
|
|
|
return null === $this->resourceCollection; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|