Skip to content

Commit a63d8f1

Browse files
committed
Added dynamic where query builder
1 parent d5512a4 commit a63d8f1

File tree

2 files changed

+62
-37
lines changed

2 files changed

+62
-37
lines changed

src/Models/BaseModel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public function __set($name, $value)
3333

3434
public function __call($method, $parameters)
3535
{
36+
if(!method_exists($this, $method)) {
37+
preg_match_all('/((?:^|[A-Z])[a-z]+)/', $method, $partials);
38+
$method = array_shift($partials[0]);
39+
if(!method_exists($this, $method)) {
40+
throw new \Exception("Sorry! you are calling wrong method");
41+
}
42+
array_unshift($parameters, strtolower(implode('_', $partials[0])));
43+
}
3644
return $this->$method(...$parameters);
3745
}
3846

src/Traits/QueryBuilderTrait.php

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,7 @@ protected function get()
103103
{
104104
try {
105105
$results = WooCommerce::all($this->endpoint, $this->options);
106-
107-
if (empty($this->where)) {
108-
return $results;
109-
}
110-
$filteredResults = [];
111-
foreach ($this->where as $key => $where) {
112-
foreach ($results as $result) {
113-
$name = $where['name'];
114-
$name = $result->$name;
115-
$operator = ($where['operator'] == '=') ? $where['operator'].'=' : $where['operator'];
116-
$value = $where['value'];
117-
$condition = "'$name' $operator '$value'";
118-
if (eval("return $condition;")) {
119-
$filteredResults[] = $result;
120-
}
121-
}
122-
}
123-
124-
return $filteredResults;
106+
return $results;
125107
} catch (\Exception $ex) {
126108
throw new \Exception($ex->getMessage(), 1);
127109
}
@@ -170,25 +152,60 @@ protected function options($parameters)
170152
*/
171153
protected function where(...$parameters)
172154
{
173-
if (count($parameters) == 3) {
174-
$where = [
175-
'name' => $parameters[0],
176-
'operator' => $parameters[1],
177-
'value' => $parameters[2],
178-
];
179-
$this->where[] = $where;
180-
}
181-
182-
if (count($parameters) == 2) {
183-
$this->options[$parameters[0]] = $parameters[1];
155+
if (count($parameters) < 2 || count($parameters) > 3) {
156+
throw new \Exception("Too many arguments. You can pass minimum 2 and maximum 3 paramneters");
184157
}
185-
186-
if (count($parameters) == 1) {
187-
foreach ($parameters as $parameter) {
188-
foreach ($parameter as $key => $value) {
189-
$this->options[$key] = $value;
190-
}
191-
}
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;
192209
}
193210

194211
return $this;

0 commit comments

Comments
 (0)