Completed
Pull Request — master (#268)
by
unknown
01:25
created

Xhgui_Storage_ResultSet   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 148
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A toArray() 0 4 1
A count() 0 4 1
A sort() 0 4 1
A skip() 0 5 1
A limit() 0 5 1
A get() 0 4 1
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 3
A rewind() 0 4 1
A getTotalRows() 0 4 1
A setTotalRows() 0 4 1
1
<?php
2
3
/**
4
 * Class Xhgui_Storage_ResultSet
5
 */
6
class Xhgui_Storage_ResultSet implements \Iterator
7
{
8
9
    /**
10
     * @var array|null
11
     */
12
    protected $data = [];
13
    /**
14
     * @var array
15
     */
16
    protected $keys = [];
17
18
    /**
19
     * @var int
20
     */
21
    protected $i = 0;
22
    /**
23
     * @var int
24
     */
25
    protected $limit = 25;
26
    /**
27
     * @var int
28
     */
29
    protected $totalRows = 0;
30
31
    /**
32
     * Xhgui_Storage_ResultSet constructor.
33
     * @param null $data
34
     * @param int $totalRows
35
     */
36
    public function __construct($data = null, $totalRows = 0)
37
    {
38
        $this->data = $data;
39
        $this->keys = array_keys($data);
40
        $this->totalRows = $totalRows;
41
    }
42
43
    /**
44
     * @return array|null
45
     */
46
    public function toArray()
47
    {
48
        return $this->data;
49
    }
50
51
    /**
52
     * @return int
53
     */
54
    public function count()
55
    {
56
        return count($this->data);
57
    }
58
59
    /**
60
     * @return $this
61
     */
62
    public function sort()
63
    {
64
        return $this;
65
    }
66
67
    /**
68
     * @param $count
69
     * @return $this
70
     */
71
    public function skip($count)
72
    {
73
        $this->i += $count;
74
        return $this;
75
    }
76
77
    /**
78
     * @param $limit
79
     * @return $this
80
     */
81
    public function limit($limit)
82
    {
83
        $this->limit = $limit;
84
        return $this;
85
    }
86
87
    /**
88
     * @param $i
89
     * @return mixed
90
     */
91
    public function get($i)
92
    {
93
        return $this->data[$i];
94
    }
95
96
    /**
97
     * Return the current element
98
     */
99
    public function current()
100
    {
101
        return $this->get($this->keys[$this->i]);
102
    }
103
104
    /**
105
     * Move forward to next element
106
     */
107
    public function next()
108
    {
109
        $this->i++;
110
    }
111
112
    /**
113
     * Return the key of the current element
114
     */
115
    public function key()
116
    {
117
        return $this->keys[$this->i];
118
    }
119
120
    /**
121
     * Checks if current position is valid
122
     *
123
     * Returns true on success or false on failure.
124
     */
125
    public function valid()
126
    {
127
        return !empty($this->keys[$this->i]) AND !empty($this->data[$this->keys[$this->i]]) AND $this->i < $this->limit;
128
    }
129
130
    /**
131
     * Rewind the Iterator to the first element
132
     */
133
    public function rewind()
134
    {
135
        $this->i = 0;
136
    }
137
138
    /**
139
     * @return int
140
     */
141
    public function getTotalRows()
142
    {
143
        return $this->totalRows;
144
    }
145
146
    /**
147
     * @param int $totalRows
148
     */
149
    public function setTotalRows($totalRows)
150
    {
151
        $this->totalRows = $totalRows;
152
    }
153
}
154