1
|
|
|
<?php namespace Anomaly\Streams\Platform\Model; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface; |
4
|
|
|
use Anomaly\Streams\Platform\Entry\EntryPresenter; |
5
|
|
|
use Anomaly\Streams\Platform\Search\SearchCriteria; |
6
|
|
|
use Anomaly\Streams\Platform\Support\Collection; |
7
|
|
|
use Anomaly\Streams\Platform\Support\Decorator; |
8
|
|
|
use Anomaly\Streams\Platform\Support\Presenter; |
9
|
|
|
use Anomaly\Streams\Platform\Traits\Hookable; |
10
|
|
|
use Illuminate\Database\Eloquent\Builder; |
11
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class EloquentCriteria |
15
|
|
|
* |
16
|
|
|
* @link http://pyrocms.com/ |
17
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
18
|
|
|
* @author Ryan Thompson <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class EloquentCriteria |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
use Hookable; |
24
|
|
|
use DispatchesJobs; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Additional available methods. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $available = [ |
32
|
|
|
'whereBetween', |
33
|
|
|
'whereNotBetween', |
34
|
|
|
'whereIn', |
35
|
|
|
'whereNotIn', |
36
|
|
|
'whereNull', |
37
|
|
|
'whereNotNull', |
38
|
|
|
'whereDate', |
39
|
|
|
'whereMonth', |
40
|
|
|
'whereDay', |
41
|
|
|
'whereYear', |
42
|
|
|
'whereColumn', |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Safe builder methods. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $disabled = [ |
51
|
|
|
'delete', |
52
|
|
|
'update', |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The query builder. |
57
|
|
|
* |
58
|
|
|
* @var Builder|\Illuminate\Database\Query\Builder |
59
|
|
|
*/ |
60
|
|
|
protected $query; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the get method. |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $method; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Create a new EntryCriteria instance. |
71
|
|
|
* |
72
|
|
|
* @param Builder $query |
73
|
|
|
* @param string $method |
74
|
|
|
*/ |
75
|
|
|
public function __construct(Builder $query, $method = 'get') |
76
|
|
|
{ |
77
|
|
|
$this->query = $query; |
78
|
|
|
$this->method = $method; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the paginated entries. |
83
|
|
|
* |
84
|
|
|
* @param int $perPage |
85
|
|
|
* @param array $columns |
86
|
|
|
* @param string $pageName |
87
|
|
|
* @return array|\ArrayAccess|\IteratorAggregate|Presenter |
88
|
|
|
*/ |
89
|
|
|
public function paginate($perPage = 15, array $columns = ['*'], $pageName = 'page') |
90
|
|
|
{ |
91
|
|
|
return (new Decorator())->decorate($this->query->paginate($perPage, $columns, $pageName)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return a new search criteria. |
96
|
|
|
* |
97
|
|
|
* @param string $term |
98
|
|
|
* @return SearchCriteria |
99
|
|
|
*/ |
100
|
|
|
public function search($term) |
101
|
|
|
{ |
102
|
|
|
return new SearchCriteria( |
103
|
|
|
$this->query->getModel()->search($term), |
|
|
|
|
104
|
|
|
$this->query->getModel() |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the entries. |
110
|
|
|
* |
111
|
|
|
* @param array $columns |
112
|
|
|
* @return Collection|Presenter|EntryPresenter |
113
|
|
|
*/ |
114
|
|
|
public function get(array $columns = ['*']) |
115
|
|
|
{ |
116
|
|
|
return (new Decorator())->decorate($this->query->{$this->method}($columns)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the entry count. |
121
|
|
|
* |
122
|
|
|
* @param array $columns |
123
|
|
|
* @return int |
124
|
|
|
*/ |
125
|
|
|
public function count(array $columns = ['*']) |
126
|
|
|
{ |
127
|
|
|
return (new Decorator())->decorate($this->query->count($columns)); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the aggregate sum. |
132
|
|
|
* |
133
|
|
|
* @param $column |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
|
|
public function sum($column) |
137
|
|
|
{ |
138
|
|
|
return (new Decorator())->decorate($this->query->sum($column)); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the aggregate max. |
143
|
|
|
* |
144
|
|
|
* @param $column |
145
|
|
|
* @return int |
146
|
|
|
*/ |
147
|
|
|
public function max($column) |
148
|
|
|
{ |
149
|
|
|
return (new Decorator())->decorate($this->query->max($column)); |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the aggregate min. |
154
|
|
|
* |
155
|
|
|
* @param $column |
156
|
|
|
* @return int |
157
|
|
|
*/ |
158
|
|
|
public function min($column) |
159
|
|
|
{ |
160
|
|
|
return (new Decorator())->decorate($this->query->min($column)); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get the aggregate avg. |
165
|
|
|
* |
166
|
|
|
* @param $column |
167
|
|
|
* @return int |
168
|
|
|
*/ |
169
|
|
|
public function avg($column) |
170
|
|
|
{ |
171
|
|
|
return (new Decorator())->decorate($this->query->avg($column)); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Find an entry. |
176
|
|
|
* |
177
|
|
|
* @param $identifier |
178
|
|
|
* @param array $columns |
179
|
|
|
* @return Presenter|EntryPresenter |
180
|
|
|
*/ |
181
|
|
|
public function find($identifier, array $columns = ['*']) |
182
|
|
|
{ |
183
|
|
|
return (new Decorator())->decorate($this->query->find($identifier, $columns)); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Find an entry by column value. |
188
|
|
|
* |
189
|
|
|
* @param $column |
190
|
|
|
* @param $value |
191
|
|
|
* @param array $columns |
192
|
|
|
* @return Presenter|EntryPresenter |
193
|
|
|
*/ |
194
|
|
|
public function findBy($column, $value, array $columns = ['*']) |
195
|
|
|
{ |
196
|
|
|
$this->query->where($column, $value); |
197
|
|
|
|
198
|
|
|
return (new Decorator())->decorate($this->query->first($columns)); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Return the first entry. |
203
|
|
|
* |
204
|
|
|
* @param array $columns |
205
|
|
|
* @return EloquentModel|EntryInterface |
206
|
|
|
*/ |
207
|
|
|
public function first(array $columns = ['*']) |
208
|
|
|
{ |
209
|
|
|
return (new Decorator())->decorate($this->query->first($columns)); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Return whether the method is safe or not. |
214
|
|
|
* |
215
|
|
|
* @param $name |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
|
|
protected function methodIsSafe($name) |
219
|
|
|
{ |
220
|
|
|
return (!in_array($name, $this->disabled)); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Return whether the method |
225
|
|
|
* exists on the query or not. |
226
|
|
|
* |
227
|
|
|
* @param $name |
228
|
|
|
* @return bool |
229
|
|
|
*/ |
230
|
|
|
protected function methodExists($name) |
231
|
|
|
{ |
232
|
|
|
return method_exists($this->query->getQuery(), $name) || method_exists($this->query, $name); |
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Route through __call. |
237
|
|
|
* |
238
|
|
|
* @param $name |
239
|
|
|
* @return Builder|null |
240
|
|
|
*/ |
241
|
|
|
public function __get($name) |
242
|
|
|
{ |
243
|
|
|
return $this->__call($name, []); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Call the method on the query. |
248
|
|
|
* |
249
|
|
|
* @param $name |
250
|
|
|
* @param $arguments |
251
|
|
|
* @return Builder|null |
252
|
|
|
*/ |
253
|
|
|
public function __call($name, $arguments) |
254
|
|
|
{ |
255
|
|
|
if ($this->hasHook($name)) { |
256
|
|
|
return $this->call($name, $arguments); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if ($this->methodExists($name) && $this->methodIsSafe($name)) { |
260
|
|
|
|
261
|
|
|
call_user_func_array([$this->query, $name], $arguments); |
262
|
|
|
|
263
|
|
|
return $this; |
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
View Code Duplication |
if (starts_with($name, 'findBy') && $column = snake_case(substr($name, 6))) { |
|
|
|
|
267
|
|
|
|
268
|
|
|
call_user_func_array([$this->query, 'where'], array_merge([$column], $arguments)); |
269
|
|
|
|
270
|
|
|
return $this->first(); |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
View Code Duplication |
if (starts_with($name, 'where') && $column = snake_case(substr($name, 5))) { |
|
|
|
|
274
|
|
|
|
275
|
|
|
call_user_func_array([$this->query, 'where'], array_merge([$column], $arguments)); |
276
|
|
|
|
277
|
|
|
return $this; |
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $this; |
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Return the string. |
285
|
|
|
* |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
|
|
public function __toString() |
289
|
|
|
{ |
290
|
|
|
return ''; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: