|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebStream\Database; |
|
4
|
|
|
|
|
5
|
|
|
use WebStream\DI\Injector; |
|
|
|
|
|
|
6
|
|
|
use WebStream\Exception\Extend\CollectionException; |
|
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Statement; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Result |
|
11
|
|
|
* @author Ryuichi TANAKA. |
|
12
|
|
|
* @since 2013/12/14 |
|
13
|
|
|
* @version 0.7 |
|
14
|
|
|
*/ |
|
15
|
|
|
class Result implements \Iterator, \SeekableIterator, \ArrayAccess, \Countable |
|
16
|
|
|
{ |
|
17
|
|
|
use Injector; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Doctrine\DBAL\Statement ステートメント |
|
|
|
|
|
|
21
|
|
|
*/ |
|
22
|
|
|
private $stmt; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array<mixed> 列データ |
|
26
|
|
|
*/ |
|
27
|
|
|
private array $row; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array<mixed> キャッシュ化列データ |
|
31
|
|
|
*/ |
|
32
|
|
|
private array $rowCache; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var int インデックス位置 |
|
36
|
|
|
*/ |
|
37
|
|
|
private int $position; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* コンストラクタ |
|
41
|
|
|
* @param Statement $stmt ステートメントオブジェクト |
|
42
|
|
|
*/ |
|
43
|
44 |
|
public function __construct(Statement $stmt) |
|
44
|
|
|
{ |
|
45
|
44 |
|
$this->stmt = $stmt; |
|
|
|
|
|
|
46
|
44 |
|
$this->position = 0; |
|
47
|
44 |
|
$this->rowCache = []; |
|
48
|
44 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* デストラクタ |
|
52
|
|
|
*/ |
|
53
|
44 |
|
public function __destruct() |
|
54
|
|
|
{ |
|
55
|
44 |
|
$this->stmt = null; |
|
56
|
44 |
|
$this->rowCache = []; |
|
57
|
44 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Implements Countable#count |
|
61
|
|
|
* @return integer 結果件数 |
|
62
|
|
|
*/ |
|
63
|
|
|
public function count() |
|
64
|
|
|
{ |
|
65
|
|
|
if ($this->stmt === null) { |
|
66
|
|
|
$count = count($this->rowCache); |
|
67
|
|
|
} else { |
|
68
|
|
|
$count = $this->stmt->rowCount(); |
|
69
|
|
|
if ($count === 0) { |
|
70
|
|
|
$this->toArray(); |
|
71
|
|
|
$count = count($this->rowCache); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $count; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Implements SeekableIterator#seek |
|
80
|
|
|
* カーソル位置を移動する |
|
81
|
|
|
* @param mixed オフセット |
|
|
|
|
|
|
82
|
|
|
*/ |
|
83
|
|
|
public function seek($offset) |
|
84
|
|
|
{ |
|
85
|
|
|
if ($this->stmt !== null) { |
|
86
|
|
|
// Mysql does not support scrollable cursor. |
|
87
|
|
|
// but if statement to array, it is accessable. |
|
88
|
|
|
$this->toArray(); |
|
89
|
|
|
} |
|
90
|
|
|
if (array_key_exists($offset, $this->rowCache)) { |
|
91
|
|
|
return $this->rowCache[$offset]; |
|
92
|
|
|
} else { |
|
93
|
|
|
throw new \OutOfBoundsException("Current cursor is out of range: " . $offset); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Implements Iterator#current |
|
99
|
|
|
* 現在の要素を返却する |
|
100
|
|
|
* @return array<string> 列データ |
|
101
|
|
|
*/ |
|
102
|
|
|
public function current() |
|
103
|
|
|
{ |
|
104
|
|
|
if ($this->stmt === null) { |
|
105
|
|
|
return array_key_exists($this->position, $this->rowCache) ? $this->rowCache[$this->position] : null; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this->row; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Implements Iterator#key |
|
113
|
|
|
* 現在の要素のキーを返却する |
|
114
|
|
|
* @return integer キー |
|
115
|
|
|
*/ |
|
116
|
|
|
public function key() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->position; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Implements Iterator#next |
|
123
|
|
|
* 次の要素に進む |
|
124
|
|
|
*/ |
|
125
|
|
|
public function next() |
|
126
|
|
|
{ |
|
127
|
|
|
if ($this->stmt !== null) { |
|
128
|
|
|
$this->row = $this->stmt->fetch(\PDO::FETCH_ASSOC); |
|
129
|
|
|
} |
|
130
|
|
|
$this->position++; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Implements Iterator#rewind |
|
135
|
|
|
* イテレータを先頭に巻き戻す |
|
136
|
|
|
*/ |
|
137
|
|
|
public function rewind() |
|
138
|
|
|
{ |
|
139
|
|
|
if ($this->stmt !== null) { |
|
140
|
|
|
$this->row = $this->stmt->fetch(\PDO::FETCH_ASSOC, \PDO::FETCH_ORI_FIRST); |
|
141
|
|
|
} |
|
142
|
|
|
$this->position = 0; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Implements Iterator#valid |
|
147
|
|
|
* 現在位置が有効かどうかを調べる |
|
148
|
|
|
* @return boolean 有効かどうか |
|
149
|
|
|
*/ |
|
150
|
|
|
public function valid() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->row !== false; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Implements ArrayAccess#offsetExists |
|
157
|
|
|
* オフセットの位置に値が存在するかどうか返却する |
|
158
|
|
|
* @param $offset |
|
159
|
|
|
* @return boolean 値が存在するかどうか |
|
160
|
|
|
*/ |
|
161
|
|
|
public function offsetExists($offset) |
|
162
|
|
|
{ |
|
163
|
|
|
if ($this->stmt !== null) { |
|
164
|
|
|
$this->toArray(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return array_key_exists($offset, $this->rowCache); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Implements ArrayAccess#offsetGet |
|
172
|
|
|
* オフセットの位置の値を返却する |
|
173
|
|
|
* @param $offset |
|
174
|
|
|
* @return mixed 値 |
|
175
|
|
|
*/ |
|
176
|
16 |
|
public function offsetGet($offset) |
|
177
|
|
|
{ |
|
178
|
16 |
|
if ($this->stmt !== null) { |
|
179
|
16 |
|
$this->toArray(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
16 |
|
return $this->rowCache[$offset]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Implements ArrayAccess#offsetSet |
|
187
|
|
|
* オフセットの位置に値を設定する |
|
188
|
|
|
* @param mixed オフセット |
|
|
|
|
|
|
189
|
|
|
* @param mixed 値 |
|
190
|
|
|
*/ |
|
191
|
|
|
public function offsetSet($offset, $value) |
|
192
|
|
|
{ |
|
193
|
|
|
throw new CollectionException("Database results are read only."); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Implements ArrayAccess#offsetUnSet |
|
198
|
|
|
* オフセットの設定を解除する |
|
199
|
|
|
* @param mixed オフセット |
|
|
|
|
|
|
200
|
|
|
*/ |
|
201
|
|
|
public function offsetUnSet($offset) |
|
202
|
|
|
{ |
|
203
|
|
|
throw new CollectionException("Database results are read only."); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* 検索結果を全て配列として返却する |
|
208
|
|
|
* @return array<string> 検索結果 |
|
209
|
|
|
*/ |
|
210
|
28 |
|
public function toArray() |
|
211
|
|
|
{ |
|
212
|
28 |
|
$this->rowCache = $this->stmt->fetchAll(\PDO::FETCH_ASSOC); |
|
213
|
28 |
|
$this->logger->debug("All results to array and cached."); |
|
214
|
28 |
|
$this->stmt = null; |
|
215
|
|
|
|
|
216
|
28 |
|
return $this->rowCache; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* 検索結果をエンティティとして返却する |
|
221
|
|
|
* @param string $classpath |
|
222
|
|
|
* @return ResultEntity 検索結果 |
|
223
|
|
|
*/ |
|
224
|
16 |
|
public function toEntity(string $classpath) |
|
225
|
|
|
{ |
|
226
|
16 |
|
$resultEntity = new ResultEntity($this->stmt->getWrappedStatement(), $classpath); |
|
227
|
16 |
|
$resultEntity->inject('logger', $this->logger)->initialize(); |
|
228
|
|
|
|
|
229
|
16 |
|
return $resultEntity; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths