Skip to content

Commit 2e468fb

Browse files
committed
Added Query Builder Trait
1 parent 2029cb0 commit 2e468fb

File tree

4 files changed

+77
-34
lines changed

4 files changed

+77
-34
lines changed

src/Models/Customer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace Codexshaper\WooCommerce\Models;
44

55
use Codexshaper\WooCommerce\Facades\WooCommerce;
6+
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
67

78
class Customer
89
{
10+
use QueryBuilderTrait;
11+
912
public function all($options = [])
1013
{
1114
return WooCommerce::all('customers', $options);

src/Models/Order.php

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace Codexshaper\WooCommerce\Models;
44

55
use Codexshaper\WooCommerce\Facades\WooCommerce;
6+
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
67

78
class Order
89
{
9-
protected $options = [];
10-
protected $results = [];
10+
use QueryBuilderTrait;
1111

1212
public function all($options = [])
1313
{
@@ -16,7 +16,7 @@ public function all($options = [])
1616

1717
public function find($id, $options = [])
1818
{
19-
return WooCommerce::find("orders/{$id}", $options);
19+
return collect(WooCommerce::find("orders/{$id}", $options));
2020
}
2121

2222
public function create($data)
@@ -86,34 +86,4 @@ public function deleteRefund($order_id, $refund_id, $options = [])
8686
{
8787
return WooCommerce::delete("orders/{$order_id}/refunds/{$refund_id}", $options);
8888
}
89-
90-
/* Custom Query */
91-
public function where(...$parameters)
92-
{
93-
if (count($parameters) == 2) {
94-
$this->options[$parameters[0]] = $parameters[1];
95-
}
96-
97-
if (count($parameters) == 1) {
98-
99-
foreach ($parameters as $parameter) {
100-
101-
foreach ($parameter as $key => $value) {
102-
$this->options[$key] = $value;
103-
}
104-
}
105-
}
106-
return $this;
107-
}
108-
109-
public function get()
110-
{
111-
return WooCommerce::all('orders', $this->options);
112-
}
113-
114-
public function first()
115-
{
116-
$order = $this->get()[0];
117-
return ['order' => $order];
118-
}
11989
}

src/Models/Product.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
namespace Codexshaper\WooCommerce\Models;
44

55
use Codexshaper\WooCommerce\Facades\WooCommerce;
6+
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
67

78
class Product
89
{
10+
use QueryBuilderTrait;
11+
912
public function all($options = [])
1013
{
1114
return WooCommerce::all('products', $options);
1215
}
1316

1417
public function find($id, $options = [])
1518
{
16-
return WooCommerce::find("products/{$id}", $options);
19+
return collect(WooCommerce::find("products/{$id}", $options));
1720
}
1821

1922
public function create($data)

src/Traits/QueryBuilderTrait.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Codexshaper\WooCommerce\Traits;
4+
5+
use Codexshaper\WooCommerce\Facades\WooCommerce;
6+
7+
trait QueryBuilderTrait
8+
{
9+
10+
protected $options;
11+
12+
/* Custom Query */
13+
14+
public function get()
15+
{
16+
return WooCommerce::all('products', $this->options);
17+
}
18+
19+
public function first()
20+
{
21+
return collect($this->get()[0]);
22+
}
23+
24+
public function options(array $parameters)
25+
{
26+
if (!is_array($parameters)) {
27+
throw new \Exception("Options must be an array", 1);
28+
}
29+
30+
if (empty($parameters)) {
31+
throw new \Exception("Options must be pass at least one element", 1);
32+
}
33+
34+
foreach ($parameters as $key => $value) {
35+
36+
$this->options[$key] = $value;
37+
}
38+
39+
return $this;
40+
}
41+
42+
public function where(...$parameters)
43+
{
44+
if (count($parameters) == 2) {
45+
$this->options[$parameters[0]] = $parameters[1];
46+
}
47+
48+
if (count($parameters) == 1) {
49+
50+
foreach ($parameters as $parameter) {
51+
52+
foreach ($parameter as $key => $value) {
53+
$this->options[$key] = $value;
54+
}
55+
}
56+
}
57+
return $this;
58+
}
59+
60+
public function orderBy($name, $direction = 'desc')
61+
{
62+
$this->options['orderby'] = $name;
63+
$this->options['order'] = $direction;
64+
65+
return $this;
66+
}
67+
}

0 commit comments

Comments
 (0)