1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Windstorm; |
4
|
|
|
|
5
|
|
|
use Rougin\Windstorm\MixedInterface; |
6
|
|
|
use Rougin\Windstorm\QueryInterface; |
7
|
|
|
use Rougin\Windstorm\ResultInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Query Repository |
11
|
|
|
* |
12
|
|
|
* @package Windstorm |
13
|
|
|
* @author Rougin Gutib <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class QueryRepository |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \Rougin\Windstorm\ResultInterface |
19
|
|
|
*/ |
20
|
|
|
protected $result; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Rougin\Windstorm\MappingInterface |
24
|
|
|
*/ |
25
|
|
|
protected $mapping; |
26
|
|
|
|
27
|
|
|
protected $mixed = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \Rougin\Windstorm\QueryInterface |
31
|
|
|
*/ |
32
|
|
|
protected $query; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initializes the repository instance. |
36
|
|
|
* |
37
|
|
|
* @param \Rougin\Windstorm\QueryInterface $query |
38
|
|
|
* @param \Rougin\Windstorm\ResultInterface $result |
39
|
|
|
*/ |
40
|
57 |
|
public function __construct(QueryInterface $query, ResultInterface $result) |
41
|
|
|
{ |
42
|
57 |
|
$this->result = $result; |
43
|
|
|
|
44
|
57 |
|
$this->query = $query; |
45
|
57 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns a number of affected rows. |
49
|
|
|
* |
50
|
|
|
* @return integer |
51
|
|
|
*/ |
52
|
21 |
|
public function affected() |
53
|
|
|
{ |
54
|
21 |
|
return $this->execute($this->query)->affected(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Executes the result against query instance. |
59
|
|
|
* |
60
|
|
|
* @param \Rougin\Windstorm\QueryInterface $query |
61
|
|
|
* @return \Rougin\Windstorm\ResultInterface |
62
|
|
|
*/ |
63
|
51 |
|
public function execute(QueryInterface $query) |
64
|
|
|
{ |
65
|
51 |
|
return $this->result->execute($query); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the first row from result. |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
9 |
|
public function first() |
74
|
|
|
{ |
75
|
9 |
|
if ($this->mixed !== null) |
76
|
6 |
|
{ |
77
|
3 |
|
return current($this->items()); |
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
$item = $this->execute($this->query)->first(); |
81
|
|
|
|
82
|
6 |
|
if ($this->mapping) |
83
|
4 |
|
{ |
84
|
3 |
|
return $this->mapping->map($item); |
85
|
|
|
} |
86
|
|
|
|
87
|
3 |
|
return $item; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns all items from the result. |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
24 |
|
public function items() |
96
|
|
|
{ |
97
|
24 |
|
if ($this->mixed !== null) |
98
|
16 |
|
{ |
99
|
3 |
|
$result = $this->combine(); |
100
|
2 |
|
} |
101
|
|
|
else |
102
|
|
|
{ |
103
|
21 |
|
$result = $this->execute($this->query)->items(); |
104
|
|
|
} |
105
|
|
|
|
106
|
24 |
|
if ($this->mapping === null) |
107
|
16 |
|
{ |
108
|
9 |
|
return $result; |
109
|
|
|
} |
110
|
|
|
|
111
|
15 |
|
foreach ($result as $key => $item) |
112
|
|
|
{ |
113
|
15 |
|
$result[$key] = $this->mapping->map($item); |
114
|
10 |
|
} |
115
|
|
|
|
116
|
15 |
|
return $result; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Sets the mapping instance. |
121
|
|
|
* |
122
|
|
|
* @param \Rougin\Windstorm\MappingInterface $mapping |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
42 |
|
public function map(MappingInterface $mapping) |
126
|
|
|
{ |
127
|
42 |
|
$this->mapping = $mapping; |
128
|
|
|
|
129
|
42 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns the query instance. |
134
|
|
|
* |
135
|
|
|
* @return \Rougin\Windstorm\QueryRepository |
136
|
|
|
*/ |
137
|
3 |
|
public function query() |
138
|
|
|
{ |
139
|
3 |
|
return clone $this->query; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Mutates the specified query instance. |
144
|
|
|
* |
145
|
|
|
* @param \Rougin\Windstorm\MutatorInterface $mutator |
146
|
|
|
* @return self |
147
|
|
|
*/ |
148
|
51 |
|
public function set(MutatorInterface $mutator) |
149
|
|
|
{ |
150
|
51 |
|
$query = $mutator->set($this->query); |
151
|
|
|
|
152
|
51 |
|
if ($query instanceof MixedInterface) |
153
|
34 |
|
{ |
154
|
3 |
|
$this->mixed = $query; |
155
|
|
|
|
156
|
3 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
48 |
|
$this->query = $query; |
160
|
|
|
|
161
|
48 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
3 |
|
public function combine() |
165
|
|
|
{ |
166
|
3 |
|
$parent = $this->execute($this->mixed)->items(); |
167
|
|
|
|
168
|
3 |
|
$ids = array(); |
169
|
|
|
|
170
|
3 |
|
foreach ($parent as $item) |
171
|
|
|
{ |
172
|
3 |
|
$ids[] = (integer) $item[$this->mixed->primary()]; |
173
|
2 |
|
} |
174
|
|
|
|
175
|
3 |
|
foreach ($this->mixed->all() as $field => $child) |
176
|
|
|
{ |
177
|
3 |
|
$children = $child->where($child->column())->in($ids); |
178
|
|
|
|
179
|
3 |
|
$children = $this->execute($children)->items(); |
180
|
|
|
|
181
|
3 |
|
foreach ($parent as $index => $item) |
182
|
|
|
{ |
183
|
3 |
|
$parent[$index][$field] = array(); |
184
|
|
|
|
185
|
3 |
|
foreach ($children as $key => $value) |
186
|
|
|
{ |
187
|
3 |
|
if ($item[$this->mixed->primary()] === $value[$child->foreign()]) |
188
|
2 |
|
{ |
189
|
3 |
|
$parent[$index][$field][] = $value; |
190
|
|
|
|
191
|
3 |
|
unset($children[$key]); |
192
|
2 |
|
} |
193
|
2 |
|
} |
194
|
2 |
|
} |
195
|
2 |
|
} |
196
|
|
|
|
197
|
3 |
|
return $parent; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|