Skip to content

Commit

Permalink
Added Query Builder Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
maab16 committed Sep 24, 2019
1 parent 2029cb0 commit 2e468fb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 34 deletions.
3 changes: 3 additions & 0 deletions src/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Codexshaper\WooCommerce\Models;

use Codexshaper\WooCommerce\Facades\WooCommerce;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;

class Customer
{
use QueryBuilderTrait;

public function all($options = [])
{
return WooCommerce::all('customers', $options);
Expand Down
36 changes: 3 additions & 33 deletions src/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Codexshaper\WooCommerce\Models;

use Codexshaper\WooCommerce\Facades\WooCommerce;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;

class Order
{
protected $options = [];
protected $results = [];
use QueryBuilderTrait;

public function all($options = [])
{
Expand All @@ -16,7 +16,7 @@ public function all($options = [])

public function find($id, $options = [])
{
return WooCommerce::find("orders/{$id}", $options);
return collect(WooCommerce::find("orders/{$id}", $options));
}

public function create($data)
Expand Down Expand Up @@ -86,34 +86,4 @@ public function deleteRefund($order_id, $refund_id, $options = [])
{
return WooCommerce::delete("orders/{$order_id}/refunds/{$refund_id}", $options);
}

/* Custom Query */
public function where(...$parameters)
{
if (count($parameters) == 2) {
$this->options[$parameters[0]] = $parameters[1];
}

if (count($parameters) == 1) {

foreach ($parameters as $parameter) {

foreach ($parameter as $key => $value) {
$this->options[$key] = $value;
}
}
}
return $this;
}

public function get()
{
return WooCommerce::all('orders', $this->options);
}

public function first()
{
$order = $this->get()[0];
return ['order' => $order];
}
}
5 changes: 4 additions & 1 deletion src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
namespace Codexshaper\WooCommerce\Models;

use Codexshaper\WooCommerce\Facades\WooCommerce;
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;

class Product
{
use QueryBuilderTrait;

public function all($options = [])
{
return WooCommerce::all('products', $options);
}

public function find($id, $options = [])
{
return WooCommerce::find("products/{$id}", $options);
return collect(WooCommerce::find("products/{$id}", $options));
}

public function create($data)
Expand Down
67 changes: 67 additions & 0 deletions src/Traits/QueryBuilderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Codexshaper\WooCommerce\Traits;

use Codexshaper\WooCommerce\Facades\WooCommerce;

trait QueryBuilderTrait
{

protected $options;

/* Custom Query */

public function get()
{
return WooCommerce::all('products', $this->options);
}

public function first()
{
return collect($this->get()[0]);
}

public function options(array $parameters)
{
if (!is_array($parameters)) {
throw new \Exception("Options must be an array", 1);
}

if (empty($parameters)) {
throw new \Exception("Options must be pass at least one element", 1);
}

foreach ($parameters as $key => $value) {

$this->options[$key] = $value;
}

return $this;
}

public function where(...$parameters)
{
if (count($parameters) == 2) {
$this->options[$parameters[0]] = $parameters[1];
}

if (count($parameters) == 1) {

foreach ($parameters as $parameter) {

foreach ($parameter as $key => $value) {
$this->options[$key] = $value;
}
}
}
return $this;
}

public function orderBy($name, $direction = 'desc')
{
$this->options['orderby'] = $name;
$this->options['order'] = $direction;

return $this;
}
}

0 comments on commit 2e468fb

Please sign in to comment.