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