Passed
Push — master ( 9971c3...383493 )
by Chito
01:52
created

PaginationResult::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
        return $this->unwrap()->current();
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function key()
52
    {
53
        return $this->unwrap()->key();
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function next()
60
    {
61
        $this->unwrap()->next();
62
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67
    public function rewind()
68
    {
69
        $this->unwrap()->rewind();
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function valid()
76
    {
77
        return $this->unwrap()->valid();
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function unwrap()
84
    {
85
        if (!$this->iterator) {
86
            $this->iterator = $this->getIterator();
87
        }
88
89
        return $this->iterator;
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function toArray($preserveKeys = true)
96
    {
97
        $array = (array)$this->result;
98
        $array['records'] = iterator_to_array($this->unwrap());
99
100
        return $array;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106
    public function jsonSerialize()
107
    {
108
        return $this->toArray();
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function serialize()
115
    {
116
        return json_encode($this->jsonSerialize());
117
    }
118
119
    /**
120
     * {@inheritDoc}
121
     */
122
    public function unserialize($serialized)
123
    {
124
        $obj = json_decode($serialized, true);
125
        $meta = $obj;
126
        unset($meta['records']);
127
128
        $this->result = new LampagerPaginationResult($obj['records'], $meta);
129
    }
130
131
    /**
132
     * @return Iterator
133
     */
134
    protected function getIterator()
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
     * Returns an array that can be used to describe the internal state of this
148
     * object.
149
     *
150
     * @return array
151
     */
152
    public function __debugInfo()
153
    {
154
        return [
155
            '(help)' => 'This is a Lampager Pagination Result object.',
156
        ] + $this->jsonSerialize();
157
    }
158
}
159