|
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
|
|
|
]; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The query builder. |
|
37
|
|
|
* |
|
38
|
|
|
* @var Builder|\Illuminate\Database\Query\Builder |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $query; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Set the get method. |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $method; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Create a new EntryCriteria instance. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Builder $query |
|
53
|
|
|
* @param string $method |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(Builder $query, $method = 'get') |
|
56
|
|
|
{ |
|
57
|
|
|
$this->query = $query; |
|
58
|
|
|
$this->method = $method; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the paginated entries. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $columns |
|
65
|
|
|
* @return Collection|Presenter|EntryPresenter |
|
66
|
|
|
*/ |
|
67
|
|
|
public function paginate($perPage = 15, array $columns = ['*']) |
|
68
|
|
|
{ |
|
69
|
|
|
return (new Decorator())->decorate($this->query->paginate($perPage, $columns)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the entries. |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $columns |
|
76
|
|
|
* @return Collection|Presenter|EntryPresenter |
|
77
|
|
|
*/ |
|
78
|
|
|
public function get(array $columns = ['*']) |
|
79
|
|
|
{ |
|
80
|
|
|
return (new Decorator())->decorate($this->query->{$this->method}($columns)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Find an entry. |
|
85
|
|
|
* |
|
86
|
|
|
* @param $identifier |
|
87
|
|
|
* @param array $columns |
|
88
|
|
|
* @return Presenter|EntryPresenter |
|
89
|
|
|
*/ |
|
90
|
|
|
public function find($identifier, array $columns = ['*']) |
|
91
|
|
|
{ |
|
92
|
|
|
return (new Decorator())->decorate($this->query->find($identifier, $columns)); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Find an entry by column value. |
|
97
|
|
|
* |
|
98
|
|
|
* @param $column |
|
99
|
|
|
* @param $value |
|
100
|
|
|
* @param array $columns |
|
101
|
|
|
* @return Presenter|EntryPresenter |
|
102
|
|
|
*/ |
|
103
|
|
|
public function findBy($column, $value, array $columns = ['*']) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->query->where($column, $value); |
|
106
|
|
|
|
|
107
|
|
|
return (new Decorator())->decorate($this->query->first($columns)); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Return the first entry. |
|
112
|
|
|
* |
|
113
|
|
|
* @param array $columns |
|
114
|
|
|
* @return EloquentModel|EntryInterface |
|
115
|
|
|
*/ |
|
116
|
|
|
public function first(array $columns = ['*']) |
|
117
|
|
|
{ |
|
118
|
|
|
return (new Decorator())->decorate($this->query->first($columns)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Return whether the method is safe or not. |
|
123
|
|
|
* |
|
124
|
|
|
* @param $name |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function methodIsSafe($name) |
|
128
|
|
|
{ |
|
129
|
|
|
return (!in_array($name, $this->disabled)); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Route through __call. |
|
134
|
|
|
* |
|
135
|
|
|
* @param $name |
|
136
|
|
|
* @return Builder|null |
|
137
|
|
|
*/ |
|
138
|
|
|
function __get($name) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
return $this->__call($name, []); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Call the method on the query. |
|
145
|
|
|
* |
|
146
|
|
|
* @param $name |
|
147
|
|
|
* @param $arguments |
|
148
|
|
|
* @return Builder|null |
|
149
|
|
|
*/ |
|
150
|
|
|
function __call($name, $arguments) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
|
|
153
|
|
|
if ($this->hasHook($name)) { |
|
154
|
|
|
return $this->call($name, $arguments); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if ($this->methodIsSafe($name)) { |
|
158
|
|
|
call_user_func_array([$this->query, $name], $arguments); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
View Code Duplication |
if (starts_with($name, 'findBy') && $column = snake_case(substr($name, 6))) { |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
call_user_func_array([$this->query, 'where'], array_merge([$column], $arguments)); |
|
164
|
|
|
|
|
165
|
|
|
return $this->first(); |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
View Code Duplication |
if (starts_with($name, 'where') && $column = snake_case(substr($name, 5))) { |
|
|
|
|
|
|
169
|
|
|
call_user_func_array([$this->query, 'where'], array_merge([$column], $arguments)); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $this; |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.