1
|
|
|
<?php |
2
|
|
|
namespace Kir\MySQL\Builder\Helpers; |
3
|
|
|
|
4
|
|
|
use Closure; |
5
|
|
|
use Iterator; |
6
|
|
|
use Kir\MySQL\Builder\QueryStatement; |
7
|
|
|
use Kir\MySQL\Builder\RunnableSelect; |
8
|
|
|
use PDO; |
9
|
|
|
|
10
|
|
|
class YieldPolyfillIterator implements Iterator { |
11
|
|
|
/** @var RunnableSelect */ |
12
|
|
|
private $select; |
13
|
|
|
/** @var Closure|null */ |
14
|
|
|
private $callback; |
15
|
|
|
/** @var bool */ |
16
|
|
|
private $preserveTypes; |
17
|
|
|
/** @var null */ |
18
|
|
|
private $stmt = null; |
19
|
|
|
/** @var Closure */ |
20
|
|
|
private $statementFactory; |
21
|
|
|
/** @var mixed */ |
22
|
|
|
private $currentItem; |
23
|
|
|
/** @var int */ |
24
|
|
|
private $idx = -1; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param RunnableSelect $select |
28
|
|
|
* @param Closure|null $callback |
29
|
|
|
* @param bool $preserveTypes |
30
|
|
|
* @param callable $statementFactory |
31
|
|
|
*/ |
32
|
|
|
public function __construct(RunnableSelect $select, Closure $callback = null, $preserveTypes, $statementFactory) { |
33
|
|
|
$this->select = $select; |
34
|
|
|
$this->callback = $callback; |
35
|
|
|
$this->preserveTypes = $preserveTypes; |
36
|
|
|
$this->statementFactory = $statementFactory; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
*/ |
41
|
|
|
public function __destruct() { |
42
|
|
|
$this->closeCursor(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
public function current() { |
49
|
|
|
$row = $this->currentItem; |
50
|
|
|
if($this->preserveTypes) { |
51
|
|
|
$columnDefinitions = FieldTypeProvider::getFieldTypes($this->getStmt()); |
|
|
|
|
52
|
|
|
$row = FieldValueConverter::convertValues($row, $columnDefinitions); |
53
|
|
|
} |
54
|
|
|
$callback = $this->callback; |
55
|
|
|
if($callback !== null) { |
56
|
|
|
$result = $callback($row); |
57
|
|
|
if($result instanceof DBIgnoreRow) { |
|
|
|
|
58
|
|
|
// Do nothing in this case |
59
|
|
|
} elseif($result !== null) { |
60
|
|
|
return $result; |
61
|
|
|
} else { |
62
|
|
|
return $row; |
63
|
|
|
} |
64
|
|
|
} else { |
65
|
|
|
return $row; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
*/ |
71
|
|
|
public function next() { |
72
|
|
|
$this->idx++; |
73
|
|
|
$this->currentItem = $this->getStmt()->fetch(PDO::FETCH_ASSOC); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function key() { |
80
|
|
|
return $this->idx; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
|
|
public function valid() { |
87
|
|
|
$result = !!$this->currentItem; |
88
|
|
|
if(!$result) { |
89
|
|
|
$this->closeCursor(); |
90
|
|
|
} |
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
*/ |
96
|
|
|
public function rewind() { |
97
|
|
|
if($this->stmt !== null) { |
98
|
|
|
throw new \Exception("It's not possible to rewind this iterator"); |
99
|
|
|
} |
100
|
|
|
$this->stmt = call_user_func($this->statementFactory); |
101
|
|
|
$this->idx = -1; |
102
|
|
|
$this->next(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return QueryStatement |
107
|
|
|
*/ |
108
|
|
|
private function getStmt() { |
109
|
|
|
if($this->stmt === null) { |
110
|
|
|
$this->rewind(); |
111
|
|
|
} |
112
|
|
|
return $this->stmt; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
*/ |
117
|
|
|
private function closeCursor() { |
118
|
|
|
if($this->stmt instanceof QueryStatement) { |
119
|
|
|
$this->stmt->closeCursor(); |
120
|
|
|
} |
121
|
|
|
$this->stmt = null; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..