|
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
|
|
|
$this->iterator = $this->getIterator(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* {@inheritDoc} |
|
86
|
|
|
*/ |
|
87
|
|
|
public function valid() |
|
88
|
|
|
{ |
|
89
|
|
|
if (!$this->iterator) { |
|
90
|
|
|
$this->iterator = $this->getIterator(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->iterator->valid(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritDoc} |
|
98
|
|
|
*/ |
|
99
|
|
|
public function jsonSerialize() |
|
100
|
|
|
{ |
|
101
|
|
|
return (array)$this->result; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritDoc} |
|
106
|
|
|
*/ |
|
107
|
|
|
public function serialize() |
|
108
|
|
|
{ |
|
109
|
|
|
return json_encode($this->result); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritDoc} |
|
114
|
|
|
*/ |
|
115
|
|
|
public function unserialize($serialized) |
|
116
|
|
|
{ |
|
117
|
|
|
$obj = json_decode($serialized, true); |
|
118
|
|
|
$meta = $obj; |
|
119
|
|
|
unset($meta['records']); |
|
120
|
|
|
|
|
121
|
|
|
$this->result = new LampagerPaginationResult($obj['records'], $meta); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return Iterator |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function getIterator() |
|
128
|
|
|
{ |
|
129
|
|
|
/** @var Iterator|IteratorAggregate */ |
|
130
|
|
|
$iterator = $this->result->getIterator(); |
|
131
|
|
|
|
|
132
|
|
|
if ($iterator instanceof IteratorAggregate) { |
|
133
|
|
|
$iterator = $iterator->getIterator(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $iterator; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|