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 collect(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
|
|
|
$results = WooCommerce::all($this->endpoint, $this->options); |
105
|
|
|
|
106
|
|
|
if (empty($this->where)) { |
107
|
|
|
return $results; |
108
|
|
|
} |
109
|
|
|
$filteredResults = []; |
110
|
|
|
foreach ($this->where as $key => $where) { |
111
|
|
|
foreach ($results as $result) { |
112
|
|
|
$name = $where['name']; |
113
|
|
|
$name = $result->$name; |
114
|
|
|
$operator = ($where['operator'] == '=') ? $where['operator'].'=' : $where['operator']; |
115
|
|
|
$value = $where['value']; |
116
|
|
|
$condition = "'$name' $operator '$value'"; |
117
|
|
|
if (eval("return $condition;")) { |
118
|
|
|
$filteredResults[] = $result; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $filteredResults; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Retrieve data. |
128
|
|
|
* |
129
|
|
|
* @return object |
130
|
|
|
*/ |
131
|
|
|
protected function first() |
132
|
|
|
{ |
133
|
|
|
return collect($this->get()[0]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set options for woocommerce request. |
138
|
|
|
* |
139
|
|
|
* @param array $parameters |
140
|
|
|
* |
141
|
|
|
* @return object $this |
142
|
|
|
*/ |
143
|
|
|
protected function options($parameters) |
144
|
|
|
{ |
145
|
|
|
if (!is_array($parameters)) { |
146
|
|
|
throw new \Exception('Options must be an array', 1); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (empty($parameters)) { |
150
|
|
|
throw new \Exception('Options must be pass at least one element', 1); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
foreach ($parameters as $key => $value) { |
154
|
|
|
$this->options[$key] = $value; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Join options for woocommerce request. |
162
|
|
|
* |
163
|
|
|
* @param array $parameters |
164
|
|
|
* |
165
|
|
|
* @return object $this |
166
|
|
|
*/ |
167
|
|
|
protected function where(...$parameters) |
168
|
|
|
{ |
169
|
|
|
if (count($parameters) == 3) { |
170
|
|
|
$where = [ |
171
|
|
|
'name' => $parameters[0], |
172
|
|
|
'operator' => $parameters[1], |
173
|
|
|
'value' => $parameters[2], |
174
|
|
|
]; |
175
|
|
|
$this->where[] = $where; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (count($parameters) == 2) { |
179
|
|
|
$this->options[$parameters[0]] = $parameters[1]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (count($parameters) == 1) { |
183
|
|
|
foreach ($parameters as $parameter) { |
184
|
|
|
foreach ($parameter as $key => $value) { |
185
|
|
|
$this->options[$key] = $value; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Set order direction. |
195
|
|
|
* |
196
|
|
|
* @param string $name |
197
|
|
|
* @param string $direction |
198
|
|
|
* |
199
|
|
|
* @return object $this |
200
|
|
|
*/ |
201
|
|
|
protected function orderBy($name, $direction = 'desc') |
202
|
|
|
{ |
203
|
|
|
$this->options['orderby'] = $name; |
204
|
|
|
$this->options['order'] = $direction; |
205
|
|
|
|
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Paginate results. |
211
|
|
|
* |
212
|
|
|
* @param int $per_page |
213
|
|
|
* @param int $current_page |
214
|
|
|
* |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
|
|
protected function paginate($per_page, $current_page = 1) |
218
|
|
|
{ |
219
|
|
|
$this->options['per_page'] = (int) $per_page; |
220
|
|
|
|
221
|
|
|
if ($current_page > 0) { |
222
|
|
|
$this->options['page'] = (int) $current_page; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$results = $this->get(); |
226
|
|
|
$totalResults = WooCommerce::countResults(); |
227
|
|
|
$totalPages = WooCommerce::countPages(); |
228
|
|
|
$currentPage = WooCommerce::current(); |
229
|
|
|
$previousPage = WooCommerce::previous(); |
230
|
|
|
$nextPage = WooCommerce::next(); |
231
|
|
|
|
232
|
|
|
$pagination = [ |
233
|
|
|
'total_results' => $totalResults, |
234
|
|
|
'total_pages' => $totalPages, |
235
|
|
|
'current_page' => $currentPage, |
236
|
|
|
'previous_page' => $previousPage, |
237
|
|
|
'next_page' => $nextPage, |
238
|
|
|
'first_page' => 1, |
239
|
|
|
'last_page' => $totalResults, |
240
|
|
|
]; |
241
|
|
|
|
242
|
|
|
$results['pagination'] = $pagination; |
243
|
|
|
|
244
|
|
|
return $results; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Count all results. |
249
|
|
|
* |
250
|
|
|
* @return int |
251
|
|
|
*/ |
252
|
|
|
protected function count() |
253
|
|
|
{ |
254
|
|
|
$results = WooCommerce::all($this->endpoint, $this->options); |
|
|
|
|
255
|
|
|
$totalResults = WooCommerce::countResults(); |
256
|
|
|
|
257
|
|
|
return $totalResults; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Store data. |
262
|
|
|
* |
263
|
|
|
* @return array |
264
|
|
|
*/ |
265
|
|
|
public function save() |
266
|
|
|
{ |
267
|
|
|
$this->results = WooCommerce::create($this->endpoint, $this->properties); |
|
|
|
|
268
|
|
|
|
269
|
|
|
return $this->results; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
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: