Passed
Pull Request — master (#7)
by Chito
01:33
created

PaginationResult   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
eloc 28
c 1
b 0
f 1
dl 0
loc 119
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 3 1
A __construct() 0 3 1
A key() 0 7 2
A rewind() 0 3 1
A next() 0 7 2
A serialize() 0 3 1
A valid() 0 7 2
A current() 0 7 2
A getIterator() 0 10 2
A unserialize() 0 7 1
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