Skip to content

Commit 76cbc1e

Browse files
committed
Fix advanced pagination querystring
Fixes #131
1 parent a9f4ea8 commit 76cbc1e

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.61] - 31 May 2018
8+
### Fixed
9+
- caching of paginated queries with page identifiers as arrays (`?page[size]=1`).
10+
11+
## [0.2.60] - 27 May 2018
12+
### Added
13+
- unit tests for multiple versions of Laravel simultaneously.
14+
- backwards-compatibility to Laravel 5.4.
15+
716
## [0.2.59] - 27 May 2018
817
### Fixed
918
- caching of queries with `whereNotIn` clauses.

src/CachedBuilder.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,34 @@ public function paginate(
127127
}
128128

129129
$page = request("page", $page ?: 1);
130+
131+
if (is_array($page)) {
132+
$page = $this->recursiveImplodeWithKey($page);
133+
}
134+
dump($perPage, $columns, $pageName, $page);
135+
130136
$cacheKey = $this->makeCacheKey($columns, null, "-paginate_by_{$perPage}_{$pageName}_{$page}");
131137

132138
return $this->cachedValue(func_get_args(), $cacheKey);
133139
}
134140

141+
protected function recursiveImplodeWithKey(array $items, string $glue = "_") : string
142+
{
143+
$result = "";
144+
145+
foreach ($items as $key => $value) {
146+
if (is_array($value)) {
147+
$result .= $key . $glue . $this->recursiveImplode($value, $glue);
148+
149+
continue;
150+
}
151+
152+
$result .= $glue . $key . $glue . $value;
153+
}
154+
155+
return $result;
156+
}
157+
135158
public function pluck($column, $key = null)
136159
{
137160
if (! $this->isCachable()) {

tests/Feature/PaginationTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,26 @@ public function testPaginationProvidesDifferentLinksOnDifferentPages()
3333
$page2->see($page2ActiveLink);
3434
$page2->see($book->title);
3535
}
36+
37+
public function testAdvancedPagination()
38+
{
39+
if (starts_with(app()->version(), "5.6")) {
40+
$page1ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">1</span></li>';
41+
$page2ActiveLink = '<li class="page-item active" aria-current="page"><span class="page-link">2</span></li>';
42+
}
43+
44+
if (starts_with(app()->version(), "5.5")) {
45+
$page1ActiveLink = '<li class="active"><span>1</span></li>';
46+
$page2ActiveLink = '<li class="active"><span>2</span></li>';
47+
}
48+
49+
if (starts_with(app()->version(), "5.4")) {
50+
$page1ActiveLink = '<li class="active"><span>1</span></li>';
51+
$page2ActiveLink = '<li class="active"><span>2</span></li>';
52+
}
53+
54+
$response = $this->visit("pagination-test?page[size]=1");
55+
56+
$response->see($page1ActiveLink);
57+
}
3658
}

0 commit comments

Comments
 (0)