1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class DBResult |
4
|
|
|
* |
5
|
|
|
* @filesource DBResult.php |
6
|
|
|
* @created 23.05.2017 |
7
|
|
|
* @package chillerlan\Database |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Database; |
14
|
|
|
|
15
|
|
|
use ArrayAccess, Countable, Iterator; |
16
|
|
|
use chillerlan\Database\Traits\{Enumerable, Magic}; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @property int $length |
20
|
|
|
* |
21
|
|
|
* each($func [, $fieldname) |
22
|
|
|
*/ |
23
|
|
|
class DBResult implements Iterator, ArrayAccess, Countable{ |
24
|
|
|
use Enumerable, Magic; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $array = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $offset = 0; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* DBResult constructor. |
38
|
|
|
* |
39
|
|
|
* @param \Traversable|\stdClass|array|null $data |
40
|
|
|
* |
41
|
|
|
* @throws \chillerlan\Database\DBException |
42
|
|
|
*/ |
43
|
|
|
public function __construct($data = null){ |
44
|
|
|
|
45
|
|
|
if(is_null($data)){ |
46
|
|
|
$data = []; |
47
|
|
|
} |
48
|
|
|
else if($data instanceof \Traversable){ |
49
|
|
|
$data = iterator_to_array($data); |
50
|
|
|
} |
51
|
|
|
else if(!$data instanceof \stdClass && !is_array($data)){ |
52
|
|
|
throw new DBException('invalid data'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
foreach($data as $k => $v){ |
|
|
|
|
56
|
|
|
$this->offsetSet($k, $v); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->offset = 0; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param \chillerlan\Database\DBResult $DBResult |
64
|
|
|
* |
65
|
|
|
* @return \chillerlan\Database\DBResult |
66
|
|
|
*/ |
67
|
|
|
public function __merge(DBResult $DBResult){ |
68
|
|
|
$this->array = array_merge($this->array, $DBResult->__toArray()); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/********* |
75
|
|
|
* magic * |
76
|
|
|
*********/ |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
protected function magic_get_length():int{ |
82
|
|
|
return $this->count(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/*************** |
87
|
|
|
* ArrayAccess * |
88
|
|
|
***************/ |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param int|string $offset |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function offsetExists($offset):bool{ |
96
|
|
|
return isset($this->array[$offset]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param int|string $offset |
101
|
|
|
* |
102
|
|
|
* @return \chillerlan\Database\DBResultRow|mixed|null |
103
|
|
|
*/ |
104
|
|
|
public function offsetGet($offset){ |
105
|
|
|
return $this->array[$offset] ?? null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param int|string $offset |
110
|
|
|
* @param array $value |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function offsetSet($offset, $value){ |
|
|
|
|
115
|
|
|
|
116
|
|
|
if(is_null($offset)){ |
117
|
|
|
$this->array[] = new DBResultRow($value); |
118
|
|
|
} |
119
|
|
|
else{ |
120
|
|
|
$this->array[$offset] = new DBResultRow($value); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param int|string $offset |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function offsetUnset($offset){ |
131
|
|
|
unset($this->array[$offset]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/************* |
136
|
|
|
* Countable * |
137
|
|
|
*************/ |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return int |
141
|
|
|
*/ |
142
|
|
|
public function count():int{ |
143
|
|
|
return count($this->array); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/************ |
148
|
|
|
* Iterator * |
149
|
|
|
************/ |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return \chillerlan\Database\DBResultRow|mixed |
153
|
|
|
*/ |
154
|
|
|
public function current(){ |
155
|
|
|
return $this->offsetGet($this->offset); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return int |
160
|
|
|
*/ |
161
|
|
|
public function key():int{ |
162
|
|
|
return $this->offset; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function valid():bool{ |
169
|
|
|
return $this->offsetExists($this->offset); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
public function next(){ |
176
|
|
|
$this->offset++; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return void |
181
|
|
|
*/ |
182
|
|
|
public function rewind(){ |
183
|
|
|
$this->offset = 0; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.