-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLudoDBIterator.php
More file actions
185 lines (171 loc) · 4.15 KB
/
LudoDBIterator.php
File metadata and controls
185 lines (171 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
*
* User: Alf Magne Kalleland
* Date: 21.12.12
* @package LudoDB
* @author Alf Magne Kalleland <post@dhtmlgoodies.com>
*/
/**
* Iterator class for LudoDBCollection
* @package LudoDB
*/
class LudoDBIterator extends LudoDBObject implements Iterator
{
/**
* true when resource ref has been created
* @var
*/
private $loaded;
/**
* DB query resource reference
* @var
*/
private $dbResource;
/**
* is valid, i.e. has a row
* @var
*/
private $isValid;
/**
* Internal position reference
* @var int
*/
private $position;
/**
* Current row
* @var array
*/
protected $currentRow;
/**
* Array of returned rows.
* @var
*/
private $rows;
/**
* Internal data cache
* @var array
*/
private $valueCache;
/**
* Rewind iterator, i.e. start from beginning.
*/
function rewind() {
if ($this->dbResource) {
$this->dbResource = null;
}
$this->position = -1;
$this->loaded = false;
$this->isValid = false;
}
/**
* Return current value when iterating collection
* @method current
* @return mixed
*/
public function current() {
return $this->currentRow;
}
/**
Return key used for iterator. default is numeric.
@method key
@return mixed
@example
function key(){
return $this->currentRow['key']
}
to return key
*/
function key() {
return $this->position;
}
/**
* Go to next row.
*/
public function next() {
++$this->position;
$this->currentRow = $this->db->nextRow($this->dbResource);
$this->isValid = $this->currentRow ? true : false;
}
/**
* Returns true when
* @return bool
*/
public function valid() {
if (!$this->loaded) {
$this->load();
}
return $this->isValid;
}
/**
* Execute query and get result set reference.
*/
private function load(){
$this->dbResource = $this->db->query($this->sqlHandler()->getSql(), $this->sqlHandler()->getArguments());
$this->loaded = true;
$this->next();
}
/**
* Return collection data
* @method getValues
* @return array
*/
public function getValues(){
// TODO checkout the difference between $this->valueCache and $this->rows
if(!isset($this->valueCache)){
$this->clearStoredRows();
$groupBy = $this->parser->getGroupBy();
$this->valueCache = array();
$staticValues = $this->parser->getStaticValues();
foreach($this as $key=>$value){
if(is_array($value)) $value = array_merge($value, $staticValues);
if(isset($groupBy) && isset($value[$groupBy])){
if(!isset($this->valueCache[$groupBy])){
$this->valueCache[$groupBy] = array();
}
$this->valueCache[$groupBy][] = $value;
$this->storeRow($value);
}else{
$this->valueCache[$key] = $value;
$this->storeRow($this->valueCache[$key]);
}
}
}
return $this->valueCache;
}
/**
* Append current row to stored rows.
* @param $row
*/
protected function storeRow(&$row){
$this->rows[] = &$row;
}
/**
* Return rows as associated array where key is the value of one column.
* @param $key
* @return array
*/
protected function getRowsAssoc($key){
$rows = $this->getRows();
$ret = array();
foreach($rows as & $row){
if(isset($row[$key])){
$ret[$row[$key]] = & $row;
}
}
return $ret;
}
/**
* Returns reference to all tree nodes as numeric array
* @return Array
*/
public function getRows(){
return $this->rows;
}
/**
* Clear internal row array
*/
protected function clearStoredRows(){
$this->rows = array();
}
}