]> git.mxchange.org Git - friendica.git/blobdiff - src/Content/Pager.php
Merge pull request #13133 from annando/start-header
[friendica.git] / src / Content / Pager.php
index 5b4345a4c8bc4a2140884e50b063c8893eb40c30..74600a71ce95e677ac60871ee50ac08024d04901 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -30,10 +30,13 @@ use Friendica\Util\Strings;
  */
 class Pager
 {
+       /** @var int Default count of items per page */
+       const ITEMS_PER_PAGE = 50;
+
        /** @var integer */
        private $page = 1;
        /** @var integer */
-       protected $itemsPerPage = 50;
+       protected $itemsPerPage = self::ITEMS_PER_PAGE;
        /** @var string */
        protected $baseQueryString = '';
 
@@ -45,25 +48,25 @@ class Pager
         *
         * Guesses the page number from the GET parameter 'page'.
         *
-        * @param L10n    $l10n
-        * @param string  $queryString  The query string of the current page
-        * @param integer $itemsPerPage An optional number of items per page to override the default value
+        * @param L10n   $l10n
+        * @param string $queryString  The query string of the current page
+        * @param int    $itemsPerPage An optional number of items per page to override the default value
         */
-       public function __construct(L10n $l10n, $queryString, $itemsPerPage = 50)
+       public function __construct(L10n $l10n, string $queryString, int $itemsPerPage = 50)
        {
                $this->l10n = $l10n;
 
                $this->setQueryString($queryString);
                $this->setItemsPerPage($itemsPerPage);
-               $this->setPage(($_GET['page'] ?? 0) ?: 1);
+               $this->setPage((int)($_GET['page'] ?? 0) ?: 1);
        }
 
        /**
         * Returns the start offset for a LIMIT clause. Starts at 0.
         *
-        * @return integer
+        * @return int
         */
-       public function getStart()
+       public function getStart(): int
        {
                return max(0, ($this->page * $this->itemsPerPage) - $this->itemsPerPage);
        }
@@ -71,9 +74,9 @@ class Pager
        /**
         * Returns the number of items per page
         *
-        * @return integer
+        * @return int
         */
-       public function getItemsPerPage()
+       public function getItemsPerPage(): int
        {
                return $this->itemsPerPage;
        }
@@ -83,7 +86,7 @@ class Pager
         *
         * @return int
         */
-       public function getPage()
+       public function getPage(): int
        {
                return $this->page;
        }
@@ -105,9 +108,9 @@ class Pager
        /**
         * Sets the number of items per page, 1 minimum.
         *
-        * @param integer $itemsPerPage
+        * @param int $itemsPerPage
         */
-       public function setItemsPerPage($itemsPerPage)
+       public function setItemsPerPage(int $itemsPerPage)
        {
                $this->itemsPerPage = max(1, intval($itemsPerPage));
        }
@@ -115,21 +118,21 @@ class Pager
        /**
         * Sets the current page number. Starts at 1.
         *
-        * @param integer $page
+        * @param int $page
         */
-       public function setPage($page)
+       public function setPage(int $page)
        {
-               $this->page = max(1, intval($page));
+               $this->page = max(1, $page);
        }
 
        /**
         * Sets the base query string from a full query string.
         *
-        * Strips the 'page' parameter, and remove the 'q=' string for some reason.
+        * Strips the 'page' parameter
         *
         * @param string $queryString
         */
-       public function setQueryString($queryString)
+       public function setQueryString(string $queryString)
        {
                $stripped = preg_replace('/([&?]page=[0-9]*)/', '', $queryString);
 
@@ -157,7 +160,7 @@ class Pager
         * @return string HTML string of the pager
         * @throws \Exception
         */
-       public function renderMinimal(int $itemCount)
+       public function renderMinimal(int $itemCount): string
        {
                $displayedItemCount = max(0, intval($itemCount));
 
@@ -196,13 +199,13 @@ class Pager
         *
         * $html = $pager->renderFull();
         *
-        * @param integer $itemCount The total number of items including those note displayed on the page
+        * @param int $itemCount The total number of items including those note displayed on the page
         * @return string HTML string of the pager
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function renderFull($itemCount)
+       public function renderFull(int $itemCount): string
        {
-               $totalItemCount = max(0, intval($itemCount));
+               $totalItemCount = max(0, $itemCount);
 
                $data = [];