Skip to content

Commit

Permalink
Add pagination functionality (#15)
Browse files Browse the repository at this point in the history
* Add methods to get http request/response headers fro the last api call

* Add pagination functionality and tests

* Update README and CHANGELOG
  • Loading branch information
pixelpeter committed Jan 29, 2017
1 parent 3f23f80 commit 56652e2
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 0 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

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

## 2.3.0
- Add pagination functionality.
Now these functions are available
- totalResults()
- firstPage()
- lastPage()
- currentPage()
- totalPages()
- previousPage()
- nextPage()
- hasPreviousPage()
- hasNextPage()
- hasNotPreviousPage()
- hasNotNextPage()
- Get the Request & Response (Headers) from the last request
- getRequest()
- getResponse()

## 2.2.0
- Laravel 5.4 compatibility

Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,54 @@ $data = [
return Woocommerce::put('products/1', $data);
```

### Pagination
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.
(WC 2.6.x or later, WP 4.4 or later)

```php
use Woocommerce;

// assuming we have 474 orders in pur result
// we will request page 5 with 25 results per page
$params = [
'per_page' => 25,
'page' => 5
];

Woocommerce::get('orders', $params);

Woocommerce::totalResults(); // 474
Woocommerce::firstPage(); // 1
Woocommerce::lastPage(); // 19
Woocommerce::currentPage(); // 5
Woocommerce::totalPages(); // 19
Woocommerce::previousPage(); // 4
Woocommerce::nextPage(); // 6
Woocommerce::hasPreviousPage(); // true
Woocommerce::hasNextPage(); // true
Woocommerce::hasNotPreviousPage(); // false
Woocommerce::hasNotNextPage(); // false
```

### HTTP Request & Response (Headers)

```php
use Woocommerce;

// first send a request
Woocommerce::get('orders');

// get the request
Woocommerce::getRequest();

// get the response headers
Woocommerce::getResponse();

// get the total number of results
Woocommerce::getResponse()->getHeaders()['X-WP-Total']
```


### More Examples
Refer to [WooCommerce REST API Documentation](https://woocommerce.github.io/woocommerce-rest-api-docs) for more examples and documention.

Expand Down
142 changes: 142 additions & 0 deletions src/WoocommerceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,146 @@ public function delete($endpoint, $parameters = [])
{
return $this->client->delete($endpoint, $parameters);
}

/**
* Get the http request header from the last request
*
* @return \Automattic\WooCommerce\HttpClient\Request
*/
public function getRequest()
{
return $this->client->http->getRequest();
}

/**
* Get the http response headers from the last request
*
* @return \Automattic\WooCommerce\HttpClient\Response
*/
public function getResponse()
{
return $this->client->http->getResponse();
}

/**
* Return the first page number of the result
*
* @return int
*/
public function firstPage()
{
return 1;
}

/**
* Return the last page number of the result
*
* @return int
*/
public function lastPage()
{
return $this->totalPages();
}

/**
* Return the current page number of the result
*
* @return int
*/
public function currentPage()
{
return !empty($this->getRequest()->getParameters()['page']) ? $this->getRequest()->getParameters()['page'] : 1;
}

/**
* Return the total number of results
*
* @return int
*/
public function totalResults()
{
return (int)$this->getResponse()->getHeaders()['X-WP-Total'];
}

/**
* Return the total number of pages for this result
*
* @return mixed
*/
public function totalPages()
{
return (int)$this->getResponse()->getHeaders()['X-WP-TotalPages'];
}

/**
* Return the previous page number for the current page
* Will be null if there's no previous page
*
* @return int|null
*/
public function previousPage()
{
$previous_page = $this->currentPage() - 1;
if ($previous_page < 1) {
$previous_page = null;
}

return $previous_page;
}

/**
* Return the next page number for the current page
* Will be null if there's no next page
*
* @return int|null
*/
public function nextPage()
{
$next_page = $this->currentPage() + 1;
if ($next_page > $this->totalPages()) {
$next_page = null;
}

return $next_page;
}

/**
* Returns true if there's a next page
*
* @return bool
*/
public function hasNextPage()
{
return (bool)$this->nextPage();
}

/**
* Returns true if there's a previous page
*
* @return bool
*/
public function hasPreviousPage()
{
return (bool)$this->previousPage();
}

/**
* Returns true if there's no next page
*
* @return bool
*/
public function hasNotNextPage()
{
return (bool)!$this->nextPage();
}

/**
* Returns true if there's no previous page
*
* @return bool
*/
public function hasNotPreviousPage()
{
return (bool)!$this->previousPage();
}
}
Loading

0 comments on commit 56652e2

Please sign in to comment.