Skip to content

Commit 56652e2

Browse files
authored
Add pagination functionality (pixelpeter#15)
* Add methods to get http request/response headers fro the last api call * Add pagination functionality and tests * Update README and CHANGELOG
1 parent 3f23f80 commit 56652e2

File tree

4 files changed

+403
-0
lines changed

4 files changed

+403
-0
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
All Notable changes for the Laravel 5 WooCommerce REST API Client will be documented in this file
44

5+
## 2.3.0
6+
- Add pagination functionality.
7+
Now these functions are available
8+
- totalResults()
9+
- firstPage()
10+
- lastPage()
11+
- currentPage()
12+
- totalPages()
13+
- previousPage()
14+
- nextPage()
15+
- hasPreviousPage()
16+
- hasNextPage()
17+
- hasNotPreviousPage()
18+
- hasNotNextPage()
19+
- Get the Request & Response (Headers) from the last request
20+
- getRequest()
21+
- getResponse()
22+
523
## 2.2.0
624
- Laravel 5.4 compatibility
725

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,54 @@ $data = [
152152
return Woocommerce::put('products/1', $data);
153153
```
154154

155+
### Pagination
156+
So you don't have to mess around with the request and response header and the calculations this wrapper will do all the heavy lifting for you.
157+
(WC 2.6.x or later, WP 4.4 or later)
158+
159+
```php
160+
use Woocommerce;
161+
162+
// assuming we have 474 orders in pur result
163+
// we will request page 5 with 25 results per page
164+
$params = [
165+
'per_page' => 25,
166+
'page' => 5
167+
];
168+
169+
Woocommerce::get('orders', $params);
170+
171+
Woocommerce::totalResults(); // 474
172+
Woocommerce::firstPage(); // 1
173+
Woocommerce::lastPage(); // 19
174+
Woocommerce::currentPage(); // 5
175+
Woocommerce::totalPages(); // 19
176+
Woocommerce::previousPage(); // 4
177+
Woocommerce::nextPage(); // 6
178+
Woocommerce::hasPreviousPage(); // true
179+
Woocommerce::hasNextPage(); // true
180+
Woocommerce::hasNotPreviousPage(); // false
181+
Woocommerce::hasNotNextPage(); // false
182+
```
183+
184+
### HTTP Request & Response (Headers)
185+
186+
```php
187+
use Woocommerce;
188+
189+
// first send a request
190+
Woocommerce::get('orders');
191+
192+
// get the request
193+
Woocommerce::getRequest();
194+
195+
// get the response headers
196+
Woocommerce::getResponse();
197+
198+
// get the total number of results
199+
Woocommerce::getResponse()->getHeaders()['X-WP-Total']
200+
```
201+
202+
155203
### More Examples
156204
Refer to [WooCommerce REST API Documentation](https://woocommerce.github.io/woocommerce-rest-api-docs) for more examples and documention.
157205

src/WoocommerceClient.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,146 @@ public function delete($endpoint, $parameters = [])
7070
{
7171
return $this->client->delete($endpoint, $parameters);
7272
}
73+
74+
/**
75+
* Get the http request header from the last request
76+
*
77+
* @return \Automattic\WooCommerce\HttpClient\Request
78+
*/
79+
public function getRequest()
80+
{
81+
return $this->client->http->getRequest();
82+
}
83+
84+
/**
85+
* Get the http response headers from the last request
86+
*
87+
* @return \Automattic\WooCommerce\HttpClient\Response
88+
*/
89+
public function getResponse()
90+
{
91+
return $this->client->http->getResponse();
92+
}
93+
94+
/**
95+
* Return the first page number of the result
96+
*
97+
* @return int
98+
*/
99+
public function firstPage()
100+
{
101+
return 1;
102+
}
103+
104+
/**
105+
* Return the last page number of the result
106+
*
107+
* @return int
108+
*/
109+
public function lastPage()
110+
{
111+
return $this->totalPages();
112+
}
113+
114+
/**
115+
* Return the current page number of the result
116+
*
117+
* @return int
118+
*/
119+
public function currentPage()
120+
{
121+
return !empty($this->getRequest()->getParameters()['page']) ? $this->getRequest()->getParameters()['page'] : 1;
122+
}
123+
124+
/**
125+
* Return the total number of results
126+
*
127+
* @return int
128+
*/
129+
public function totalResults()
130+
{
131+
return (int)$this->getResponse()->getHeaders()['X-WP-Total'];
132+
}
133+
134+
/**
135+
* Return the total number of pages for this result
136+
*
137+
* @return mixed
138+
*/
139+
public function totalPages()
140+
{
141+
return (int)$this->getResponse()->getHeaders()['X-WP-TotalPages'];
142+
}
143+
144+
/**
145+
* Return the previous page number for the current page
146+
* Will be null if there's no previous page
147+
*
148+
* @return int|null
149+
*/
150+
public function previousPage()
151+
{
152+
$previous_page = $this->currentPage() - 1;
153+
if ($previous_page < 1) {
154+
$previous_page = null;
155+
}
156+
157+
return $previous_page;
158+
}
159+
160+
/**
161+
* Return the next page number for the current page
162+
* Will be null if there's no next page
163+
*
164+
* @return int|null
165+
*/
166+
public function nextPage()
167+
{
168+
$next_page = $this->currentPage() + 1;
169+
if ($next_page > $this->totalPages()) {
170+
$next_page = null;
171+
}
172+
173+
return $next_page;
174+
}
175+
176+
/**
177+
* Returns true if there's a next page
178+
*
179+
* @return bool
180+
*/
181+
public function hasNextPage()
182+
{
183+
return (bool)$this->nextPage();
184+
}
185+
186+
/**
187+
* Returns true if there's a previous page
188+
*
189+
* @return bool
190+
*/
191+
public function hasPreviousPage()
192+
{
193+
return (bool)$this->previousPage();
194+
}
195+
196+
/**
197+
* Returns true if there's no next page
198+
*
199+
* @return bool
200+
*/
201+
public function hasNotNextPage()
202+
{
203+
return (bool)!$this->nextPage();
204+
}
205+
206+
/**
207+
* Returns true if there's no previous page
208+
*
209+
* @return bool
210+
*/
211+
public function hasNotPreviousPage()
212+
{
213+
return (bool)!$this->previousPage();
214+
}
73215
}

0 commit comments

Comments
 (0)