1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Windstorm\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Rougin\Windstorm\MixedInterface; |
7
|
|
|
use Rougin\Windstorm\QueryInterface; |
8
|
|
|
use Rougin\Windstorm\ResultInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Result |
12
|
|
|
* |
13
|
|
|
* @package Windstorm |
14
|
|
|
* @author Rougin Gutib <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class Result implements ResultInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var integer |
20
|
|
|
*/ |
21
|
|
|
protected $affected = 0; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Rougin\Windstorm\MixedInterface|null |
25
|
|
|
*/ |
26
|
|
|
protected $mixed = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Doctrine\DBAL\Connection |
30
|
|
|
*/ |
31
|
|
|
protected $connection; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var integer |
35
|
|
|
*/ |
36
|
|
|
protected $fetch = \PDO::FETCH_ASSOC; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Doctrine\DBAL\Driver\Statement |
40
|
|
|
*/ |
41
|
|
|
protected $result; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Initializes the result instance. |
45
|
|
|
* |
46
|
|
|
* @param \Doctrine\DBAL\Connection $connection |
47
|
|
|
*/ |
48
|
36 |
|
public function __construct(Connection $connection) |
49
|
|
|
{ |
50
|
36 |
|
$this->connection = $connection; |
51
|
36 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns a number of affected rows. |
55
|
|
|
* |
56
|
|
|
* @return integer |
57
|
|
|
*/ |
58
|
12 |
|
public function affected() |
59
|
|
|
{ |
60
|
12 |
|
return $this->affected; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns a result from a query instance. |
65
|
|
|
* |
66
|
|
|
* @param \Rougin\Windstorm\QueryInterface $query |
67
|
|
|
* @return self |
68
|
|
|
*/ |
69
|
36 |
|
public function execute(QueryInterface $query) |
70
|
|
|
{ |
71
|
36 |
|
if ($query->type() !== QueryInterface::TYPE_SELECT) |
72
|
24 |
|
{ |
73
|
12 |
|
$this->affected = $this->response($query); |
|
|
|
|
74
|
|
|
|
75
|
12 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
24 |
|
$this->result = $this->response($query); |
79
|
|
|
|
80
|
24 |
|
if ($query instanceof MixedInterface) |
81
|
16 |
|
{ |
82
|
6 |
|
$this->mixed = $query; |
83
|
4 |
|
} |
84
|
|
|
|
85
|
24 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the first row from result. |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
6 |
|
public function first() |
94
|
|
|
{ |
95
|
6 |
|
$result = $this->result->fetch($this->fetch); |
96
|
|
|
|
97
|
6 |
|
if ($this->mixed) |
98
|
4 |
|
{ |
99
|
3 |
|
$items = array($result); |
100
|
|
|
|
101
|
3 |
|
$items = $this->mixed($items); |
102
|
|
|
|
103
|
3 |
|
return current($items); |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
return $result; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns all items from the result. |
111
|
|
|
* |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
18 |
|
public function items() |
115
|
|
|
{ |
116
|
18 |
|
$items = $this->result->fetchAll($this->fetch); |
117
|
|
|
|
118
|
18 |
|
if ($this->mixed) |
119
|
12 |
|
{ |
120
|
3 |
|
return $this->mixed($items); |
121
|
|
|
} |
122
|
|
|
|
123
|
15 |
|
return $items; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Appends children output to original result. |
128
|
|
|
* |
129
|
|
|
* @param array $items |
130
|
|
|
* @param array $result |
131
|
|
|
* @param string $primary |
132
|
|
|
* @param string $foreign |
133
|
|
|
* @param string $field |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
6 |
|
protected function append($items, $result, $primary, $foreign, $field) |
137
|
|
|
{ |
138
|
6 |
|
foreach ($items as $key => $item) |
139
|
|
|
{ |
140
|
6 |
|
$items[$key][$field] = array(); |
141
|
|
|
|
142
|
6 |
|
foreach ($result as $child) |
143
|
|
|
{ |
144
|
6 |
|
if ($child[$foreign] === $item[$primary]) |
145
|
4 |
|
{ |
146
|
6 |
|
$items[$key][$field][] = $child; |
147
|
4 |
|
} |
148
|
4 |
|
} |
149
|
4 |
|
} |
150
|
|
|
|
151
|
6 |
|
return $items; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Appends result from mixed query to main result. |
156
|
|
|
* |
157
|
|
|
* @param array $items |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
6 |
|
protected function mixed($items) |
161
|
|
|
{ |
162
|
6 |
|
foreach ($this->mixed->children() as $child) |
|
|
|
|
163
|
|
|
{ |
164
|
6 |
|
$foreign = $child->foreign(); |
165
|
|
|
|
166
|
6 |
|
$field = $child->field(); |
167
|
|
|
|
168
|
6 |
|
$primary = $child->primary(); |
169
|
|
|
|
170
|
6 |
|
$ids = $this->identities($items, $primary); |
|
|
|
|
171
|
|
|
|
172
|
6 |
|
$stmt = $this->response($child->query()->where($foreign)->in($ids)); |
173
|
|
|
|
174
|
6 |
|
$result = $stmt->fetchAll($this->fetch); |
175
|
|
|
|
176
|
6 |
|
$items = $this->append($items, $result, $primary, $foreign, $field); |
177
|
4 |
|
} |
178
|
|
|
|
179
|
6 |
|
return $items; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Returns the values based of specified key. |
184
|
|
|
* |
185
|
|
|
* @param string $items |
186
|
|
|
* @param string $key |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
6 |
|
protected function identities($items, $key) |
190
|
|
|
{ |
191
|
6 |
|
$ids = array(); |
192
|
|
|
|
193
|
6 |
|
foreach ($items as $item) |
|
|
|
|
194
|
|
|
{ |
195
|
6 |
|
$ids[] = $item[$key]; |
196
|
4 |
|
} |
197
|
|
|
|
198
|
6 |
|
return $ids; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Executes the query from the connection instance. |
203
|
|
|
* |
204
|
|
|
* @param \Rougin\Windstorm\QueryInterface $query |
205
|
|
|
* @return \Doctrine\DBAL\Driver\Statement|null |
206
|
|
|
*/ |
207
|
36 |
|
protected function response(QueryInterface $query) |
208
|
|
|
{ |
209
|
36 |
|
$bindings = $query->bindings(); |
210
|
|
|
|
211
|
36 |
|
$sql = (string) $query->sql(); |
212
|
|
|
|
213
|
36 |
|
if ($query->type() === QueryInterface::TYPE_SELECT) |
214
|
24 |
|
{ |
215
|
24 |
|
return $this->connection->executeQuery($sql, $bindings); |
216
|
|
|
} |
217
|
|
|
|
218
|
12 |
|
return $this->connection->executeUpdate($sql, $bindings); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
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..