-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathBuilder.php
More file actions
executable file
·293 lines (248 loc) · 6.42 KB
/
Builder.php
File metadata and controls
executable file
·293 lines (248 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
namespace Cristal\ApiWrapper;
use Cristal\ApiWrapper\Exceptions\ApiEntityNotFoundException;
class Builder
{
const MAX_RESULTS = 9999;
const PAGINATION_MAPPING_PAGE = 'page';
const PAGINATION_MAPPING_TOTAL = 'total';
const PAGINATION_MAPPING_PER_PAGE = 'per_page';
const PAGINATION_MAPPING_CURRENT_PAGE = 'current_page';
/**
* @var array
*/
protected $query = [];
/**
* The model being queried.
*
* @var Model
*/
protected $model;
/**
* Get the underlying query builder instance.
*
* @return array
*/
public function getQuery()
{
return array_merge(
array_merge(...array_values($this->scopes)),
$this->query
);
}
/**
* Applied global scopes.
*
* @var array
*/
protected $scopes = [];
/**
* Removed global scopes.
*
* @var array
*/
protected $removedScopes = [];
/**
* Set a model instance for the model being queried.
*
* @param Model $model
*
* @return $this
*/
public function setModel(Model $model)
{
$this->model = $model;
return $this;
}
/**
* Get the model instance being queried.
*
* @return Model
*/
public function getModel()
{
return $this->model;
}
public function first()
{
return $this->get()[0] ?? null;
}
public function find($field, $value = null)
{
$res = null;
try {
$res = $this->findOrFail($field, $value);
} catch (ApiEntityNotFoundException) {
return null;
}
return $res;
}
public function findOrFail($field, $value = null)
{
if (is_array($field)) {
$this->query = array_merge($this->query, ['id' => $field]);
return $this->where($this->query)->get();
} elseif (!is_int($field) && $value !== null && count($this->query)) {
$this->query = array_merge($this->query, [$field => $value]);
return $this->where($this->query)->get()[0] ?? null;
}
$data = $this->model->getApi()->{'get'.ucfirst((string) $this->model->getEntity())}($field, $this->getQuery());
return $this->model->newInstance($data, true);
}
/**
* Add a basic where clause to the query.
*
* @param $field
* @param null $value
*
* @return self
*/
public function where($field, $value = null)
{
if (!is_array($field)) {
$field = [$field => $value];
}
$this->query = array_merge($this->query, $field);
return $this;
}
/**
* @return self[]
*/
public function all()
{
return $this->take(static::MAX_RESULTS)->get();
}
/**
* Alias to set the "limit" value of the query.
*
* @param int $value
*
* @return Builder|static
*/
public function take($value)
{
return $this->limit($value);
}
/**
* Set the "limit" value of the query.
*
* @param int $value
*
* @return Builder|static
*/
public function limit($value)
{
return $this->where('limit', $value);
}
/**
* Set the limit and offset for a given page.
*
* @param int $page
* @param int $perPage
*
* @return Builder|static
*/
public function forPage($page, $perPage = 15)
{
return $this->where('page', $page)->take($perPage);
}
/**
* Register a new global scope.
*
* @param string $identifier
* @param array $scope
*
* @return $this
*/
public function withGlobalScope($identifier, array $scope)
{
$this->scopes[$identifier] = $scope;
return $this;
}
/**
* Remove a registered global scope.
*
* @param string $identifier
* @return $this
*/
public function withoutGlobalScope(string $identifier)
{
unset($this->scopes[$identifier]);
$this->removedScopes[] = $identifier;
return $this;
}
/**
* Apply the given scope on the current builder instance.
*
* @param array $scope
* @param array $parameters
*
* @return mixed
*/
protected function callScope(array $scope, $parameters = [])
{
[$model, $method] = $scope;
return $model->$method($this, ...$parameters) ?? $this;
}
/**
* Dynamically handle calls into the query instance.
*
* @param string $method
* @param array $parameters
*
* @return mixed
*/
public function __call($method, $parameters)
{
if (method_exists($this->model, $scope = 'scope'.ucfirst($method))) {
return $this->callScope([$this->model, $scope], $parameters);
}
try {
$this->query->{$method}(...$parameters);
} catch (\Throwable $e) {
// Pour une raison qui m'échappe, PHP retourne une Fatal exception qui efface la stack d'exception
// si une erreur arrive... on re throw qqc de plus expressif
throw new \Exception($e->getMessage());
}
return $this;
}
/**
* Execute the query.
*
* @return array|static[]
*/
public function get()
{
$entities = $this->raw();
return $this->instanciateModels($entities);
}
public function raw()
{
$instance = $this->getModel();
try {
return $instance->getApi()->{'get'.ucfirst($instance->getEntities())}($this->getQuery());
} catch (ApiEntityNotFoundException) {
return [];
}
}
public function instanciateModels($data)
{
if (!$data) {
return null;
}
return array_map(fn($entity) => $this->model->newInstance($entity, true), $data);
}
public function paginate(?int $perPage = null, ?int $page = 1)
{
$this->limit($perPage);
$this->where([static::PAGINATION_MAPPING_PAGE => $page]);
$instance = $this->getModel();
$entities = $this->raw();
return [
'data' => $this->instanciateModels($entities),
'total' => $entities[static::PAGINATION_MAPPING_TOTAL] ?? null,
'per_page' => $entities[static::PAGINATION_MAPPING_PER_PAGE] ?? $perPage,
'current_page' => $entities[static::PAGINATION_MAPPING_CURRENT_PAGE] ?? $page
];
}
}