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\Support\Collection; |
6
|
|
|
use Anomaly\Streams\Platform\Support\Decorator; |
7
|
|
|
use Anomaly\Streams\Platform\Support\Presenter; |
8
|
|
|
use Anomaly\Streams\Platform\Traits\Hookable; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
10
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class EloquentCriteria |
14
|
|
|
* |
15
|
|
|
* @link http://anomaly.is/streams-platform |
16
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
17
|
|
|
* @author Ryan Thompson <[email protected]> |
18
|
|
|
* @package Anomaly\Streams\Platform\Model |
19
|
|
|
*/ |
20
|
|
|
class EloquentCriteria |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
use Hookable; |
24
|
|
|
use DispatchesJobs; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Safe builder methods. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $disabled = [ |
32
|
|
|
'delete', |
33
|
|
|
'update', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The query builder. |
38
|
|
|
* |
39
|
|
|
* @var Builder|\Illuminate\Database\Query\Builder |
40
|
|
|
*/ |
41
|
|
|
protected $query; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set the get method. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $method; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create a new EntryCriteria instance. |
52
|
|
|
* |
53
|
|
|
* @param Builder $query |
54
|
|
|
* @param string $method |
55
|
|
|
*/ |
56
|
|
|
public function __construct(Builder $query, $method = 'get') |
57
|
|
|
{ |
58
|
|
|
$this->query = $query; |
59
|
|
|
$this->method = $method; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the paginated entries. |
64
|
|
|
* |
65
|
|
|
* @param array $columns |
66
|
|
|
* @return Collection|Presenter|EntryPresenter |
67
|
|
|
*/ |
68
|
|
|
public function paginate($perPage = 15, array $columns = ['*']) |
69
|
|
|
{ |
70
|
|
|
return (new Decorator())->decorate($this->query->paginate($perPage, $columns)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the entries. |
75
|
|
|
* |
76
|
|
|
* @param array $columns |
77
|
|
|
* @return Collection|Presenter|EntryPresenter |
78
|
|
|
*/ |
79
|
|
|
public function get(array $columns = ['*']) |
80
|
|
|
{ |
81
|
|
|
return (new Decorator())->decorate($this->query->{$this->method}($columns)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the aggregate sum. |
86
|
|
|
* |
87
|
|
|
* @param array $columns |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
|
|
public function sum(array $columns = ['*']) |
91
|
|
|
{ |
92
|
|
|
return (new Decorator())->decorate($this->query->sum($columns)); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the entry count. |
97
|
|
|
* |
98
|
|
|
* @param array $columns |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
|
|
public function count(array $columns = ['*']) |
102
|
|
|
{ |
103
|
|
|
return (new Decorator())->decorate($this->query->count($columns)); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Find an entry. |
108
|
|
|
* |
109
|
|
|
* @param $identifier |
110
|
|
|
* @param array $columns |
111
|
|
|
* @return Presenter|EntryPresenter |
112
|
|
|
*/ |
113
|
|
|
public function find($identifier, array $columns = ['*']) |
114
|
|
|
{ |
115
|
|
|
return (new Decorator())->decorate($this->query->find($identifier, $columns)); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Find an entry by column value. |
120
|
|
|
* |
121
|
|
|
* @param $column |
122
|
|
|
* @param $value |
123
|
|
|
* @param array $columns |
124
|
|
|
* @return Presenter|EntryPresenter |
125
|
|
|
*/ |
126
|
|
|
public function findBy($column, $value, array $columns = ['*']) |
127
|
|
|
{ |
128
|
|
|
$this->query->where($column, $value); |
129
|
|
|
|
130
|
|
|
return (new Decorator())->decorate($this->query->first($columns)); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Return the first entry. |
135
|
|
|
* |
136
|
|
|
* @param array $columns |
137
|
|
|
* @return EloquentModel|EntryInterface |
138
|
|
|
*/ |
139
|
|
|
public function first(array $columns = ['*']) |
140
|
|
|
{ |
141
|
|
|
return (new Decorator())->decorate($this->query->first($columns)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return whether the method is safe or not. |
146
|
|
|
* |
147
|
|
|
* @param $name |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
protected function methodIsSafe($name) |
151
|
|
|
{ |
152
|
|
|
return (!in_array($name, $this->disabled)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Route through __call. |
157
|
|
|
* |
158
|
|
|
* @param $name |
159
|
|
|
* @return Builder|null |
160
|
|
|
*/ |
161
|
|
|
function __get($name) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
return $this->__call($name, []); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Call the method on the query. |
168
|
|
|
* |
169
|
|
|
* @param $name |
170
|
|
|
* @param $arguments |
171
|
|
|
* @return Builder|null |
172
|
|
|
*/ |
173
|
|
|
function __call($name, $arguments) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
|
176
|
|
|
if ($this->hasHook($name)) { |
177
|
|
|
return $this->call($name, $arguments); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($this->methodIsSafe($name)) { |
181
|
|
|
call_user_func_array([$this->query, $name], $arguments); |
182
|
|
|
|
183
|
|
|
return $this; |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
View Code Duplication |
if (starts_with($name, 'findBy') && $column = snake_case(substr($name, 6))) { |
|
|
|
|
187
|
|
|
|
188
|
|
|
call_user_func_array([$this->query, 'where'], array_merge([$column], $arguments)); |
189
|
|
|
|
190
|
|
|
return $this->first(); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
View Code Duplication |
if (starts_with($name, 'where') && $column = snake_case(substr($name, 5))) { |
|
|
|
|
194
|
|
|
call_user_func_array([$this->query, 'where'], array_merge([$column], $arguments)); |
195
|
|
|
|
196
|
|
|
return $this; |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $this; |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Return the string. |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function __toString() |
208
|
|
|
{ |
209
|
|
|
return ''; |
210
|
|
|
} |
211
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: