@@ -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}
0 commit comments