This repository has been archived by the owner on Mar 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFinderTrait.php
348 lines (303 loc) · 10.6 KB
/
FinderTrait.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
namespace Synapse\Mapper;
use InvalidArgumentException;
use LogicException;
use Synapse\Entity\AbstractEntity;
use Synapse\Stdlib\Arr;
use Synapse\Entity\EntityIterator;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\PreparableSqlInterface;
use Zend\Db\Sql\Predicate\Like;
use Zend\Db\Sql\Predicate\NotLike;
use Zend\Db\Sql\Predicate\Operator;
use Zend\Db\Sql\Predicate\In;
use Zend\Db\Sql\Predicate\NotIn;
use Zend\Db\Sql\Predicate\IsNull;
use Zend\Db\Sql\Predicate\IsNotNull;
/**
* Use this trait to add find functionality to AbstractMappers.
*/
trait FinderTrait
{
/**
* Default maximum number of results to return if pagination is used
*
* @var integer
*/
protected $resultsPerPage = 50;
/**
* Set maximum number of results to return if pagination is enabled
*
* @param int $resultsPerPage
*/
public function setResultsPerPage($resultsPerPage)
{
$this->resultsPerPage = $resultsPerPage;
}
/**
* Find a single entity by specific field values
*
* @param array $wheres An array of where conditions in the format:
* ['column' => 'value'] or
* ['column', 'operator', 'value']
* @param array $options
* @return AbstractEntity|bool
*/
public function findBy(array $wheres, array $options = [])
{
$query = $this->getSqlObject()->select();
$this->setColumns($query, $options);
$wheres = $this->addJoins($query, $wheres, $options);
$this->addWheres($query, $wheres, $options);
if (Arr::get($options, 'order')) {
$this->setOrder($query, $options['order']);
}
return $this->executeAndGetResultsAsEntity($query);
}
/**
* Find a single entity by ID
*
* @param int|string $id Entity ID
* @return AbstractEntity|bool
*/
public function findById($id)
{
return $this->findBy(['id' => $id]);
}
/**
* Find all entities matching specific field values
*
* @param array $wheres An array of where conditions in the format:
* ['column' => 'value'] or
* ['column', 'operator', 'value']
* @param array $options Array of options for this request.
* May include 'order', 'page', or 'resultsPerPage'.
* @return EntityIterator AbstractEntity objects
* @throws LogicException If pagination enabled and no 'order' option specified.
*/
public function findAllBy(array $wheres, array $options = [])
{
$query = $this->getSqlObject()->select();
$this->setColumns($query, $options);
$wheres = $this->addJoins($query, $wheres, $options);
$this->addWheres($query, $wheres, $options);
$page = Arr::get($options, 'page');
if ($page && !Arr::get($options, 'order')) {
throw new LogicException('Must provide an ORDER BY if using pagination');
}
if (Arr::get($options, 'order')) {
$this->setOrder($query, $options['order']);
}
if ($page) {
$paginationData = $this->getPaginationData($query, $options);
// Set LIMIT and OFFSET
$query->limit($paginationData->getResultsPerPage());
$query->offset(($page - 1) * $paginationData->getResultsPerPage());
}
$entityIterator = $this->executeAndGetResultsAsEntityIterator($query);
if ($page) {
$entityIterator->setPaginationData($paginationData);
}
return $entityIterator;
}
/**
* Find all entities in this table
*
* @param array $options Array of options for this request
* @return array|EntityIterator Array of AbstractEntity objects
*/
public function findAll(array $options = [])
{
return $this->findAllBy([], $options);
}
/**
* Set the order on the given query
*
* Can specify order as [['column', 'direction'], ['column', 'direction']]
* or just ['column', 'direction'] or even [['column', 'direction'], 'column']
*
* @param Select $query
* @param array $order
*
* @return Select
*/
protected function setOrder(Select $query, array $order)
{
// Normalize to [['column', 'direction']] format if only one column
if (! is_array(Arr::get($order, 0))) {
$order = [$order];
}
foreach ($order as $key => $orderValue) {
if (is_array($orderValue)) {
// Column and direction
$query->order(
Arr::get($orderValue, 0).' '.Arr::get($orderValue, 1)
);
}
}
return $query;
}
/**
* Get data object with pagination data like page and page_count
*
* @param Select $query
* @param array $options
* @return PaginationData
*/
protected function getPaginationData(Select $query, array $options)
{
// Get pagination options
$page = (int) Arr::get($options, 'page');
if ($page < 1) {
$page = 1;
}
$resultsPerPage = Arr::get(
$options,
'resultsPerPage',
$this->resultsPerPage
);
// Get total results
$resultCount = $this->getQueryResultCount($query);
$pageCount = ceil($resultCount / $resultsPerPage);
return new PaginationData([
'page' => $page,
'page_count' => $pageCount,
'result_count' => $resultCount,
'results_per_page' => $resultsPerPage
]);
}
/**
* Get the count of results from a given query
*
* @param Select $query
* @return int
*/
protected function getQueryResultCount(Select $query)
{
$queryString = $this->getSqlObject()->getSqlStringForSqlObject($query, $this->dbAdapter->getPlatform());
$format = 'Select count(*) as `count` from (%s) as `query_count`';
$countQueryString = sprintf($format, $queryString);
$countQuery = $this->dbAdapter->query($countQueryString);
$result = $countQuery->execute()->current();
return (int) Arr::get($result, 'count');
}
/**
* Add where clauses to query
*
* @param PreparableSqlInterface $query
* @param array $wheres An array of where conditions in the format:
* ['column' => 'value'] or
* ['column', 'operator', 'value']
* @param array $options
* @return PreparableSqlInterface
* @throws InvalidArgumentException If a WHERE requirement is in an unsupported format.
*/
protected function addWheres(PreparableSqlInterface $query, array $wheres, array $options = [])
{
foreach ($wheres as $key => $where) {
if (is_array($where) && count($where) === 3) {
$leftOpRightSyntax = true;
$operator = $where[1];
switch ($operator) {
case '=':
$predicate = new Operator(
$where[0],
Operator::OP_EQ,
$where[2]
);
break;
case '!=':
$predicate = new Operator(
$where[0],
Operator::OP_NE,
$where[2]
);
break;
case '>':
$predicate = new Operator(
$where[0],
Operator::OP_GT,
$where[2]
);
break;
case '<':
$predicate = new Operator(
$where[0],
Operator::OP_LT,
$where[2]
);
break;
case '>=':
$predicate = new Operator(
$where[0],
Operator::OP_GTE,
$where[2]
);
break;
case '<=':
$predicate = new Operator(
$where[0],
Operator::OP_LTE,
$where[2]
);
break;
case 'LIKE':
$predicate = new Like($where[0], $where[2]);
break;
case 'NOT LIKE':
$predicate = new NotLike($where[0], $where[2]);
break;
case 'IN':
$predicate = new In($where[0], $where[2]);
break;
case 'NOT IN':
$predicate = new NotIn($where[0], $where[2]);
break;
case 'IS':
$predicate = new IsNull($where[0]);
break;
case 'IS NOT':
$predicate = new IsNotNull($where[0]);
break;
default:
$leftOpRightSyntax = false;
break;
}
if ($leftOpRightSyntax === false) {
$predicate = [$key => $where];
}
} else {
$predicate = [$key => $where];
}
$query->where($predicate);
}
return $query;
}
/**
* Add any necessary joins to the query.
*
* @param PreparableSqlInterface $query
* @param array $wheres
* @param array $options
* @return array Updated $wheres that may include
* fully qualified names.
*/
protected function addJoins($query, $wheres, $options = [])
{
// Override if joins are required
return $wheres;
}
/**
* Set the columns to be selected
*
* By default selects all of the columns for the table
* associated with the mapper
*
* @param PreparableSqlInterface $query
* @param array $options
*/
protected function setColumns($query, $options = [])
{
$query->columns(['*']);
}
}