diff --git a/README.md b/README.md index 1a5ad27..b70e200 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/src/WoocommerceClient.php b/src/WoocommerceClient.php index 1bfd025..fdd708c 100644 --- a/src/WoocommerceClient.php +++ b/src/WoocommerceClient.php @@ -12,6 +12,11 @@ class WoocommerceClient */ protected $client; + /** + * @var bool + */ + protected $usingLowerCaseHeaders = false; + /** */ public function __construct(Client $client) @@ -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')]; } /** @@ -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')]; } /** @@ -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; + } } diff --git a/tests/WoocommerceClient.php b/tests/WoocommerceClient.php index 8e8b0c3..b7d9aea 100644 --- a/tests/WoocommerceClient.php +++ b/tests/WoocommerceClient.php @@ -274,6 +274,22 @@ 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() + { + $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 */ @@ -281,4 +297,4 @@ public function tearDown() { Mockery::close(); } -} \ No newline at end of file +}