1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lampager\Cake; |
4
|
|
|
|
5
|
|
|
use Cake\Collection\CollectionTrait; |
6
|
|
|
use Cake\Datasource\ResultSetInterface; |
7
|
|
|
use Iterator; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
use Lampager\PaginationResult as LampagerPaginationResult; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PaginationResult |
13
|
|
|
* |
14
|
|
|
* This class intentionally does not extend \Lampager\PaginationResult |
15
|
|
|
* but has the same signature because \Cake\Datasource\ResultSetInterface |
16
|
|
|
* already implements \Iterator which conflicts with \IteratorAggregate. |
17
|
|
|
*/ |
18
|
|
|
class PaginationResult implements ResultSetInterface |
19
|
|
|
{ |
20
|
|
|
/** @var LampagerPaginationResult */ |
21
|
|
|
protected $result; |
22
|
|
|
|
23
|
|
|
/** @var Iterator */ |
24
|
|
|
protected $iterator; |
25
|
|
|
|
26
|
|
|
use CollectionTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* PaginationResult constructor. |
30
|
|
|
* Merge $meta entries into $this. |
31
|
|
|
* |
32
|
|
|
* @param mixed $rows |
33
|
|
|
* @param array $meta |
34
|
|
|
*/ |
35
|
|
|
public function __construct($rows, array $meta) |
36
|
|
|
{ |
37
|
|
|
$this->result = new LampagerPaginationResult($rows, $meta); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
public function current() |
44
|
|
|
{ |
45
|
|
|
if (!$this->iterator) { |
46
|
|
|
$this->iterator = $this->getIterator(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->iterator->current(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function key() |
56
|
|
|
{ |
57
|
|
|
if (!$this->iterator) { |
58
|
|
|
$this->iterator = $this->getIterator(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $this->iterator->key(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function next() |
68
|
|
|
{ |
69
|
|
|
if (!$this->iterator) { |
70
|
|
|
$this->iterator = $this->getIterator(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->iterator->next(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
|
|
public function rewind() |
80
|
|
|
{ |
81
|
|
|
if (!$this->iterator) { |
82
|
|
|
$this->iterator = $this->getIterator(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->iterator->rewind(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
|
|
public function valid() |
92
|
|
|
{ |
93
|
|
|
if (!$this->iterator) { |
94
|
|
|
$this->iterator = $this->getIterator(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->iterator->valid(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritDoc} |
102
|
|
|
*/ |
103
|
|
|
public function jsonSerialize() |
104
|
|
|
{ |
105
|
|
|
return (array)$this->result; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
|
|
public function serialize() |
112
|
|
|
{ |
113
|
|
|
return json_encode($this->result); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
|
|
public function unserialize($serialized) |
120
|
|
|
{ |
121
|
|
|
$obj = json_decode($serialized, true); |
122
|
|
|
$meta = $obj; |
123
|
|
|
unset($meta['records']); |
124
|
|
|
|
125
|
|
|
$this->result = new LampagerPaginationResult($obj['records'], $meta); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return Iterator |
130
|
|
|
*/ |
131
|
|
|
protected function getIterator() |
132
|
|
|
{ |
133
|
|
|
/** @var Iterator|IteratorAggregate */ |
134
|
|
|
$iterator = $this->result->getIterator(); |
135
|
|
|
|
136
|
|
|
if ($iterator instanceof IteratorAggregate) { |
137
|
|
|
$iterator = $iterator->getIterator(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $iterator; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|