|
1
|
|
|
<?php namespace Bedard\Shop\Classes; |
|
2
|
|
|
|
|
3
|
|
|
class Repository |
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* Order the results. |
|
7
|
|
|
* |
|
8
|
|
|
* @param \October\Rain\Database\Builder $query |
|
9
|
|
|
* @param array $options |
|
10
|
|
|
* @return void |
|
11
|
|
|
*/ |
|
12
|
|
|
protected function orderResults(&$query, array $options) |
|
13
|
|
|
{ |
|
14
|
|
|
if ( |
|
15
|
|
|
array_key_exists('order', $options) && |
|
16
|
|
|
is_string($options['order']) |
|
17
|
|
|
) { |
|
18
|
|
|
$orderParam = array_map('trim', explode(',', $options['order'])); |
|
19
|
|
|
$column = $orderParam[0]; |
|
20
|
|
|
$direction = count($orderParam) > 1 ? $orderParam[1] : 'asc'; |
|
21
|
|
|
|
|
22
|
|
|
$query->orderBy($column, $direction); |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Paginate the results. |
|
28
|
|
|
* |
|
29
|
|
|
* @param \October\Rain\Database\Builder $query |
|
30
|
|
|
* @param array $options |
|
31
|
|
|
* @param int $total |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function paginateResults(&$query, array $options, $total) |
|
35
|
|
|
{ |
|
36
|
|
|
if (array_key_exists('skip', $options)) { |
|
37
|
|
|
$query->skip((int) $options['skip']); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (array_key_exists('take', $options)) { |
|
41
|
|
|
$query->take((int) $options['take']); |
|
42
|
|
|
} else { |
|
43
|
|
|
$query->take($total); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Select specific columns. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \October\Rain\Database\Builder $query |
|
51
|
|
|
* @param array $options |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function selectColumns(&$query, array $options) |
|
55
|
|
|
{ |
|
56
|
|
|
if ( |
|
57
|
|
|
array_key_exists('columns', $options) && |
|
58
|
|
|
is_array($options['columns']) |
|
59
|
|
|
) { |
|
60
|
|
|
$query->addSelect($options['columns']); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Eager load related models. |
|
66
|
|
|
* |
|
67
|
|
|
* @param \October\Rain\Database\Builder $query |
|
68
|
|
|
* @param array $options |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function withRelationships(&$query, array $options) |
|
72
|
|
|
{ |
|
73
|
|
|
if ( |
|
74
|
|
|
array_key_exists('relationships', $options) && |
|
75
|
|
|
is_array($options['relationships']) |
|
76
|
|
|
) { |
|
77
|
|
|
foreach ($options['relationships'] as $relationship) { |
|
78
|
|
|
$query->with([ |
|
79
|
|
|
$relationship => function ($model) { |
|
80
|
|
|
// @todo: add controls to select columns of a relationship |
|
81
|
|
|
$model->select('*'); |
|
82
|
|
|
}, |
|
83
|
|
|
]); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|