1 | <?php |
||
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 | 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 | * Override column if provided column not valid |
||
65 | * |
||
66 | * @param string $column |
||
67 | * @return string |
||
68 | */ |
||
69 | protected function setOrderColumn($column) |
||
70 | { |
||
71 | // If $column not in order_fields list, use default |
||
72 | if ($column == '' || !isset($this->order_fields[$column])) { |
||
73 | $column = $this->order_defaults['field']; |
||
74 | } |
||
75 | |||
76 | return $column; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Override direction if provided direction not valid |
||
81 | * |
||
82 | * @param string $direction |
||
83 | * @return string |
||
84 | */ |
||
85 | protected function setOrderDirection($direction) |
||
86 | { |
||
87 | // If $direction not asc or desc, use default |
||
88 | if ($direction == '' || !in_array(strtoupper($direction), ['ASC', 'DESC'])) { |
||
89 | $direction = $this->order_defaults['dir']; |
||
90 | } |
||
91 | |||
92 | return $direction; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Set order based on order_fields |
||
97 | * |
||
98 | * @param Builder $query |
||
99 | * @param string $column |
||
100 | * @param string $direction |
||
101 | * @return Builder |
||
102 | */ |
||
103 | protected function setOrder($query, $column, $direction) |
||
115 | |||
116 | /** |
||
117 | * Join a related table if not already joined |
||
118 | * |
||
119 | * @param Builder $query |
||
120 | * @param string $table |
||
121 | * @return Builder |
||
122 | */ |
||
123 | protected function joinRelatedTable($query, $table) |
||
140 | |||
141 | /** |
||
142 | * Check if this model has already been joined to a table or relation |
||
143 | * |
||
144 | * @param Builder $builder |
||
145 | * @param string $table |
||
146 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
147 | * @return bool |
||
148 | */ |
||
149 | protected function hasJoin(Builder $builder, $table, $relation) |
||
157 | |||
158 | /** |
||
159 | * Check if model is currently joined to $table |
||
160 | * |
||
161 | * @param Builder $builder |
||
162 | * @param string $table |
||
163 | * @return bool |
||
164 | */ |
||
165 | protected function isJoinedToTable(Builder $builder, $table) |
||
178 | } |
||
179 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.