Skip to content

Commit 3faa93c

Browse files
Add support for lower case headers in pagination (#57)
* Add support for lower case headers in pagination In some cases the webserver only return lower case headers which leads to an error being thrown when the pagination tries to index the array with headers. This adds methods to fix this issue. * Moved opening brace to new line * Fix styling --------- Co-authored-by: pixelpeter <[email protected]> Co-authored-by: pixelpeter <[email protected]>
1 parent 60ba826 commit 3faa93c

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ Woocommerce::hasNotPreviousPage(); // false
194194
Woocommerce::hasNotNextPage(); // false
195195
```
196196

197+
In some cases the web server returns headers only in lower case. This may cause errors when
198+
using the pagination functions. If that is your case, call the following method before calling
199+
any pagination methods.
200+
201+
```php
202+
use Woocommerce;
203+
204+
Woocommerce::useLowerCaseHeaders();
205+
```
206+
197207
### HTTP Request & Response (Headers)
198208

199209
```php
@@ -212,7 +222,6 @@ Woocommerce::getResponse();
212222
Woocommerce::getResponse()->getHeaders()['X-WP-Total']
213223
```
214224

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

src/WoocommerceClient.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class WoocommerceClient
1414
*/
1515
protected $client;
1616

17+
/**
18+
* @var bool
19+
*/
20+
protected $usingLowerCaseHeaders = false;
21+
1722
public function __construct(Client $client)
1823
{
1924
$this->client = $client;
@@ -124,7 +129,7 @@ public function currentPage()
124129
*/
125130
public function totalResults()
126131
{
127-
return (int) $this->getResponse()->getHeaders()['X-WP-Total'];
132+
return (int) $this->getResponse()->getHeaders()[$this->getHeaderWithCase('X-WP-Total')];
128133
}
129134

130135
/**
@@ -134,7 +139,7 @@ public function totalResults()
134139
*/
135140
public function totalPages()
136141
{
137-
return (int) $this->getResponse()->getHeaders()['X-WP-TotalPages'];
142+
return (int) $this->getResponse()->getHeaders()[$this->getHeaderWithCase('X-WP-TotalPages')];
138143
}
139144

140145
/**
@@ -208,4 +213,31 @@ public function hasNotPreviousPage()
208213
{
209214
return (bool) ! $this->previousPage();
210215
}
216+
217+
/**
218+
* Set if headers should be indexed using lower case
219+
*/
220+
public function useLowerCaseHeaders()
221+
{
222+
$this->usingLowerCaseHeaders = true;
223+
}
224+
225+
/**
226+
* Set if headers should be case sensitive
227+
*/
228+
public function useCaseSensitiveHeaders()
229+
{
230+
$this->usingLowerCaseHeaders = false;
231+
}
232+
233+
/**
234+
* Convert header to correct case
235+
*
236+
*
237+
* @return string
238+
*/
239+
public function getHeaderWithCase($header)
240+
{
241+
return $this->usingLowerCaseHeaders ? strtolower($header) : $header;
242+
}
211243
}

tests/WoocommerceClient.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,22 @@ public function pagination_next_page_returns_valid_page_number()
274274
$this->assertFalse($this->woocommerce->hasNotNextPage());
275275
}
276276

277+
/**
278+
* @test
279+
*/
280+
public function headers_are_converted_to_lower_case_when_requested()
281+
{
282+
$header = 'X-Test-Header';
283+
284+
$this->woocommerce->useCaseSensitiveHeaders();
285+
286+
$this->assertEquals($header, $this->woocommerce->getHeaderWithCase($header));
287+
288+
$this->woocommerce->useLowerCaseHeaders();
289+
290+
$this->assertEquals(strtolower($header), $this->woocommerce->getHeaderWithCase($header));
291+
}
292+
277293
/**
278294
* tear down
279295
*/

0 commit comments

Comments
 (0)