123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- <?php
-
- namespace Illuminate\Pagination;
-
- use Closure;
- use Illuminate\Support\Str;
- use Illuminate\Support\Collection;
- use Illuminate\Contracts\Support\Htmlable;
-
- /**
- * @mixin \Illuminate\Support\Collection
- */
- abstract class AbstractPaginator implements Htmlable
- {
- /**
- * All of the items being paginated.
- *
- * @var \Illuminate\Support\Collection
- */
- protected $items;
-
- /**
- * The number of items to be shown per page.
- *
- * @var int
- */
- protected $perPage;
-
- /**
- * The current page being "viewed".
- *
- * @var int
- */
- protected $currentPage;
-
- /**
- * The base path to assign to all URLs.
- *
- * @var string
- */
- protected $path = '/';
-
- /**
- * The query parameters to add to all URLs.
- *
- * @var array
- */
- protected $query = [];
-
- /**
- * The URL fragment to add to all URLs.
- *
- * @var string|null
- */
- protected $fragment;
-
- /**
- * The query string variable used to store the page.
- *
- * @var string
- */
- protected $pageName = 'page';
-
- /**
- * The current path resolver callback.
- *
- * @var \Closure
- */
- protected static $currentPathResolver;
-
- /**
- * The current page resolver callback.
- *
- * @var \Closure
- */
- protected static $currentPageResolver;
-
- /**
- * The view factory resolver callback.
- *
- * @var \Closure
- */
- protected static $viewFactoryResolver;
-
- /**
- * The default pagination view.
- *
- * @var string
- */
- public static $defaultView = 'pagination::bootstrap-4';
-
- /**
- * The default "simple" pagination view.
- *
- * @var string
- */
- public static $defaultSimpleView = 'pagination::simple-bootstrap-4';
-
- /**
- * Determine if the given value is a valid page number.
- *
- * @param int $page
- * @return bool
- */
- protected function isValidPageNumber($page)
- {
- return $page >= 1 && filter_var($page, FILTER_VALIDATE_INT) !== false;
- }
-
- /**
- * Get the URL for the previous page.
- *
- * @return string|null
- */
- public function previousPageUrl()
- {
- if ($this->currentPage() > 1) {
- return $this->url($this->currentPage() - 1);
- }
- }
-
- /**
- * Create a range of pagination URLs.
- *
- * @param int $start
- * @param int $end
- * @return array
- */
- public function getUrlRange($start, $end)
- {
- return collect(range($start, $end))->mapWithKeys(function ($page) {
- return [$page => $this->url($page)];
- })->all();
- }
-
- /**
- * Get the URL for a given page number.
- *
- * @param int $page
- * @return string
- */
- public function url($page)
- {
- if ($page <= 0) {
- $page = 1;
- }
-
- // If we have any extra query string key / value pairs that need to be added
- // onto the URL, we will put them in query string form and then attach it
- // to the URL. This allows for extra information like sortings storage.
- $parameters = [$this->pageName => $page];
-
- if (count($this->query) > 0) {
- $parameters = array_merge($this->query, $parameters);
- }
-
- return $this->path
- .(Str::contains($this->path, '?') ? '&' : '?')
- .http_build_query($parameters, '', '&')
- .$this->buildFragment();
- }
-
- /**
- * Get / set the URL fragment to be appended to URLs.
- *
- * @param string|null $fragment
- * @return $this|string|null
- */
- public function fragment($fragment = null)
- {
- if (is_null($fragment)) {
- return $this->fragment;
- }
-
- $this->fragment = $fragment;
-
- return $this;
- }
-
- /**
- * Add a set of query string values to the paginator.
- *
- * @param array|string $key
- * @param string|null $value
- * @return $this
- */
- public function appends($key, $value = null)
- {
- if (is_array($key)) {
- return $this->appendArray($key);
- }
-
- return $this->addQuery($key, $value);
- }
-
- /**
- * Add an array of query string values.
- *
- * @param array $keys
- * @return $this
- */
- protected function appendArray(array $keys)
- {
- foreach ($keys as $key => $value) {
- $this->addQuery($key, $value);
- }
-
- return $this;
- }
-
- /**
- * Add a query string value to the paginator.
- *
- * @param string $key
- * @param string $value
- * @return $this
- */
- protected function addQuery($key, $value)
- {
- if ($key !== $this->pageName) {
- $this->query[$key] = $value;
- }
-
- return $this;
- }
-
- /**
- * Build the full fragment portion of a URL.
- *
- * @return string
- */
- protected function buildFragment()
- {
- return $this->fragment ? '#'.$this->fragment : '';
- }
-
- /**
- * Load a set of relationships onto the mixed relationship collection.
- *
- * @param string $relation
- * @param array $relations
- * @return $this
- */
- public function loadMorph($relation, $relations)
- {
- $this->getCollection()->loadMorph($relation, $relations);
-
- return $this;
- }
-
- /**
- * Get the slice of items being paginated.
- *
- * @return array
- */
- public function items()
- {
- return $this->items->all();
- }
-
- /**
- * Get the number of the first item in the slice.
- *
- * @return int
- */
- public function firstItem()
- {
- return count($this->items) > 0 ? ($this->currentPage - 1) * $this->perPage + 1 : null;
- }
-
- /**
- * Get the number of the last item in the slice.
- *
- * @return int
- */
- public function lastItem()
- {
- return count($this->items) > 0 ? $this->firstItem() + $this->count() - 1 : null;
- }
-
- /**
- * Get the number of items shown per page.
- *
- * @return int
- */
- public function perPage()
- {
- return $this->perPage;
- }
-
- /**
- * Determine if there are enough items to split into multiple pages.
- *
- * @return bool
- */
- public function hasPages()
- {
- return $this->currentPage() != 1 || $this->hasMorePages();
- }
-
- /**
- * Determine if the paginator is on the first page.
- *
- * @return bool
- */
- public function onFirstPage()
- {
- return $this->currentPage() <= 1;
- }
-
- /**
- * Get the current page.
- *
- * @return int
- */
- public function currentPage()
- {
- return $this->currentPage;
- }
-
- /**
- * Get the query string variable used to store the page.
- *
- * @return string
- */
- public function getPageName()
- {
- return $this->pageName;
- }
-
- /**
- * Set the query string variable used to store the page.
- *
- * @param string $name
- * @return $this
- */
- public function setPageName($name)
- {
- $this->pageName = $name;
-
- return $this;
- }
-
- /**
- * Set the base path to assign to all URLs.
- *
- * @param string $path
- * @return $this
- */
- public function withPath($path)
- {
- return $this->setPath($path);
- }
-
- /**
- * Set the base path to assign to all URLs.
- *
- * @param string $path
- * @return $this
- */
- public function setPath($path)
- {
- $this->path = $path;
-
- return $this;
- }
-
- /**
- * Resolve the current request path or return the default value.
- *
- * @param string $default
- * @return string
- */
- public static function resolveCurrentPath($default = '/')
- {
- if (isset(static::$currentPathResolver)) {
- return call_user_func(static::$currentPathResolver);
- }
-
- return $default;
- }
-
- /**
- * Set the current request path resolver callback.
- *
- * @param \Closure $resolver
- * @return void
- */
- public static function currentPathResolver(Closure $resolver)
- {
- static::$currentPathResolver = $resolver;
- }
-
- /**
- * Resolve the current page or return the default value.
- *
- * @param string $pageName
- * @param int $default
- * @return int
- */
- public static function resolveCurrentPage($pageName = 'page', $default = 1)
- {
- if (isset(static::$currentPageResolver)) {
- return call_user_func(static::$currentPageResolver, $pageName);
- }
-
- return $default;
- }
-
- /**
- * Set the current page resolver callback.
- *
- * @param \Closure $resolver
- * @return void
- */
- public static function currentPageResolver(Closure $resolver)
- {
- static::$currentPageResolver = $resolver;
- }
-
- /**
- * Get an instance of the view factory from the resolver.
- *
- * @return \Illuminate\Contracts\View\Factory
- */
- public static function viewFactory()
- {
- return call_user_func(static::$viewFactoryResolver);
- }
-
- /**
- * Set the view factory resolver callback.
- *
- * @param \Closure $resolver
- * @return void
- */
- public static function viewFactoryResolver(Closure $resolver)
- {
- static::$viewFactoryResolver = $resolver;
- }
-
- /**
- * Set the default pagination view.
- *
- * @param string $view
- * @return void
- */
- public static function defaultView($view)
- {
- static::$defaultView = $view;
- }
-
- /**
- * Set the default "simple" pagination view.
- *
- * @param string $view
- * @return void
- */
- public static function defaultSimpleView($view)
- {
- static::$defaultSimpleView = $view;
- }
-
- /**
- * Indicate that Bootstrap 3 styling should be used for generated links.
- *
- * @return void
- */
- public static function useBootstrapThree()
- {
- static::defaultView('pagination::default');
- static::defaultSimpleView('pagination::simple-default');
- }
-
- /**
- * Get an iterator for the items.
- *
- * @return \ArrayIterator
- */
- public function getIterator()
- {
- return $this->items->getIterator();
- }
-
- /**
- * Determine if the list of items is empty.
- *
- * @return bool
- */
- public function isEmpty()
- {
- return $this->items->isEmpty();
- }
-
- /**
- * Determine if the list of items is not empty.
- *
- * @return bool
- */
- public function isNotEmpty()
- {
- return $this->items->isNotEmpty();
- }
-
- /**
- * Get the number of items for the current page.
- *
- * @return int
- */
- public function count()
- {
- return $this->items->count();
- }
-
- /**
- * Get the paginator's underlying collection.
- *
- * @return \Illuminate\Support\Collection
- */
- public function getCollection()
- {
- return $this->items;
- }
-
- /**
- * Set the paginator's underlying collection.
- *
- * @param \Illuminate\Support\Collection $collection
- * @return $this
- */
- public function setCollection(Collection $collection)
- {
- $this->items = $collection;
-
- return $this;
- }
-
- /**
- * Determine if the given item exists.
- *
- * @param mixed $key
- * @return bool
- */
- public function offsetExists($key)
- {
- return $this->items->has($key);
- }
-
- /**
- * Get the item at the given offset.
- *
- * @param mixed $key
- * @return mixed
- */
- public function offsetGet($key)
- {
- return $this->items->get($key);
- }
-
- /**
- * Set the item at the given offset.
- *
- * @param mixed $key
- * @param mixed $value
- * @return void
- */
- public function offsetSet($key, $value)
- {
- $this->items->put($key, $value);
- }
-
- /**
- * Unset the item at the given key.
- *
- * @param mixed $key
- * @return void
- */
- public function offsetUnset($key)
- {
- $this->items->forget($key);
- }
-
- /**
- * Render the contents of the paginator to HTML.
- *
- * @return string
- */
- public function toHtml()
- {
- return (string) $this->render();
- }
-
- /**
- * Make dynamic calls into the collection.
- *
- * @param string $method
- * @param array $parameters
- * @return mixed
- */
- public function __call($method, $parameters)
- {
- return $this->getCollection()->$method(...$parameters);
- }
-
- /**
- * Render the contents of the paginator when casting to string.
- *
- * @return string
- */
- public function __toString()
- {
- return (string) $this->render();
- }
- }
|