1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lampager\Idiorm; |
4
|
|
|
|
5
|
|
|
use IdiormMethodMissingException; |
6
|
|
|
use IdiormResultSet; |
7
|
|
|
use Lampager\Idiorm\Concerns\HasSnakeAliases; |
8
|
|
|
use Lampager\PaginationResult as BasePaginationResult; |
9
|
|
|
use Model; |
10
|
|
|
use ORM; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PaginationResult |
14
|
|
|
* |
15
|
|
|
* @method array to_array() Get the instance as an array. |
16
|
|
|
* @method mixed json_serialize() Convert the object into something JSON serializable. |
17
|
|
|
* @method string to_json(int $options = 0) Convert the object to its JSON representation. |
18
|
|
|
* @method \ArrayIterator get_iterator() Get iterator of records. |
19
|
|
|
* |
20
|
|
|
* @mixin IdiormResultSet |
21
|
|
|
* |
22
|
|
|
* @see BasePaginationResult |
23
|
|
|
*/ |
24
|
|
|
class PaginationResult extends BasePaginationResult implements \JsonSerializable |
25
|
|
|
{ |
26
|
|
|
use HasSnakeAliases; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Make dynamic calls into the collection. |
30
|
|
|
* |
31
|
|
|
* @param string $method |
32
|
|
|
* @param array $parameters |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
2 |
|
public function __call($method, array $parameters) |
36
|
|
|
{ |
37
|
2 |
|
if ($this->hasSnakeMethod($method)) { |
38
|
1 |
|
return $this->callSnakeMethod($method, $parameters); |
39
|
|
|
} |
40
|
|
|
try { |
41
|
1 |
|
$r = $this->records instanceof IdiormResultSet ? $this->records : new IdiormResultSet($this->records); |
42
|
1 |
|
return $r->{$method}(...$parameters); |
43
|
1 |
|
} catch (IdiormMethodMissingException $e) { |
44
|
1 |
|
return $this->callSnakeMethod($method, $parameters); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the instance as an array. |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
19 |
|
public function toArray() |
54
|
|
|
{ |
55
|
19 |
|
$array = []; |
56
|
19 |
|
foreach (get_object_vars($this) as $key => $value) { |
57
|
19 |
|
$array[strtolower(preg_replace('/[a-z]+(?=[A-Z])/', '\0_', $key))] = $value; |
58
|
|
|
} |
59
|
19 |
|
return $array; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Convert the object into something JSON serializable. |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
19 |
|
public function jsonSerialize() |
68
|
|
|
{ |
69
|
19 |
|
$array = $this->toArray(); |
70
|
19 |
|
if (isset($array['records']) && is_array($array['records'])) { |
71
|
19 |
|
$array['records'] = array_map(function ($record) { |
72
|
|
|
/* @var $record ORM|Model */ |
73
|
19 |
|
return $record->asArray(); |
74
|
19 |
|
}, $array['records']); |
75
|
|
|
} |
76
|
19 |
|
return $array; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Convert the object to its JSON representation. |
81
|
|
|
* |
82
|
|
|
* @param int $options |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
1 |
|
public function toJson($options = 0) |
86
|
|
|
{ |
87
|
1 |
|
return json_encode($this->jsonSerialize(), $options); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|