Skip to content

Commit

Permalink
Added operators in where clause
Browse files Browse the repository at this point in the history
  • Loading branch information
maab16 committed Sep 25, 2019
1 parent daf793d commit 6f66e0d
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Traits/QueryBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,27 @@ trait QueryBuilderTrait
*/
public function get()
{
return WooCommerce::all($this->endpoint, $this->options);
$orders = WooCommerce::all($this->endpoint, $this->options);

if (empty($this->where)) {
return $orders;
}
$filteredOrders = [];
foreach ($this->where as $key => $where) {

foreach ($orders as $order) {
$name = $where['name'];
$name = $order->$name;
$operator = ($where['operator'] == '=') ? $where['operator'] . "=" : $where['operator'];
$value = $where['value'];
$condition = "'$name' $operator '$value'";
if (eval("return $condition;")) {
$filteredOrders[] = $order;
}
}
}

return $filteredOrders;
}

/**
Expand Down Expand Up @@ -65,6 +85,15 @@ public function options($parameters)
*/
public function where(...$parameters)
{
if (count($parameters) == 3) {
$where = [
'name' => $parameters[0],
'operator' => $parameters[1],
'value' => $parameters[2],
];
$this->where[] = $where;
}

if (count($parameters) == 2) {
$this->options[$parameters[0]] = $parameters[1];
}
Expand Down

0 comments on commit 6f66e0d

Please sign in to comment.