-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathORMIterator.php
199 lines (165 loc) · 4.27 KB
/
ORMIterator.php
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
*
*
* @package ORM
* @author Espen Volden
*/
class AetherORMIterator implements Iterator, ArrayAccess, Countable {
// Class attributes
protected $className;
protected $primaryKey;
protected $primaryVal;
// Database result object
protected $result;
public function __construct(AetherORM $model, AetherDatabaseResult $result) {
// Class attributes
$this->className = get_class($model);
$this->primaryKey = $model->primaryKey;
$this->primaryVal = $model->primaryVal;
// AetherDatabase result
$this->result = $result->result(true);
}
/**
* Returns an array of the results as AetherORM objects.
*
* @return array
*/
public function asArray() {
$array = array();
if ($results = $this->result->resultArray()) {
// Import class name
$class = $this->className;
foreach ($results as $obj)
$array[] = new $class($obj);
}
return $array;
}
/**
* Return an array of all of the primary keys for this object.
*
* @return array
*/
public function primaryKeyArray() {
$ids = array();
foreach ($this->result as $row)
$ids[] = $row->{$this->primaryKey};
return $ids;
}
/**
* Create a key/value array from the results.
*
* @param string $key key column
* @param string $val value column
* @return array
*/
public function selectList($key = NULL, $val = NULL) {
if ($key === NULL) {
// Use the default key
$key = $this->primaryKey;
}
if ($val === NULL) {
// Use the default value
$val = $this->primaryVal;
}
$array = array();
foreach ($this->result->resultArray() as $row)
$array[$row->$key] = $row->$val;
return $array;
}
/**
* Return a range of offsets.
*
* @param integer start
* @param integer end
* @return array
*/
public function range($start, $end) {
// Array of objects
$array = array();
if ($this->result->offsetExists($start)) {
// Import the class name
$class = $this->className;
// Set the end offset
$end = $this->result->offsetExists($end) ? $end : $this->count();
for ($i = $start; $i < $end; $i++) {
// Insert each object in the range
$array[] = new $class($this->result->offsetGet($i));
}
}
return $array;
}
/**
* Countable: count
*/
public function count() {
return $this->result->count();
}
/**
* Iterator: current
*/
public function current() {
if ($row = $this->result->current()) {
// Import class name
$class = $this->className;
$row = new $class($row);
}
return $row;
}
/**
* Iterator: key
*/
public function key() {
return $this->result->key();
}
/**
* Iterator: next
*/
public function next() {
return $this->result->next();
}
/**
* Iterator: rewind
*/
public function rewind() {
$this->result->rewind();
}
/**
* Iterator: valid
*/
public function valid() {
return $this->result->valid();
}
/**
* ArrayAccess: offsetExists
*/
public function offsetExists($offset) {
return $this->result->offsetExists($offset);
}
/**
* ArrayAccess: offsetGet
*/
public function offsetGet($offset) {
if ($this->result->offsetExists($offset)) {
// Import class name
$class = $this->className;
return new $class($this->result->offsetGet($offset));
}
}
/**
* ArrayAccess: offsetSet
*
* @throws DatabaseException
*/
public function offsetSet($offset, $value) {
throw new DatabaseException('database.result_read_only');
}
/**
* ArrayAccess: offsetUnset
*
* @throws DatabaseException
*/
public function offsetUnset($offset) {
throw new DatabaseException('database.result_read_only');
}
}