| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Framework\Pagination; |
| 4 |
|
| 5 |
use FluentForm\Framework\Pagination\LengthAwarePaginatorInterface; |
| 6 |
|
| 7 |
class UrlWindow |
| 8 |
{ |
| 9 |
/** |
| 10 |
* The paginator implementation. |
| 11 |
* |
| 12 |
* @var \FluentForm\Framework\Pagination\LengthAwarePaginatorInterface |
| 13 |
*/ |
| 14 |
protected $paginator; |
| 15 |
|
| 16 |
/** |
| 17 |
* Create a new URL window instance. |
| 18 |
* |
| 19 |
* @param \FluentForm\Framework\Pagination\LengthAwarePaginatorInterface $paginator |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function __construct(LengthAwarePaginatorInterface $paginator) |
| 23 |
{ |
| 24 |
$this->paginator = $paginator; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Create a new URL window instance. |
| 29 |
* |
| 30 |
* @param \FluentForm\Framework\Pagination\LengthAwarePaginatorInterface $paginator |
| 31 |
* @return array |
| 32 |
*/ |
| 33 |
public static function make(LengthAwarePaginatorInterface $paginator) |
| 34 |
{ |
| 35 |
return (new static($paginator))->get(); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get the window of URLs to be shown. |
| 40 |
* |
| 41 |
* @return array |
| 42 |
*/ |
| 43 |
public function get() |
| 44 |
{ |
| 45 |
// @phpstan-ignore-next-line |
| 46 |
$onEachSide = $this->paginator->onEachSide; |
| 47 |
|
| 48 |
if ($this->paginator->lastPage() < ($onEachSide * 2) + 8) { |
| 49 |
return $this->getSmallSlider(); |
| 50 |
} |
| 51 |
|
| 52 |
return $this->getUrlSlider($onEachSide); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get the slider of URLs there are not enough pages to slide. |
| 57 |
* |
| 58 |
* @return array |
| 59 |
*/ |
| 60 |
protected function getSmallSlider() |
| 61 |
{ |
| 62 |
return [ |
| 63 |
'first' => $this->paginator->getUrlRange(1, $this->lastPage()), |
| 64 |
'slider' => null, |
| 65 |
'last' => null, |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Create a URL slider links. |
| 71 |
* |
| 72 |
* @param int $onEachSide |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
protected function getUrlSlider($onEachSide) |
| 76 |
{ |
| 77 |
$window = $onEachSide + 4; |
| 78 |
|
| 79 |
if (! $this->hasPages()) { |
| 80 |
return ['first' => null, 'slider' => null, 'last' => null]; |
| 81 |
} |
| 82 |
|
| 83 |
// If the current page is very close to the beginning of the page range, |
| 84 |
// we will just render the beginning of the page range, followed |
| 85 |
// by the last 2 of the links in this list, since we |
| 86 |
// will not have room to create a full slider. |
| 87 |
if ($this->currentPage() <= $window) { |
| 88 |
return $this->getSliderTooCloseToBeginning($window, $onEachSide); |
| 89 |
} |
| 90 |
|
| 91 |
// If the current page is close to the ending of the page range, we will |
| 92 |
// just get this first couple pages, followed by a larger window of |
| 93 |
// these ending pages since we're too close to the end of |
| 94 |
// the list to create a full on slider. |
| 95 |
if ($this->currentPage() > ($this->lastPage() - $window)) { |
| 96 |
return $this->getSliderTooCloseToEnding($window, $onEachSide); |
| 97 |
} |
| 98 |
|
| 99 |
// If we have enough room on both sides of the current page to build |
| 100 |
// a slider we will surround it with both the beginning and |
| 101 |
// ending caps, with this window of pages in the middle |
| 102 |
// providing a Google style sliding paginator setup. |
| 103 |
return $this->getFullSlider($onEachSide); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get the slider of URLs when too close to beginning of window. |
| 108 |
* |
| 109 |
* @param int $window |
| 110 |
* @param int $onEachSide |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
protected function getSliderTooCloseToBeginning($window, $onEachSide) |
| 114 |
{ |
| 115 |
return [ |
| 116 |
'first' => $this->paginator->getUrlRange(1, $window + $onEachSide), |
| 117 |
'slider' => null, |
| 118 |
'last' => $this->getFinish(), |
| 119 |
]; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Get the slider of URLs when too close to ending of window. |
| 124 |
* |
| 125 |
* @param int $window |
| 126 |
* @param int $onEachSide |
| 127 |
* @return array |
| 128 |
*/ |
| 129 |
protected function getSliderTooCloseToEnding($window, $onEachSide) |
| 130 |
{ |
| 131 |
$last = $this->paginator->getUrlRange( |
| 132 |
$this->lastPage() - ($window + ($onEachSide - 1)), |
| 133 |
$this->lastPage() |
| 134 |
); |
| 135 |
|
| 136 |
return [ |
| 137 |
'first' => $this->getStart(), |
| 138 |
'slider' => null, |
| 139 |
'last' => $last, |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get the slider of URLs when a full slider can be made. |
| 145 |
* |
| 146 |
* @param int $onEachSide |
| 147 |
* @return array |
| 148 |
*/ |
| 149 |
protected function getFullSlider($onEachSide) |
| 150 |
{ |
| 151 |
return [ |
| 152 |
'first' => $this->getStart(), |
| 153 |
'slider' => $this->getAdjacentUrlRange($onEachSide), |
| 154 |
'last' => $this->getFinish(), |
| 155 |
]; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get the page range for the current page window. |
| 160 |
* |
| 161 |
* @param int $onEachSide |
| 162 |
* @return array |
| 163 |
*/ |
| 164 |
public function getAdjacentUrlRange($onEachSide) |
| 165 |
{ |
| 166 |
return $this->paginator->getUrlRange( |
| 167 |
$this->currentPage() - $onEachSide, |
| 168 |
$this->currentPage() + $onEachSide |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get the starting URLs of a pagination slider. |
| 174 |
* |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
public function getStart() |
| 178 |
{ |
| 179 |
return $this->paginator->getUrlRange(1, 2); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Get the ending URLs of a pagination slider. |
| 184 |
* |
| 185 |
* @return array |
| 186 |
*/ |
| 187 |
public function getFinish() |
| 188 |
{ |
| 189 |
return $this->paginator->getUrlRange( |
| 190 |
$this->lastPage() - 1, |
| 191 |
$this->lastPage() |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Determine if the underlying paginator being presented has pages to show. |
| 197 |
* |
| 198 |
* @return bool |
| 199 |
*/ |
| 200 |
public function hasPages() |
| 201 |
{ |
| 202 |
return $this->paginator->lastPage() > 1; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get the current page from the paginator. |
| 207 |
* |
| 208 |
* @return int |
| 209 |
*/ |
| 210 |
protected function currentPage() |
| 211 |
{ |
| 212 |
return $this->paginator->currentPage(); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Get the last page from the paginator. |
| 217 |
* |
| 218 |
* @return int |
| 219 |
*/ |
| 220 |
protected function lastPage() |
| 221 |
{ |
| 222 |
return $this->paginator->lastPage(); |
| 223 |
} |
| 224 |
} |
| 225 |
|