1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lampager\Cake; |
6
|
|
|
|
7
|
|
|
use Cake\Collection\CollectionTrait; |
8
|
|
|
use Cake\Datasource\ResultSetInterface; |
9
|
|
|
use Iterator; |
10
|
|
|
use IteratorAggregate; |
11
|
|
|
use Lampager\PaginationResult as LampagerPaginationResult; |
12
|
|
|
use Traversable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class PaginationResult |
16
|
|
|
* |
17
|
|
|
* This class intentionally does not extend \Lampager\PaginationResult |
18
|
|
|
* but has the same signature because \Cake\Datasource\ResultSetInterface |
19
|
|
|
* already implements \Iterator which conflicts with \IteratorAggregate. |
20
|
|
|
*/ |
21
|
|
|
class PaginationResult implements ResultSetInterface |
22
|
|
|
{ |
23
|
|
|
/** @var LampagerPaginationResult */ |
24
|
|
|
protected $result; |
25
|
|
|
|
26
|
|
|
/** @var Iterator */ |
27
|
|
|
protected $iterator; |
28
|
|
|
|
29
|
|
|
use CollectionTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* PaginationResult constructor. |
33
|
|
|
* Merge $meta entries into $this. |
34
|
|
|
* |
35
|
|
|
* @param mixed $rows |
36
|
|
|
*/ |
37
|
|
|
public function __construct($rows, array $meta) |
38
|
|
|
{ |
39
|
|
|
$this->result = new LampagerPaginationResult($rows, $meta); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function current() |
46
|
|
|
{ |
47
|
|
|
return $this->unwrap()->current(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function key() |
54
|
|
|
{ |
55
|
|
|
return $this->unwrap()->key(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function next() |
62
|
|
|
{ |
63
|
|
|
$this->unwrap()->next(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function rewind() |
70
|
|
|
{ |
71
|
|
|
$this->unwrap()->rewind(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function valid() |
78
|
|
|
{ |
79
|
|
|
return $this->unwrap()->valid(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
* @return Iterator |
85
|
|
|
*/ |
86
|
|
|
public function unwrap(): Traversable |
87
|
|
|
{ |
88
|
|
|
if (!$this->iterator) { |
89
|
|
|
$this->iterator = $this->getIterator(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->iterator; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function toArray(bool $preserveKeys = true): array |
99
|
|
|
{ |
100
|
|
|
$array = (array)$this->result; |
101
|
|
|
$array['records'] = iterator_to_array($this->unwrap()); |
102
|
|
|
|
103
|
|
|
return $array; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function jsonSerialize(): array |
110
|
|
|
{ |
111
|
|
|
return $this->toArray(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function serialize() |
118
|
|
|
{ |
119
|
|
|
return json_encode($this->jsonSerialize()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function unserialize($serialized) |
126
|
|
|
{ |
127
|
|
|
$obj = json_decode($serialized, true); |
128
|
|
|
$meta = $obj; |
129
|
|
|
unset($meta['records']); |
130
|
|
|
|
131
|
|
|
$this->result = new LampagerPaginationResult($obj['records'], $meta); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function getIterator(): Iterator |
135
|
|
|
{ |
136
|
|
|
/** @var Iterator|IteratorAggregate */ |
137
|
|
|
$iterator = $this->result->getIterator(); |
138
|
|
|
|
139
|
|
|
if ($iterator instanceof IteratorAggregate) { |
140
|
|
|
$iterator = $iterator->getIterator(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $iterator; |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $name The name of the parameter to fetch |
148
|
|
|
* |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
|
|
public function __get(string $name) |
152
|
|
|
{ |
153
|
|
|
if (property_exists($this->result, $name)) { |
154
|
|
|
return $this->result->{$name}; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$trace = debug_backtrace(); |
158
|
|
|
trigger_error( |
159
|
|
|
'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], |
160
|
|
|
E_USER_NOTICE |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns an array that can be used to describe the internal state of this |
168
|
|
|
* object. |
169
|
|
|
*/ |
170
|
|
|
public function __debugInfo(): array |
171
|
|
|
{ |
172
|
|
|
return [ |
173
|
|
|
'(help)' => 'This is a Lampager Pagination Result object.', |
174
|
|
|
] + $this->jsonSerialize(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|