1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sofa\Eloquence; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Connection; |
6
|
|
|
use Sofa\Hookable\Hookable; |
7
|
|
|
use Sofa\Hookable\Contracts\ArgumentBag; |
8
|
|
|
use Sofa\Eloquence\Query\Builder as QueryBuilder; |
9
|
|
|
use Sofa\Eloquence\AttributeCleaner\Observer as AttributeCleaner; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This trait is an entry point for all the hooks that we want to apply |
13
|
|
|
* on the Eloquent Model and Builder in order to let the magic happen. |
14
|
|
|
* |
15
|
|
|
* It also provides hasColumn and getColumnListing helper methods |
16
|
|
|
* so you can easily list or check columns in the model's table. |
17
|
|
|
* |
18
|
|
|
* @version 5.5 |
19
|
|
|
* |
20
|
|
|
* @method Connection getConnection() |
21
|
|
|
* @method string getTable() |
22
|
|
|
*/ |
23
|
|
|
trait Eloquence |
24
|
|
|
{ |
25
|
|
|
use Hookable; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Model's table column listing. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected static $columnListing = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Boot the trait. |
36
|
|
|
* |
37
|
|
|
* @codeCoverageIgnore |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public static function bootEloquence() |
42
|
|
|
{ |
43
|
|
|
static::observe(new AttributeCleaner); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Determine whether where should be treated as whereNull. |
48
|
|
|
* |
49
|
|
|
* @param string $method |
50
|
|
|
* @param ArgumentBag $args |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
protected function isWhereNull($method, ArgumentBag $args) |
54
|
|
|
{ |
55
|
|
|
return $method === 'whereNull' || $method === 'where' && $this->isWhereNullByArgs($args); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Determine whether where is a whereNull by the arguments passed to where method. |
60
|
|
|
* |
61
|
|
|
* @param ArgumentBag $args |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
protected function isWhereNullByArgs(ArgumentBag $args) |
65
|
|
|
{ |
66
|
|
|
return is_null($args->get('operator')) |
67
|
|
|
|| is_null($args->get('value')) && !in_array($args->get('operator'), ['<>', '!=']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Extract real name and alias from the sql select clause. |
72
|
|
|
* |
73
|
|
|
* @param string $column |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected function extractColumnAlias($column) |
77
|
|
|
{ |
78
|
|
|
$alias = $column; |
79
|
|
|
|
80
|
|
|
if (strpos($column, ' as ') !== false) { |
81
|
|
|
list($column, $alias) = explode(' as ', $column); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return [$column, $alias]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the target relation and column from the mapping. |
89
|
|
|
* |
90
|
|
|
* @param string $mapping |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public function parseMappedColumn($mapping) |
94
|
|
|
{ |
95
|
|
|
$segments = explode('.', $mapping); |
96
|
|
|
|
97
|
|
|
$column = array_pop($segments); |
98
|
|
|
|
99
|
|
|
$target = implode('.', $segments); |
100
|
|
|
|
101
|
|
|
return [$target, $column]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Determine whether the key is meta attribute or actual table field. |
106
|
|
|
* |
107
|
|
|
* @param string $key |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public static function hasColumn($key) |
111
|
|
|
{ |
112
|
|
|
static::loadColumnListing(); |
113
|
|
|
|
114
|
|
|
return in_array((string) $key, static::$columnListing); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get searchable columns defined on the model. |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getSearchableColumns() |
123
|
|
|
{ |
124
|
|
|
return (property_exists($this, 'searchableColumns')) ? $this->searchableColumns : []; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get model table columns. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public static function getColumnListing() |
133
|
|
|
{ |
134
|
|
|
static::loadColumnListing(); |
135
|
|
|
|
136
|
|
|
return static::$columnListing; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Fetch model table columns. |
141
|
|
|
* |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
protected static function loadColumnListing() |
145
|
|
|
{ |
146
|
|
|
if (empty(static::$columnListing)) { |
147
|
|
|
$instance = new static; |
148
|
|
|
|
149
|
|
|
static::$columnListing = $instance->getConnection() |
150
|
|
|
->getSchemaBuilder() |
151
|
|
|
->getColumnListing($instance->getTable()); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Create new Eloquence query builder for the instance. |
157
|
|
|
* |
158
|
|
|
* @param QueryBuilder $query |
159
|
|
|
* @return Builder |
160
|
|
|
*/ |
161
|
|
|
public function newEloquentBuilder($query) |
162
|
|
|
{ |
163
|
|
|
return new Builder($query); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get a new query builder instance for the connection. |
168
|
|
|
* |
169
|
|
|
* @return QueryBuilder |
170
|
|
|
*/ |
171
|
|
|
protected function newBaseQueryBuilder() |
172
|
|
|
{ |
173
|
|
|
$conn = $this->getConnection(); |
174
|
|
|
|
175
|
|
|
$grammar = $conn->getQueryGrammar(); |
176
|
|
|
|
177
|
|
|
return new QueryBuilder($conn, $grammar, $conn->getPostProcessor()); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: