Skip to content

Commit 0875c55

Browse files
committed
Added BaseBodel class to load static, nonstatic and inaccisible data
1 parent 1ace946 commit 0875c55

File tree

7 files changed

+163
-37
lines changed

7 files changed

+163
-37
lines changed

src/Models/BaseModel.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Codexshaper\WooCommerce\Models;
4+
5+
use Codexshaper\WooCommerce\Facades\WooCommerce;
6+
7+
class BaseModel
8+
{
9+
protected $properties = [];
10+
11+
/**
12+
* Get Inaccessible Property
13+
*
14+
* @param string $name
15+
*
16+
* @return int|string|array|object|null
17+
*/
18+
public function __get($name)
19+
{
20+
return $this->$name;
21+
}
22+
23+
/**
24+
* Set Option
25+
*
26+
* @param string $name
27+
* @param string $value
28+
*
29+
* @return void
30+
*/
31+
public function __set($name, $value)
32+
{
33+
$this->properties[$name] = $value;
34+
}
35+
36+
public function __call($method, $parameters)
37+
{
38+
return $this->$method(...$parameters);
39+
}
40+
41+
public static function __callStatic($method, $parameters)
42+
{
43+
return (new static )->$method(...$parameters);
44+
}
45+
}

src/Models/Customer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Codexshaper\WooCommerce\Facades\WooCommerce;
66
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
77

8-
class Customer
8+
class Customer extends BaseModel
99
{
1010
use QueryBuilderTrait;
1111

src/Models/Order.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Codexshaper\WooCommerce\Facades\WooCommerce;
66
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
77

8-
class Order
8+
class Order extends BaseModel
99
{
1010
use QueryBuilderTrait;
1111

src/Models/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Codexshaper\WooCommerce\Facades\WooCommerce;
66
use Codexshaper\WooCommerce\Traits\QueryBuilderTrait;
77

8-
class Product
8+
class Product extends BaseModel
99
{
1010
use QueryBuilderTrait;
1111

src/Traits/QueryBuilderTrait.php

Lines changed: 100 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,80 @@ trait QueryBuilderTrait
1010
* @var $options
1111
*/
1212
protected $options = [];
13+
/**
14+
* @var $options
15+
*/
16+
protected $where = [];
17+
/**
18+
* @var $results
19+
*/
20+
protected $properties = [];
1321

1422
/**
15-
* Retrieve all products
23+
* Retrieve all Items
1624
*
1725
* @param array $options
1826
*
1927
* @return array
2028
*/
21-
public function all($options = [])
29+
protected function all($options = [])
2230
{
2331
return WooCommerce::all($this->endpoint, $options);
2432
}
2533

2634
/**
27-
* Retrieve single product
35+
* Retrieve single Item
2836
*
2937
* @param integer $id
3038
* @param array $options
3139
*
3240
* @return object
3341
*/
34-
public function find($id, $options = [])
42+
protected function find($id, $options = [])
3543
{
36-
return collect(WooCommerce::find("{$this->endpoint}/{$id}", $options));
44+
$result = collect(WooCommerce::find("{$this->endpoint}/{$id}", $options));
45+
return $this;
3746
}
3847

3948
/**
40-
* Create new product
49+
* Create new Item
4150
*
4251
* @param array $data
4352
*
4453
* @return object
4554
*/
46-
public function create($data)
55+
protected function create($data)
4756
{
4857
return WooCommerce::create($this->endpoint, $data);
4958
}
5059

51-
public function update($id, $data)
60+
/**
61+
* Update Existing Item
62+
*
63+
* @param integer $id
64+
* @param array $data
65+
*
66+
* @return object
67+
*/
68+
protected function update($id, $data)
5269
{
5370
return WooCommerce::update("{$this->endpoint}/{$id}", $data);
5471
}
5572

56-
public function delete($id, $options = [])
73+
/**
74+
* Destroy Item
75+
*
76+
* @param integer $id
77+
* @param array $options
78+
*
79+
* @return object
80+
*/
81+
protected function delete($id, $options = [])
5782
{
5883
return WooCommerce::delete("{$this->endpoint}/{$id}", $options);
5984
}
6085

61-
public function batch($data)
86+
protected function batch($data)
6287
{
6388
return WooCommerce::create('{$this->endpoint}/batch', $options);
6489
}
@@ -68,37 +93,37 @@ public function batch($data)
6893
*
6994
* @return array
7095
*/
71-
public function get()
96+
protected function get()
7297
{
73-
$orders = WooCommerce::all($this->endpoint, $this->options);
98+
$results = WooCommerce::all($this->endpoint, $this->options);
7499

75100
if (empty($this->where)) {
76-
return $orders;
101+
return $results;
77102
}
78-
$filteredOrders = [];
103+
$filteredResults = [];
79104
foreach ($this->where as $key => $where) {
80105

81-
foreach ($orders as $order) {
106+
foreach ($results as $result) {
82107
$name = $where['name'];
83-
$name = $order->$name;
108+
$name = $result->$name;
84109
$operator = ($where['operator'] == '=') ? $where['operator'] . "=" : $where['operator'];
85110
$value = $where['value'];
86111
$condition = "'$name' $operator '$value'";
87112
if (eval("return $condition;")) {
88-
$filteredOrders[] = $order;
113+
$filteredResults[] = $result;
89114
}
90115
}
91116
}
92117

93-
return $filteredOrders;
118+
return $filteredResults;
94119
}
95120

96121
/**
97122
* Retrieve data
98123
*
99124
* @return object
100125
*/
101-
public function first()
126+
protected function first()
102127
{
103128
return collect($this->get()[0]);
104129
}
@@ -110,7 +135,7 @@ public function first()
110135
*
111136
* @return object $this
112137
*/
113-
public function options($parameters)
138+
protected function options($parameters)
114139
{
115140
if (!is_array($parameters)) {
116141
throw new \Exception("Options must be an array", 1);
@@ -135,7 +160,7 @@ public function options($parameters)
135160
*
136161
* @return object $this
137162
*/
138-
public function where(...$parameters)
163+
protected function where(...$parameters)
139164
{
140165
if (count($parameters) == 3) {
141166
$where = [
@@ -167,11 +192,64 @@ public function where(...$parameters)
167192
*
168193
* @return object $this
169194
*/
170-
public function orderBy($name, $direction = 'desc')
195+
protected function orderBy($name, $direction = 'desc')
171196
{
172197
$this->options['orderby'] = $name;
173198
$this->options['order'] = $direction;
174199

175200
return $this;
176201
}
202+
203+
/**
204+
*
205+
* @param integer $per_page
206+
* @param integer $current_page
207+
*
208+
* @return array
209+
*/
210+
protected function paginate($per_page, $current_page = 1)
211+
{
212+
$this->options['per_page'] = (int) $per_page;
213+
214+
if ($current_page > 0) {
215+
$this->options['page'] = (int) $current_page;
216+
}
217+
218+
$results = $this->get();
219+
$totalResults = WooCommerce::countResults();
220+
$totalPages = WooCommerce::countPages();
221+
$currentPage = WooCommerce::current();
222+
$previousPage = WooCommerce::previous();
223+
$nextPage = WooCommerce::next();
224+
225+
$pagination = [
226+
'total_results' => $totalResults,
227+
'total_pages' => $totalPages,
228+
'current_page' => $currentPage,
229+
'previous_page' => $previousPage,
230+
'next_page' => $nextPage,
231+
'first_page' => 1,
232+
'last_page' => $totalResults,
233+
];
234+
235+
$results['pagination'] = $pagination;
236+
237+
return $results;
238+
}
239+
240+
/**
241+
*
242+
* @return array
243+
*/
244+
public function save()
245+
{
246+
$this->results = WooCommerce::create($this->endpoint, $this->properties);
247+
248+
return $this->results;
249+
}
250+
251+
public function hello()
252+
{
253+
return "Called";
254+
}
177255
}

src/Traits/WoocommerceTrait.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,6 @@ public function getResponse()
9494
return $this->client->http->getResponse();
9595
}
9696

97-
/**
98-
* Return the current page number
99-
*
100-
* @return int
101-
*/
102-
public function current()
103-
{
104-
return !empty($this->getRequest()->getParameters()['page']) ? $this->getRequest()->getParameters()['page'] : 1;
105-
}
106-
10797
/**
10898
* Count the total results and return it
10999
*
@@ -124,6 +114,16 @@ public function countPages()
124114
return (int) $this->getResponse()->getHeaders()['X-WP-TotalPages'];
125115
}
126116

117+
/**
118+
* Return the current page number
119+
*
120+
* @return int
121+
*/
122+
public function current()
123+
{
124+
return !empty($this->getRequest()->getParameters()['page']) ? $this->getRequest()->getParameters()['page'] : 1;
125+
}
126+
127127
/**
128128
* Return the previous page number
129129
*
@@ -132,7 +132,7 @@ public function countPages()
132132
public function previous()
133133
{
134134
$currentPage = $this->current();
135-
return (--$currentPage > 1) ? $currentPage : null;
135+
return ($currentPage-- > 1) ? $currentPage : null;
136136
}
137137

138138
/**
@@ -143,6 +143,6 @@ public function previous()
143143
public function next()
144144
{
145145
$currentPage = $this->current();
146-
return (++$currentPage < $this->countPages()) ? $currentPage : null;
146+
return ($currentPage++ < $this->countPages()) ? $currentPage : null;
147147
}
148148
}

src/WoocommerceApi.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class WooCommerceApi
99
{
1010
use WooCommerceTrait;
1111

12+
/**
13+
*@var \Automattic\WooCommerce\Client
14+
*/
1215
protected $client;
1316

1417
/**

0 commit comments

Comments
 (0)