1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codexshaper\WooCommerce\Traits; |
4
|
|
|
|
5
|
|
|
use Codexshaper\WooCommerce\Facades\WooCommerce; |
6
|
|
|
|
7
|
|
|
trait QueryBuilderTrait |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var |
11
|
|
|
*/ |
12
|
|
|
protected $options = []; |
13
|
|
|
/** |
14
|
|
|
* @var |
15
|
|
|
*/ |
16
|
|
|
protected $where = []; |
17
|
|
|
/** |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
protected $properties = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Retrieve all Items. |
24
|
|
|
* |
25
|
|
|
* @param array $options |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
protected function all($options = []) |
30
|
|
|
{ |
31
|
|
|
return WooCommerce::all($this->endpoint, $options); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Retrieve single Item. |
36
|
|
|
* |
37
|
|
|
* @param int $id |
38
|
|
|
* @param array $options |
39
|
|
|
* |
40
|
|
|
* @return object |
41
|
|
|
*/ |
42
|
|
|
protected function find($id, $options = []) |
43
|
|
|
{ |
44
|
|
|
return WooCommerce::find("{$this->endpoint}/{$id}", $options); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create new Item. |
49
|
|
|
* |
50
|
|
|
* @param array $data |
51
|
|
|
* |
52
|
|
|
* @return object |
53
|
|
|
*/ |
54
|
|
|
protected function create($data) |
55
|
|
|
{ |
56
|
|
|
return WooCommerce::create($this->endpoint, $data); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Update Existing Item. |
61
|
|
|
* |
62
|
|
|
* @param int $id |
63
|
|
|
* @param array $data |
64
|
|
|
* |
65
|
|
|
* @return object |
66
|
|
|
*/ |
67
|
|
|
protected function update($id, $data) |
68
|
|
|
{ |
69
|
|
|
return WooCommerce::update("{$this->endpoint}/{$id}", $data); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Destroy Item. |
74
|
|
|
* |
75
|
|
|
* @param int $id |
76
|
|
|
* @param array $options |
77
|
|
|
* |
78
|
|
|
* @return object |
79
|
|
|
*/ |
80
|
|
|
protected function delete($id, $options = []) |
81
|
|
|
{ |
82
|
|
|
return WooCommerce::delete("{$this->endpoint}/{$id}", $options); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Batch Update. |
87
|
|
|
* |
88
|
|
|
* @param array $data |
89
|
|
|
* |
90
|
|
|
* @return object |
91
|
|
|
*/ |
92
|
|
|
protected function batch($data) |
93
|
|
|
{ |
94
|
|
|
return WooCommerce::create("{$this->endpoint}/batch", $data); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Retrieve data. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
protected function get() |
103
|
|
|
{ |
104
|
|
|
try { |
105
|
|
|
$results = WooCommerce::all($this->endpoint, $this->options); |
106
|
|
|
return $results; |
107
|
|
|
} catch (\Exception $ex) { |
108
|
|
|
throw new \Exception($ex->getMessage(), 1); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieve data. |
114
|
|
|
* |
115
|
|
|
* @return object |
116
|
|
|
*/ |
117
|
|
|
protected function first() |
118
|
|
|
{ |
119
|
|
|
return $this->get()[0] ?? new \stdClass(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set options for woocommerce request. |
124
|
|
|
* |
125
|
|
|
* @param array $parameters |
126
|
|
|
* |
127
|
|
|
* @return object $this |
128
|
|
|
*/ |
129
|
|
|
protected function options($parameters) |
130
|
|
|
{ |
131
|
|
|
if (!is_array($parameters)) { |
132
|
|
|
throw new \Exception('Options must be an array', 1); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (empty($parameters)) { |
136
|
|
|
throw new \Exception('Options must be pass at least one element', 1); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
foreach ($parameters as $key => $value) { |
140
|
|
|
$this->options[$key] = $value; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Join options for woocommerce request. |
148
|
|
|
* |
149
|
|
|
* @param array $parameters |
150
|
|
|
* |
151
|
|
|
* @return object $this |
152
|
|
|
*/ |
153
|
|
|
protected function where(...$parameters) |
154
|
|
|
{ |
155
|
|
|
if (count($parameters) < 2 || count($parameters) > 3) { |
156
|
|
|
throw new \Exception("Too many arguments. You can pass minimum 2 and maximum 3 paramneters"); |
157
|
|
|
} |
158
|
|
|
$field = $parameters[0]; |
159
|
|
|
$value = count($parameters) == 3 ? $parameters[2] : $parameters[1]; |
160
|
|
|
|
161
|
|
|
switch ($field) { |
162
|
|
|
case 'name': |
163
|
|
|
case 'title': |
164
|
|
|
case 'description': |
165
|
|
|
$this->options['search'] = $value; |
166
|
|
|
break; |
167
|
|
|
case 'sku': |
168
|
|
|
$this->options['sku'] = $value; |
169
|
|
|
break; |
170
|
|
|
case 'type': |
171
|
|
|
$this->options['type'] = $value; |
172
|
|
|
break; |
173
|
|
|
case 'category': |
174
|
|
|
$this->options['category'] = $value; |
175
|
|
|
break; |
176
|
|
|
case 'tag': |
177
|
|
|
$this->options['tag'] = $value; |
178
|
|
|
break; |
179
|
|
|
case 'after': |
180
|
|
|
$this->options['after'] = $value; |
181
|
|
|
break; |
182
|
|
|
case 'before': |
183
|
|
|
$this->options['before'] = $value; |
184
|
|
|
break; |
185
|
|
|
case 'attribute': |
186
|
|
|
$this->options['attribute'] = $value; |
187
|
|
|
break; |
188
|
|
|
case 'attribute_term': |
189
|
|
|
$this->options['attribute_term'] = $value; |
190
|
|
|
break; |
191
|
|
|
case 'in_stock': |
192
|
|
|
$this->options['in_stock'] = $value; |
193
|
|
|
break; |
194
|
|
|
case 'featured': |
195
|
|
|
$this->options['featured'] = $value; |
196
|
|
|
break; |
197
|
|
|
case 'min_price': |
198
|
|
|
$this->options['min_price'] = $value; |
199
|
|
|
break; |
200
|
|
|
case 'max_price': |
201
|
|
|
$this->options['max_price'] = $value; |
202
|
|
|
break; |
203
|
|
|
case 'shipping_class': |
204
|
|
|
$this->options['shipping_class'] = $value; |
205
|
|
|
break; |
206
|
|
|
case 'tax_class': |
207
|
|
|
$this->options['tax_class'] = $value; |
208
|
|
|
break; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Set order direction. |
216
|
|
|
* |
217
|
|
|
* @param string $name |
218
|
|
|
* @param string $direction |
219
|
|
|
* |
220
|
|
|
* @return object $this |
221
|
|
|
*/ |
222
|
|
|
protected function orderBy($name, $direction = 'desc') |
223
|
|
|
{ |
224
|
|
|
$this->options['orderby'] = $name; |
225
|
|
|
$this->options['order'] = $direction; |
226
|
|
|
|
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Paginate results. |
232
|
|
|
* |
233
|
|
|
* @param int $per_page |
234
|
|
|
* @param int $current_page |
235
|
|
|
* |
236
|
|
|
* @return array |
237
|
|
|
*/ |
238
|
|
|
protected function paginate($per_page, $current_page = 1) |
239
|
|
|
{ |
240
|
|
|
try { |
241
|
|
|
$this->options['per_page'] = (int) $per_page; |
242
|
|
|
|
243
|
|
|
if ($current_page > 0) { |
244
|
|
|
$this->options['page'] = (int) $current_page; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$results = $this->get(); |
248
|
|
|
$totalResults = WooCommerce::countResults(); |
249
|
|
|
$totalPages = WooCommerce::countPages(); |
250
|
|
|
$currentPage = WooCommerce::current(); |
251
|
|
|
$previousPage = WooCommerce::previous(); |
252
|
|
|
$nextPage = WooCommerce::next(); |
253
|
|
|
|
254
|
|
|
$pagination = [ |
255
|
|
|
'total_results' => $totalResults, |
256
|
|
|
'total_pages' => $totalPages, |
257
|
|
|
'current_page' => $currentPage, |
258
|
|
|
'previous_page' => $previousPage, |
259
|
|
|
'next_page' => $nextPage, |
260
|
|
|
'first_page' => 1, |
261
|
|
|
'last_page' => $totalResults, |
262
|
|
|
]; |
263
|
|
|
|
264
|
|
|
$results['pagination'] = $pagination; |
265
|
|
|
|
266
|
|
|
return $results; |
267
|
|
|
} catch (\Exception $ex) { |
268
|
|
|
throw new \Exception($ex->getMessage(), 1); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Count all results. |
274
|
|
|
* |
275
|
|
|
* @return int |
276
|
|
|
*/ |
277
|
|
|
protected function count() |
278
|
|
|
{ |
279
|
|
|
try { |
280
|
|
|
$results = WooCommerce::all($this->endpoint, $this->options); |
|
|
|
|
281
|
|
|
$totalResults = WooCommerce::countResults(); |
282
|
|
|
|
283
|
|
|
return $totalResults; |
284
|
|
|
} catch (\Exception $ex) { |
285
|
|
|
throw new \Exception($ex->getMessage(), 1); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Store data. |
291
|
|
|
* |
292
|
|
|
* @return array |
293
|
|
|
*/ |
294
|
|
|
public function save() |
295
|
|
|
{ |
296
|
|
|
$this->results = WooCommerce::create($this->endpoint, $this->properties); |
|
|
|
|
297
|
|
|
|
298
|
|
|
return $this->results; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
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: