1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magister\Services\Database\Elegant; |
4
|
|
|
|
5
|
|
|
use Magister\Services\Database\Query\Builder as QueryBuilder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Builder. |
9
|
|
|
*/ |
10
|
|
|
class Builder |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The base query builder implementation. |
14
|
|
|
* |
15
|
|
|
* @var \Magister\Services\Database\Query\Builder |
16
|
|
|
*/ |
17
|
|
|
protected $query; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The model being queried. |
21
|
|
|
* |
22
|
|
|
* @var \Magister\Services\Database\Elegant\Model |
23
|
|
|
*/ |
24
|
|
|
protected $model; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a new elegant builder instance. |
28
|
|
|
* |
29
|
|
|
* @param \Magister\Services\Database\Query\Builder $query |
30
|
|
|
*/ |
31
|
|
|
public function __construct(QueryBuilder $query) |
32
|
|
|
{ |
33
|
|
|
$this->query = $query; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Find a model by its primary key. |
38
|
|
|
* |
39
|
|
|
* @param mixed $id |
40
|
|
|
* |
41
|
|
|
* @return \Magister\Services\Database\Elegant\Model|\Magister\Services\Support\Collection|null |
42
|
|
|
*/ |
43
|
|
|
public function find($id) |
44
|
|
|
{ |
45
|
|
|
return $this->get()->find($id); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Execute the query and get the first result. |
50
|
|
|
* |
51
|
|
|
* @return \Magister\Services\Database\Elegant\Model|static|null |
52
|
|
|
*/ |
53
|
|
|
public function first() |
54
|
|
|
{ |
55
|
|
|
return $this->get()->first(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Execute the query as a select statement. |
60
|
|
|
* |
61
|
|
|
* @return \Magister\Services\Support\Collection|static[] |
62
|
|
|
*/ |
63
|
|
|
public function get() |
64
|
|
|
{ |
65
|
|
|
$models = $this->getModels(); |
66
|
|
|
|
67
|
|
|
return $this->model->newCollection($models); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Pluck a single column from the database. |
72
|
|
|
* |
73
|
|
|
* @param string $column |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function pluck($column) |
78
|
|
|
{ |
79
|
|
|
$result = $this->first(); |
80
|
|
|
|
81
|
|
|
if ($result) { |
82
|
|
|
return $result->$column; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get an array with the values of a given column. |
88
|
|
|
* |
89
|
|
|
* @param string $column |
90
|
|
|
* @param string $key |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function lists($column, $key = null) |
95
|
|
|
{ |
96
|
|
|
return $this->query->lists($column, $key); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the hydrated models. |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function getModels() |
105
|
|
|
{ |
106
|
|
|
$results = $this->query->get(); |
107
|
|
|
|
108
|
|
|
$connection = $this->model->getConnectionName(); |
109
|
|
|
|
110
|
|
|
return $this->model->hydrate($results, $connection)->all(); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the underlying query builder instance. |
115
|
|
|
* |
116
|
|
|
* @return \Magister\Services\Database\Query\Builder|static |
117
|
|
|
*/ |
118
|
|
|
public function getQuery() |
119
|
|
|
{ |
120
|
|
|
return $this->query; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set the underlying query builder instance. |
125
|
|
|
* |
126
|
|
|
* @param \Magister\Services\Database\Query\Builder $query |
127
|
|
|
* |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function setQuery($query) |
131
|
|
|
{ |
132
|
|
|
$this->query = $query; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the model instance being queried. |
139
|
|
|
* |
140
|
|
|
* @param \Magister\Services\Database\Elegant\Model $model |
141
|
|
|
* |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function setModel(Model $model) |
145
|
|
|
{ |
146
|
|
|
$this->model = $model; |
147
|
|
|
|
148
|
|
|
$this->query->from($model->getUrl()); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the model instance being queried. |
155
|
|
|
* |
156
|
|
|
* @return \Magister\Services\Database\Elegant\Model |
157
|
|
|
*/ |
158
|
|
|
public function getModel() |
159
|
|
|
{ |
160
|
|
|
return $this->model; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Dynamically handle calls into the query instance. |
165
|
|
|
* |
166
|
|
|
* @param string $method |
167
|
|
|
* @param array $parameters |
168
|
|
|
* |
169
|
|
|
* @return mixed |
170
|
|
|
*/ |
171
|
|
|
public function __call($method, $parameters) |
172
|
|
|
{ |
173
|
|
|
call_user_func_array([$this->query, $method], $parameters); |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.