Skip to content

Commit

Permalink
fix behavior of oneachside = 1 with paginator
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 25, 2020
1 parent 7aeacdd commit c59cffa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/Illuminate/Pagination/UrlWindow.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function getSmallSlider()
*/
protected function getUrlSlider($onEachSide)
{
$window = $onEachSide * 2;
$window = $onEachSide + 4;

if (! $this->hasPages()) {
return ['first' => null, 'slider' => null, 'last' => null];
Expand All @@ -83,14 +83,14 @@ protected function getUrlSlider($onEachSide)
// just render the beginning of the page range, followed by the last 2 of the
// links in this list, since we will not have room to create a full slider.
if ($this->currentPage() <= $window) {
return $this->getSliderTooCloseToBeginning($window);
return $this->getSliderTooCloseToBeginning($window, $onEachSide);
}

// If the current page is close to the ending of the page range we will just get
// this first couple pages, followed by a larger window of these ending pages
// since we're too close to the end of the list to create a full on slider.
elseif ($this->currentPage() > ($this->lastPage() - $window)) {
return $this->getSliderTooCloseToEnding($window);
return $this->getSliderTooCloseToEnding($window, $onEachSide);
}

// If we have enough room on both sides of the current page to build a slider we
Expand All @@ -103,12 +103,13 @@ protected function getUrlSlider($onEachSide)
* Get the slider of URLs when too close to beginning of window.
*
* @param int $window
* @param int $onEachSide
* @return array
*/
protected function getSliderTooCloseToBeginning($window)
protected function getSliderTooCloseToBeginning($window, $onEachSide)
{
return [
'first' => $this->paginator->getUrlRange(1, $window + 2),
'first' => $this->paginator->getUrlRange(1, $window + $onEachSide),
'slider' => null,
'last' => $this->getFinish(),
];
Expand All @@ -118,12 +119,13 @@ protected function getSliderTooCloseToBeginning($window)
* Get the slider of URLs when too close to ending of window.
*
* @param int $window
* @param int $onEachSide
* @return array
*/
protected function getSliderTooCloseToEnding($window)
protected function getSliderTooCloseToEnding($window, $onEachSide)
{
$last = $this->paginator->getUrlRange(
$this->lastPage() - ($window + 2),
$this->lastPage() - ($window + ($onEachSide - 1)),
$this->lastPage()
);

Expand Down
17 changes: 10 additions & 7 deletions tests/Pagination/UrlWindowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,31 @@ public function testPresenterCanGetAUrlRangeForASmallNumberOfUrls()
public function testPresenterCanGetAUrlRangeForAWindowOfLinks()
{
$array = [];
for ($i = 1; $i <= 13; $i++) {
for ($i = 1; $i <= 20; $i++) {
$array[$i] = 'item'.$i;
}
$p = new LengthAwarePaginator($array, count($array), 1, 7);
$p = new LengthAwarePaginator($array, count($array), 1, 12);
$window = new UrlWindow($p);
$slider = [];
for ($i = 4; $i <= 10; $i++) {
for ($i = 9; $i <= 15; $i++) {
$slider[$i] = '/?page='.$i;
}

$this->assertEquals(['first' => [1 => '/?page=1', 2 => '/?page=2'], 'slider' => $slider, 'last' => [12 => '/?page=12', 13 => '/?page=13']], $window->get());
$this->assertEquals(['first' => [1 => '/?page=1', 2 => '/?page=2'], 'slider' => $slider, 'last' => [19 => '/?page=19', 20 => '/?page=20']], $window->get());

/*
* Test Being Near The End Of The List
*/
$p = new LengthAwarePaginator($array, count($array), 1, 8);
$array = [];
for ($i = 1; $i <= 13; $i++) {
$array[$i] = 'item'.$i;
}
$p = new LengthAwarePaginator($array, count($array), 1, 10);
$window = new UrlWindow($p);
$last = [];
for ($i = 5; $i <= 13; $i++) {
for ($i = 4; $i <= 13; $i++) {
$last[$i] = '/?page='.$i;
}

$this->assertEquals(['first' => [1 => '/?page=1', 2 => '/?page=2'], 'slider' => null, 'last' => $last], $window->get());
}

Expand Down

0 comments on commit c59cffa

Please sign in to comment.