1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Blasttech\EloquentRelatedPlus; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait CustomOrderTrait |
10
|
|
|
* |
11
|
|
|
* @property array order_fields |
12
|
|
|
* @property array order_defaults |
13
|
|
|
* @property array order_relations |
14
|
|
|
* @property array order_with |
15
|
|
|
* @property array search_fields |
16
|
|
|
* @property string connection |
17
|
|
|
*/ |
18
|
|
|
trait CustomOrderTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Check $order_fields and $order_defaults are set |
22
|
|
|
* |
23
|
|
|
* @param string $orderField |
24
|
|
|
* @param string $direction |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
|
|
protected function hasOrderFieldsAndDefaults($orderField, $direction) |
28
|
|
|
{ |
29
|
|
|
return $this->hasOrderFields() && $this->hasOrderDefaults($orderField, $direction); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Check $this->order_fields set correctly |
34
|
|
|
* |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
protected function hasOrderFields() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
if (!isset($this->order_fields) || !is_array($this->order_fields)) { |
40
|
|
|
throw new InvalidArgumentException(get_class($this) . ' order fields not set correctly.'); |
41
|
|
|
} else { |
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check order defaults set correctly |
48
|
|
|
* |
49
|
|
|
* @param string $orderField |
50
|
|
|
* @param string $direction |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
protected function hasOrderDefaults($orderField, $direction) |
54
|
|
|
{ |
55
|
|
|
if (($orderField === '' || $direction === '') |
56
|
|
|
&& (!isset($this->order_defaults) || !is_array($this->order_defaults))) { |
57
|
|
|
throw new InvalidArgumentException(get_class($this) . ' order defaults not set and not overriden.'); |
58
|
|
|
} else { |
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check $this->search_fields set correctly |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
protected function hasSearchFields() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if (!isset($this->search_fields) || !is_array($this->search_fields) || empty($this->search_fields)) { |
71
|
|
|
throw new InvalidArgumentException(get_class($this) . ' search properties not set correctly.'); |
72
|
|
|
} else { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Override column if provided column not valid |
79
|
|
|
* |
80
|
|
|
* @param string $column |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected function setOrderColumn($column) |
84
|
|
|
{ |
85
|
|
|
// If $column not in order_fields list, use default |
86
|
|
|
if ($column == '' || !isset($this->order_fields[$column])) { |
87
|
|
|
$column = $this->order_defaults['field']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $column; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Override direction if provided direction not valid |
95
|
|
|
* |
96
|
|
|
* @param string $direction |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function setOrderDirection($direction) |
100
|
|
|
{ |
101
|
|
|
// If $direction not asc or desc, use default |
102
|
|
|
if ($direction == '' || !in_array(strtoupper($direction), ['ASC', 'DESC'])) { |
103
|
|
|
$direction = $this->order_defaults['dir']; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $direction; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set order based on order_fields |
111
|
|
|
* |
112
|
|
|
* @param Builder $query |
113
|
|
|
* @param string $column |
114
|
|
|
* @param string $direction |
115
|
|
|
* @return Builder |
116
|
|
|
*/ |
117
|
|
|
protected function setOrder($query, $column, $direction) |
118
|
|
|
{ |
119
|
|
|
if (!is_array($this->order_fields[$column])) { |
120
|
|
|
$query->orderByCheckModel($this->order_fields[$column], $direction); |
121
|
|
|
} else { |
122
|
|
|
foreach ($this->order_fields[$column] as $dbField) { |
123
|
|
|
$query->orderByCheckModel($dbField, $direction); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $query; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Join a related table if not already joined |
132
|
|
|
* |
133
|
|
|
* @param Builder $query |
134
|
|
|
* @param string $table |
135
|
|
|
* @return Builder |
136
|
|
|
*/ |
137
|
|
|
protected function joinRelatedTable($query, $table) |
138
|
|
|
{ |
139
|
|
|
if (isset($this->order_relations[$table]) && |
140
|
|
|
!$this->hasJoin($query, $table, $this->order_relations[$table])) { |
141
|
|
|
$columnRelations = $this->order_relations[$table]; |
142
|
|
|
|
143
|
|
|
$query->modelJoin( |
144
|
|
|
$columnRelations, |
145
|
|
|
'=', |
146
|
|
|
'left', |
147
|
|
|
false, |
148
|
|
|
false |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $query; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Check if this model has already been joined to a table or relation |
157
|
|
|
* |
158
|
|
|
* @param Builder $builder |
159
|
|
|
* @param string $table |
160
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
protected function hasJoin(Builder $builder, $table, $relation) |
164
|
|
|
{ |
165
|
|
|
if (!$this->isJoinedToTable($builder, $table)) { |
166
|
|
|
return $this->isEagerLoaded($builder, $relation); |
167
|
|
|
} else { |
168
|
|
|
return true; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Check if model is currently joined to $table |
174
|
|
|
* |
175
|
|
|
* @param Builder $builder |
176
|
|
|
* @param string $table |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
protected function isJoinedToTable(Builder $builder, $table) |
180
|
|
|
{ |
181
|
|
|
$joins = $builder->getQuery()->joins; |
182
|
|
|
if (!is_null($joins)) { |
183
|
|
|
foreach ($joins as $joinClause) { |
184
|
|
|
if ($joinClause->table == $table) { |
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Check if relation exists in eager loads |
195
|
|
|
* |
196
|
|
|
* @param Builder $builder |
197
|
|
|
* @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
protected function isEagerLoaded(Builder $builder, $relation) |
201
|
|
|
{ |
202
|
|
|
$eagerLoads = $builder->getEagerLoads(); |
203
|
|
|
|
204
|
|
|
return !is_null($eagerLoads) && in_array($relation, $eagerLoads); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.