Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for lower case headers in pagination #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ Woocommerce::hasNotPreviousPage(); // false
Woocommerce::hasNotNextPage(); // false
```

In some cases the web server returns headers only in lower case. This may cause errors when
using the pagination functions. If that is your case, call the following method before calling
any pagination methods.

```php
use Woocommerce;

Woocommerce::useLowerCaseHeaders();
```

### HTTP Request & Response (Headers)

```php
Expand All @@ -212,7 +222,6 @@ Woocommerce::getResponse();
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
37 changes: 35 additions & 2 deletions src/WoocommerceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class WoocommerceClient
*/
protected $client;

/**
* @var bool
*/
protected $usingLowerCaseHeaders = false;

/**
*/
public function __construct(Client $client)
Expand Down Expand Up @@ -128,7 +133,7 @@ public function currentPage()
*/
public function totalResults()
{
return (int)$this->getResponse()->getHeaders()['X-WP-Total'];
return (int)$this->getResponse()->getHeaders()[$this->getHeaderWithCase('X-WP-Total')];
}

/**
Expand All @@ -138,7 +143,7 @@ public function totalResults()
*/
public function totalPages()
{
return (int)$this->getResponse()->getHeaders()['X-WP-TotalPages'];
return (int)$this->getResponse()->getHeaders()[$this->getHeaderWithCase('X-WP-TotalPages')];
}

/**
Expand Down Expand Up @@ -212,4 +217,32 @@ public function hasNotPreviousPage()
{
return (bool)!$this->previousPage();
}

/**
* Set if headers should be indexed using lower case
*/
public function useLowerCaseHeaders()
{
$this->usingLowerCaseHeaders = true;
}

/**
* Set if headers should be case sensitive
*/
public function useCaseSensitiveHeaders()
{
$this->usingLowerCaseHeaders = false;
}

/**
* Convert header to correct case
*
* @param $header
*
* @return string
*/
public function getHeaderWithCase($header)
{
return $this->usingLowerCaseHeaders ? strtolower($header) : $header;
}
}
18 changes: 17 additions & 1 deletion tests/WoocommerceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,27 @@ public function pagination_next_page_returns_valid_page_number()
$this->assertFalse($this->woocommerce->hasNotNextPage());
}

/**
* @test
*/
public function headers_are_converted_to_lower_case_when_requested()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method name WoocommerceClientTest::headers_are_converted_to_lower_case_when_requested is not in camel caps format

{
$header = "X-Test-Header";

$this->woocommerce->useCaseSensitiveHeaders();

$this->assertEquals($header, $this->woocommerce->getHeaderWithCase($header));

$this->woocommerce->useLowerCaseHeaders();

$this->assertEquals(strtolower($header), $this->woocommerce->getHeaderWithCase($header));
}

/**
* tear down
*/
public function tearDown()
{
Mockery::close();
}
}
}