1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace idiorm\orm; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A result set class for working with collections of model instances |
7
|
|
|
* |
8
|
|
|
* @author Simon Holywell <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class IdiormResultSet implements \Countable, \IteratorAggregate, \ArrayAccess, \Serializable |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The current result set as an array |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $_results = array(); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Optionally set the contents of the result set by passing in array |
22
|
|
|
* |
23
|
|
|
* @param array $results |
24
|
|
|
*/ |
25
|
14 |
|
public function __construct(array $results = array()) |
26
|
|
|
{ |
27
|
14 |
|
$this->set_results($results); |
28
|
14 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set the contents of the result set by passing in array |
32
|
|
|
* |
33
|
|
|
* @param array $results |
34
|
|
|
*/ |
35
|
14 |
|
public function set_results(array $results) |
36
|
|
|
{ |
37
|
14 |
|
$this->_results = $results; |
38
|
14 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the current result set as an array |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
4 |
|
public function get_results() |
46
|
|
|
{ |
47
|
4 |
|
return $this->_results; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return the content of the result set |
52
|
|
|
* as an array of associative arrays. Column |
53
|
|
|
* names may optionally be supplied as arguments, |
54
|
|
|
* if so, only those keys will be returned. |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
1 |
|
public function as_array() |
59
|
|
|
{ |
60
|
1 |
|
$rows = array(); |
61
|
1 |
|
$args = func_get_args(); |
62
|
|
|
|
63
|
1 |
|
foreach ($this->_results as $key => $row) { |
64
|
|
|
if ( |
65
|
1 |
|
is_object($row) |
66
|
1 |
|
&& |
67
|
1 |
|
method_exists($row, 'as_array') |
68
|
1 |
|
&& |
69
|
1 |
|
is_callable(array($row, 'as_array')) |
70
|
1 |
|
) { |
71
|
1 |
|
$rows[] = call_user_func_array(array($row, 'as_array'), $args); |
72
|
1 |
|
} else { |
73
|
1 |
|
$rows[$key] = $row; |
74
|
|
|
} |
75
|
1 |
|
} |
76
|
|
|
|
77
|
1 |
|
return $rows; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the current result set as an json |
82
|
|
|
* |
83
|
|
|
* @param int $options |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
1 |
|
public function as_json($options = 0) |
88
|
|
|
{ |
89
|
1 |
|
return json_encode($this->get_results(), $options); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the number of records in the result set |
94
|
|
|
* |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
3 |
|
public function count() |
98
|
|
|
{ |
99
|
3 |
|
return count($this->_results); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get an iterator for this object. In this case it supports foreaching |
104
|
|
|
* over the result set. |
105
|
|
|
* |
106
|
|
|
* @return \ArrayIterator |
107
|
|
|
*/ |
108
|
3 |
|
public function getIterator() |
109
|
|
|
{ |
110
|
3 |
|
return new \ArrayIterator($this->_results); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* ArrayAccess |
115
|
|
|
* |
116
|
|
|
* @param int|string $offset |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
1 |
|
public function offsetExists($offset) |
121
|
|
|
{ |
122
|
1 |
|
return isset($this->_results[$offset]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* ArrayAccess |
127
|
|
|
* |
128
|
|
|
* @param int|string $offset |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
1 |
|
public function offsetGet($offset) |
133
|
|
|
{ |
134
|
1 |
|
return $this->_results[$offset]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* ArrayAccess |
139
|
|
|
* |
140
|
|
|
* @param int|string $offset |
141
|
|
|
* @param mixed $value |
142
|
|
|
*/ |
143
|
1 |
|
public function offsetSet($offset, $value) |
144
|
|
|
{ |
145
|
1 |
|
$this->_results[$offset] = $value; |
146
|
1 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* ArrayAccess |
150
|
|
|
* |
151
|
|
|
* @param int|string $offset |
152
|
|
|
*/ |
153
|
1 |
|
public function offsetUnset($offset) |
154
|
|
|
{ |
155
|
1 |
|
unset($this->_results[$offset]); |
156
|
1 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Serializable |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
1 |
|
public function serialize() |
164
|
|
|
{ |
165
|
1 |
|
return serialize($this->_results); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Serializable |
170
|
|
|
* |
171
|
|
|
* @param string $serialized |
172
|
|
|
* |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
1 |
|
public function unserialize($serialized) |
176
|
|
|
{ |
177
|
1 |
|
return unserialize($serialized); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Call a method on all models in a result set. This allows for method |
182
|
|
|
* chaining such as setting a property on all models in a result set or |
183
|
|
|
* any other batch operation across models. |
184
|
|
|
* |
185
|
|
|
* @example ORM::for_table('Widget')->find_many()->set('field', 'value')->save(); |
186
|
|
|
* |
187
|
|
|
* @param string $method |
188
|
|
|
* @param array $params |
189
|
|
|
* |
190
|
|
|
* @return $this |
191
|
|
|
* |
192
|
|
|
* @throws IdiormMethodMissingException |
193
|
|
|
*/ |
194
|
2 |
|
public function __call($method, $params = array()) |
195
|
|
|
{ |
196
|
2 |
|
foreach ($this->_results as $model) { |
197
|
2 |
View Code Duplication |
if (method_exists($model, $method)) { |
|
|
|
|
198
|
1 |
|
call_user_func_array(array($model, $method), $params); |
199
|
1 |
|
} else { |
200
|
1 |
|
throw new IdiormMethodMissingException("Method $method() does not exist in class " . get_class($this)); |
201
|
|
|
} |
202
|
1 |
|
} |
203
|
|
|
|
204
|
1 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.