1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codexshaper\WooCommerce\Traits; |
4
|
|
|
|
5
|
|
|
use Codexshaper\WooCommerce\Facades\WooCommerce; |
6
|
|
|
use Illuminate\Support\LazyCollection; |
7
|
|
|
|
8
|
|
|
trait QueryBuilderTrait |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var |
12
|
|
|
*/ |
13
|
|
|
protected $options = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var |
17
|
|
|
*/ |
18
|
|
|
protected $where = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var |
22
|
|
|
*/ |
23
|
|
|
protected $properties = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var |
27
|
|
|
*/ |
28
|
|
|
protected $isLazyCollection = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var |
32
|
|
|
*/ |
33
|
|
|
protected $isCollection = true; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var |
37
|
|
|
*/ |
38
|
|
|
protected $isOriginal = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieve all Items. |
42
|
|
|
* |
43
|
|
|
* @param array $options |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
protected function all($options = []) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
if ($this->isLazyCollection) { |
50
|
|
|
return LazyCollection::make(WooCommerce::all($this->endpoint, $options)); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($this->isCollection) { |
54
|
|
|
return collect(WooCommerce::all($this->endpoint, $options)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return WooCommerce::all($this->endpoint, $options); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Retrieve single Item. |
62
|
|
|
* |
63
|
|
|
* @param int $id |
64
|
|
|
* @param array $options |
65
|
|
|
* |
66
|
|
|
* @return object |
67
|
|
|
*/ |
68
|
|
|
protected function find($id, $options = []) |
69
|
|
|
{ |
70
|
|
|
if ($this->isLazyCollection) { |
71
|
|
|
return LazyCollection::make(WooCommerce::find("{$this->endpoint}/{$id}", $options)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($this->isCollection) { |
75
|
|
|
return collect(WooCommerce::find("{$this->endpoint}/{$id}", $options)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return WooCommerce::find("{$this->endpoint}/{$id}", $options); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Create new Item. |
83
|
|
|
* |
84
|
|
|
* @param array $data |
85
|
|
|
* |
86
|
|
|
* @return object |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
protected function create($data) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
if ($this->isLazyCollection) { |
91
|
|
|
return LazyCollection::make(WooCommerce::create($this->endpoint, $data)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($this->isCollection) { |
95
|
|
|
return collect(WooCommerce::create($this->endpoint, $data)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return WooCommerce::create($this->endpoint, $data); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Update Existing Item. |
103
|
|
|
* |
104
|
|
|
* @param int $id |
105
|
|
|
* @param array $data |
106
|
|
|
* |
107
|
|
|
* @return object |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
protected function update($id, $data) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
if ($this->isLazyCollection) { |
112
|
|
|
return LazyCollection::make(WooCommerce::update("{$this->endpoint}/{$id}", $data)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($this->isCollection) { |
116
|
|
|
return collect(WooCommerce::update("{$this->endpoint}/{$id}", $data)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return WooCommerce::update("{$this->endpoint}/{$id}", $data); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Destroy Item. |
124
|
|
|
* |
125
|
|
|
* @param int $id |
126
|
|
|
* @param array $options |
127
|
|
|
* |
128
|
|
|
* @return object |
129
|
|
|
*/ |
130
|
|
|
protected function delete($id, $options = []) |
131
|
|
|
{ |
132
|
|
|
if ($this->isLazyCollection) { |
133
|
|
|
return LazyCollection::make(WooCommerce::delete("{$this->endpoint}/{$id}", $options)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($this->isCollection) { |
137
|
|
|
return collect(WooCommerce::delete("{$this->endpoint}/{$id}", $options)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return WooCommerce::delete("{$this->endpoint}/{$id}", $options); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Batch Update. |
145
|
|
|
* |
146
|
|
|
* @param array $data |
147
|
|
|
* |
148
|
|
|
* @return object |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
protected function batch($data) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
if ($this->isLazyCollection) { |
153
|
|
|
return LazyCollection::make(WooCommerce::create("{$this->endpoint}/batch", $data)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($this->isCollection) { |
157
|
|
|
return collect(WooCommerce::create("{$this->endpoint}/batch", $data)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return WooCommerce::create("{$this->endpoint}/batch", $data); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Retrieve data. |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
protected function get() |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
if ($this->isLazyCollection) { |
171
|
|
|
return LazyCollection::make(WooCommerce::all($this->endpoint, $this->options)); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($this->isCollection) { |
175
|
|
|
return collect(WooCommerce::all($this->endpoint, $this->options)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return WooCommerce::all($this->endpoint, $this->options); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Retrieve data. |
183
|
|
|
* |
184
|
|
|
* @return object |
185
|
|
|
*/ |
186
|
|
|
protected function first() |
187
|
|
|
{ |
188
|
|
|
if ($this->isLazyCollection) { |
189
|
|
|
return LazyCollection::make($this->get()[0] ?? new \stdClass()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if ($this->isCollection) { |
193
|
|
|
return collect($this->get()[0] ?? new \stdClass()); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return collect($this->get()[0] ?? new \stdClass()); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set original. |
201
|
|
|
* |
202
|
|
|
* @return object $this |
203
|
|
|
*/ |
204
|
|
View Code Duplication |
protected function withOriginal() |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
$this->isOriginal = true; |
207
|
|
|
$this->isCollection = false; |
208
|
|
|
$this->isLazyCollection = false; |
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Set collection. |
215
|
|
|
* |
216
|
|
|
* @return object $this |
217
|
|
|
*/ |
218
|
|
View Code Duplication |
protected function withCollection() |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
$this->isOriginal = false; |
221
|
|
|
$this->isCollection = true; |
222
|
|
|
$this->isLazyCollection = false; |
223
|
|
|
|
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Set lazy collection. |
229
|
|
|
* |
230
|
|
|
* @return object $this |
231
|
|
|
*/ |
232
|
|
View Code Duplication |
protected function withLazyCollection() |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
$this->isOriginal = false; |
235
|
|
|
$this->isCollection = false; |
236
|
|
|
$this->isLazyCollection = true; |
237
|
|
|
|
238
|
|
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set options for woocommerce request. |
243
|
|
|
* |
244
|
|
|
* @param array $parameters |
245
|
|
|
* |
246
|
|
|
* @return object $this |
247
|
|
|
*/ |
248
|
|
|
protected function options($parameters) |
249
|
|
|
{ |
250
|
|
|
if (!is_array($parameters)) { |
251
|
|
|
throw new \Exception('Options must be an array', 1); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (empty($parameters)) { |
255
|
|
|
throw new \Exception('Options must be pass at least one element', 1); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
foreach ($parameters as $key => $value) { |
259
|
|
|
$this->options[$key] = $value; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Join options for woocommerce request. |
267
|
|
|
* |
268
|
|
|
* @param array $parameters |
269
|
|
|
* |
270
|
|
|
* @return object $this |
271
|
|
|
*/ |
272
|
|
|
protected function where(...$parameters) |
273
|
|
|
{ |
274
|
|
|
if (count($parameters) < 2 || count($parameters) > 3) { |
275
|
|
|
throw new \Exception('You can pass minimum 2 and maximum 3 paramneters'); |
276
|
|
|
} |
277
|
|
|
$field = strtolower($parameters[0]); |
278
|
|
|
$value = count($parameters) == 3 ? $parameters[2] : $parameters[1]; |
279
|
|
|
|
280
|
|
|
switch ($field) { |
281
|
|
|
case 'name': case 'title': case 'description': |
282
|
|
|
$this->options['search'] = $value; |
283
|
|
|
break; |
284
|
|
|
default: |
285
|
|
|
$this->options[$field] = $value; |
286
|
|
|
break; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $this; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Set order direction. |
294
|
|
|
* |
295
|
|
|
* @param string $name |
296
|
|
|
* @param string $direction |
297
|
|
|
* |
298
|
|
|
* @return object $this |
299
|
|
|
*/ |
300
|
|
|
protected function orderBy($name, $direction = 'desc') |
301
|
|
|
{ |
302
|
|
|
$this->options['orderby'] = $name; |
303
|
|
|
$this->options['order'] = $direction; |
304
|
|
|
|
305
|
|
|
return $this; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Paginate results. |
310
|
|
|
* |
311
|
|
|
* @param int $per_page |
312
|
|
|
* @param int $current_page |
313
|
|
|
* @param array $options |
314
|
|
|
* |
315
|
|
|
* @return array |
316
|
|
|
*/ |
317
|
|
|
protected function paginate($per_page = 10, $current_page = 1, $options = []) |
318
|
|
|
{ |
319
|
|
|
try { |
320
|
|
|
$this->options['per_page'] = (int) $per_page; |
321
|
|
|
|
322
|
|
|
if ($current_page > 0) { |
323
|
|
|
$this->options['page'] = (int) $current_page; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
foreach ($options as $option => $value) { |
327
|
|
|
$this->options[$option] = $value; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$data = $this->get(); |
331
|
|
|
$totalResults = WooCommerce::countResults(); |
332
|
|
|
$totalPages = WooCommerce::countPages(); |
333
|
|
|
$currentPage = WooCommerce::current(); |
334
|
|
|
$previousPage = WooCommerce::previous(); |
335
|
|
|
$nextPage = WooCommerce::next(); |
336
|
|
|
|
337
|
|
|
$pagination = [ |
338
|
|
|
'total_results' => $totalResults, |
339
|
|
|
'total_pages' => $totalPages, |
340
|
|
|
'current_page' => $currentPage, |
341
|
|
|
'previous_page' => $previousPage, |
342
|
|
|
'next_page' => $nextPage, |
343
|
|
|
'first_page' => 1, |
344
|
|
|
'last_page' => $totalResults, |
345
|
|
|
]; |
346
|
|
|
|
347
|
|
|
$results = [ |
348
|
|
|
'meta' => $pagination, |
349
|
|
|
'data' => $data, |
350
|
|
|
]; |
351
|
|
|
|
352
|
|
|
if ($this->isLazyCollection) { |
353
|
|
|
return LazyCollection::make($results); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if ($this->isCollection) { |
357
|
|
|
return collect($results); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return $results; |
361
|
|
|
} catch (\Exception $ex) { |
362
|
|
|
throw new \Exception($ex->getMessage(), 1); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Count all results. |
368
|
|
|
* |
369
|
|
|
* @return int |
370
|
|
|
*/ |
371
|
|
|
protected function count() |
372
|
|
|
{ |
373
|
|
|
try { |
374
|
|
|
$results = WooCommerce::all($this->endpoint, $this->options); |
|
|
|
|
375
|
|
|
$totalResults = WooCommerce::countResults(); |
376
|
|
|
|
377
|
|
|
return $totalResults; |
378
|
|
|
} catch (\Exception $ex) { |
379
|
|
|
throw new \Exception($ex->getMessage(), 1); |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* Store data. |
385
|
|
|
* |
386
|
|
|
* @return array |
387
|
|
|
*/ |
388
|
|
View Code Duplication |
public function save() |
|
|
|
|
389
|
|
|
{ |
390
|
|
|
$this->results = WooCommerce::create($this->endpoint, $this->properties); |
|
|
|
|
391
|
|
|
|
392
|
|
|
if ($this->isLazyCollection) { |
393
|
|
|
return LazyCollection::make($this->results); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
if ($this->isCollection) { |
397
|
|
|
return collect($this->results); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
return $this->results; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
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.