This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
/
QueryBuilder.php
310 lines (263 loc) · 7.47 KB
/
QueryBuilder.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
namespace Spy\Timeline\Driver\QueryBuilder;
use Spy\Timeline\Driver\ActionManagerInterface;
use Spy\Timeline\Driver\QueryBuilder\Criteria\CriteriaInterface;
use Spy\Timeline\Driver\QueryBuilder\Criteria\Operator;
use Spy\Timeline\Model\Component;
use Spy\Timeline\Model\ComponentInterface;
class QueryBuilder
{
/**
* @var array
*/
protected $subjects = array();
/**
* @var integer
*/
protected $page = 1;
/**
* @var integer
*/
protected $maxPerPage = 10;
/**
* @var array
*/
protected $criterias;
/**
* @var array(<string>$field, <string>way)
*/
protected $sort;
/**
* @var QueryBuilderFactory
*/
protected $factory;
/**
* @var array
*/
protected static $fieldLocation = array(
'context' => 'timeline',
'createdAt' => 'action',
'verb' => 'action',
'type' => 'actionComponent',
'text' => 'actionComponent',
'model' => 'component',
'identifier' => 'component',
);
/**
* @param QueryBuilderFactory $factory factory
*/
public function __construct(QueryBuilderFactory $factory = null)
{
if (null === $factory) {
$factory = new QueryBuilderFactory();
}
$this->factory = $factory;
}
/**
* @return CriteriaInterface
*/
public function logicalAnd()
{
return $this->createNewOperator(Operator::TYPE_AND, func_get_args());
}
/**
* @return CriteriaInterface
*/
public function logicalOr()
{
return $this->createNewOperator(Operator::TYPE_OR, func_get_args());
}
/**
* @param string $field field
*/
public function field($field)
{
if (!in_array($field, $this->getAvailableFields())) {
throw new \InvalidArgumentException(sprintf('Field "%s" not supported, prefer: %s', $field, implode(', ', $this->getAvailableFields())));
}
return $this->factory
->createAsserter()
->field($field)
;
}
/**
* @param string $type type
* @param array $args args
*
* @return Operator
*/
public function createNewOperator($type, array $args)
{
if (empty($args) || count($args) < 2) {
throw new \InvalidArgumentException(__METHOD__.' accept minimum 2 arguments');
}
return $this->factory
->createOperator()
->setType($type)
->setCriterias($args)
;
}
/**
* @param ComponentInterface $component component
*
* @return QueryBuilder
*/
public function addSubject(ComponentInterface $component)
{
$this->subjects[$component->getHash()] = $component;
return $this;
}
/**
* @return array
*/
public function getSubjects()
{
return $this->subjects;
}
/**
* @param CriteriaInterface $criteria criteria
*
* @return QueryBuilder
*/
public function setCriterias(CriteriaInterface $criteria)
{
$this->criterias = $criteria;
return $this;
}
/**
* @return CriteriaInterface|null
*/
public function getCriterias()
{
return $this->criterias;
}
/**
* @param integer $page page
*
* @return QueryBuilder
*/
public function setPage($page)
{
$this->page = (int) $page;
return $this;
}
/**
* @param string $maxPerPage maxPerPage
*
* @return QueryBuilder
*/
public function setMaxPerPage($maxPerPage)
{
$this->maxPerPage = (int) $maxPerPage;
return $this;
}
/**
* @param string $field field
* @param string $order order
*
* @return QueryBuilder
*/
public function orderBy($field, $order)
{
if (!in_array($field, $this->getAvailableFields())) {
throw new \InvalidArgumentException(sprintf('Field "%s" not supported, prefer: %s', $field, implode(', ', $this->getAvailableFields())));
}
if (!in_array($order, array('ASC', 'DESC'))) {
throw new \InvalidArgumentException(sprintf('Order "%s" not supported, prefer: ASC or DESC', $order));
}
$this->sort = array($field, $order);
return $this;
}
/**
* @param string $field field
*
* @return string
*/
public static function getFieldLocation($field)
{
return self::$fieldLocation[$field];
}
/**
* @return array
*/
public function getAvailableFields()
{
return array(
'context',
'verb',
'createdAt',
'type',
'text',
'model',
'identifier',
);
}
/**
* @param array $data data
* @param ActionManagerInterface $actionManager actionManager
* @throws \Exception
* @return $this
*/
public function fromArray(array $data, ActionManagerInterface $actionManager = null)
{
if (isset($data['criterias']) && isset($data['criterias']['type'])) {
$criterias = $data['criterias'];
$type = $criterias['type'];
if ('operator' == $type) {
$method = 'createOperatorFromArray';
} elseif ('expr' == $type) {
$method = 'createAsserterFromArray';
} else {
throw new \Exception('Invalid array, cannot be unserialized');
}
$this->setCriterias($this->factory->{$method}($criterias));
}
if (isset($data['page'])) {
$this->setPage($data['page']);
}
if (isset($data['max_per_page'])) {
$this->setMaxPerPage($data['max_per_page']);
}
if (isset($data['sort'])) {
list ($field, $order) = $data['sort'];
$this->orderBy($field, $order);
}
if (isset($data['subject']) && !empty($data['subject'])) {
$subjects = $data['subject'];
if (!$actionManager) {
throw new \Exception('Please provide the actionManager to retrieve components');
}
$components = $actionManager->findComponents($subjects);
if (count($components) != count($subjects)) {
foreach ($components as $component) {
// remove existing components from subjects to keep only new one components
unset($subjects[array_search($component->getHash(), $subjects)]);
}
// create new components
foreach ($subjects as $subject) {
list ($model, $identifier) = explode('#', $subject);
$components[] = $actionManager->createComponent($model, unserialize($identifier));
}
}
foreach ($components as $component) {
$this->addSubject($component);
}
}
return $this;
}
/**
* @return array
*/
public function toArray()
{
return array(
'subject' => array_values(
array_map(function ($v) { return $v->getHash(); }, $this->subjects)
),
'page' => $this->page,
'max_per_page' => $this->maxPerPage,
'criterias' => $this->criterias ? $this->criterias->toArray() : null,
'sort' => $this->sort,
);
}
}