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