From 78a11b221e2634a3062d1f699488a847d8469ea7 Mon Sep 17 00:00:00 2001 From: Nick Sagona Date: Mon, 16 Oct 2023 21:28:30 -0500 Subject: [PATCH] Update for PHP 8.1 --- .github/workflows/phpunit.yml | 5 +- .gitignore | 5 +- LICENSE.TXT | 2 +- README.md | 7 +++ composer.json | 6 +- phpunit.xml | 12 ++-- src/AbstractPaginator.php | 70 +++++++++++----------- src/Exception.php | 6 +- src/Form.php | 40 ++++++------- src/Paginator.php | 10 ++-- src/PaginatorInterface.php | 110 ++++++++++++++++++++++++++++++++++ src/Range.php | 68 ++++++++++----------- 12 files changed, 233 insertions(+), 108 deletions(-) create mode 100644 src/PaginatorInterface.php diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 131ac45..46eabbb 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -6,13 +6,16 @@ on: pull_request: branches: [ master ] +env: + XDEBUG_MODE: debug,coverage + jobs: build: runs-on: ubuntu-latest strategy: matrix: - php-versions: [ '7.4', '8.0' ] + php-versions: [ '8.1', '8.2' ] steps: - uses: actions/checkout@v3 diff --git a/.gitignore b/.gitignore index 4a39b16..b4b31a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ composer.lock public +script vendor - +kettle +.phpunit.result.cache +.phpunit.cache diff --git a/LICENSE.TXT b/LICENSE.TXT index 686bb74..d8b6beb 100644 --- a/LICENSE.TXT +++ b/LICENSE.TXT @@ -1,6 +1,6 @@ BSD 3 Clause License -Copyright (c) 2009-2023, NOLA Interactive, LLC. +Copyright (c) 2009-2024, NOLA Interactive, LLC. All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/README.md b/README.md index 3282da8..d2522b3 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,13 @@ Install `pop-paginator` using Composer. composer require popphp/pop-paginator +Or, require it in your composer.json file + + "require": { + "popphp/pop-paginator" : "^4.0.0" + } + + BASIC USAGE ----------- diff --git a/composer.json b/composer.json index 4023c97..322803a 100644 --- a/composer.json +++ b/composer.json @@ -18,10 +18,10 @@ } ], "require": { - "php": ">=7.4.0" + "php": ">=8.1.0" }, "require-dev": { - "phpunit/phpunit": "^9.0.0" + "phpunit/phpunit": "^10.0.0" }, "autoload": { "psr-4": { @@ -38,7 +38,7 @@ }, "extra": { "branch-alias": { - "dev-master": "3.1.x-dev" + "dev-master": "4.0.x-dev" } } } diff --git a/phpunit.xml b/phpunit.xml index 437ae3a..6999a39 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,9 +1,6 @@ - - - - src - + + @@ -14,4 +11,9 @@ + + + src + + diff --git a/src/AbstractPaginator.php b/src/AbstractPaginator.php index 091ac4a..7737918 100644 --- a/src/AbstractPaginator.php +++ b/src/AbstractPaginator.php @@ -4,7 +4,7 @@ * * @link https://github.com/popphp/popphp-framework * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License */ @@ -19,66 +19,66 @@ * @category Pop * @package Pop\Paginator * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License - * @version 3.1.0 + * @version 4.0.0 */ -abstract class AbstractPaginator +abstract class AbstractPaginator implements PaginatorInterface { /** * Total number of items * @var int */ - protected $total = 0; + protected int $total = 0; /** * Number of items per page * @var int */ - protected $perPage = 10; + protected int $perPage = 10; /** * Range of pages per page * @var int */ - protected $range = 10; + protected int $range = 10; /** * Query key * @var string */ - protected $queryKey = 'page'; + protected string $queryKey = 'page'; /** * Current page property * @var int */ - protected $currentPage = 1; + protected int $currentPage = 1; /** * Number of pages property - * @var int + * @var ?int */ - protected $numberOfPages = null; + protected ?int $numberOfPages = null; /** * Current page start index property - * @var int + * @var ?int */ - protected $start = null; + protected ?int $start = null; /** * Current page end index property - * @var int + * @var ?int */ - protected $end = null; + protected ?int $end = null; /** * Page bookends * @var array */ - protected $bookends = [ + protected array $bookends = [ 'start' => '«', 'previous' => '‹', 'next' => '›', @@ -94,11 +94,11 @@ abstract class AbstractPaginator * @param int $perPage * @param int $range */ - public function __construct($total, $perPage = 10, $range = 10) + public function __construct(int $total, int $perPage = 10, int $range = 10) { - $this->total = (int)$total; - $this->perPage = (int)$perPage; - $this->range = (int)$range; + $this->total = $total; + $this->perPage = $perPage; + $this->range = $range; } /** @@ -107,7 +107,7 @@ public function __construct($total, $perPage = 10, $range = 10) * @param string $key * @return AbstractPaginator */ - public function setQueryKey($key) + public function setQueryKey(string $key): AbstractPaginator { $this->queryKey = $key; return $this; @@ -119,7 +119,7 @@ public function setQueryKey($key) * @param array $bookends * @return AbstractPaginator */ - public function setBookends(array $bookends) + public function setBookends(array $bookends): AbstractPaginator { if (array_key_exists('start', $bookends)) { $this->bookends['start'] = $bookends['start']; @@ -142,7 +142,7 @@ public function setBookends(array $bookends) * * @return int */ - public function getTotal() + public function getTotal(): int { return $this->total; } @@ -152,7 +152,7 @@ public function getTotal() * * @return int */ - public function getPerPage() + public function getPerPage(): int { return $this->perPage; } @@ -162,7 +162,7 @@ public function getPerPage() * * @return int */ - public function getRange() + public function getRange(): int { return $this->range; } @@ -170,9 +170,9 @@ public function getRange() /** * Get the query key * - * @return int + * @return string */ - public function getQueryKey() + public function getQueryKey(): string { return $this->queryKey; } @@ -182,7 +182,7 @@ public function getQueryKey() * * @return int */ - public function getCurrentPage() + public function getCurrentPage(): int { return $this->currentPage; } @@ -192,7 +192,7 @@ public function getCurrentPage() * * @return int */ - public function getNumberOfPages() + public function getNumberOfPages(): int { return $this->numberOfPages; } @@ -201,11 +201,11 @@ public function getNumberOfPages() * Get a bookend * * @param string $key - * @return string + * @return string|null */ - public function getBookend($key) + public function getBookend(string $key): string|null { - return (isset($this->bookends[$key])) ? $this->bookends[$key] : null; + return $this->bookends[$key] ?? null; } /** @@ -213,7 +213,7 @@ public function getBookend($key) * * @return array */ - public function getBookends() + public function getBookends(): array { return $this->bookends; } @@ -224,7 +224,7 @@ public function getBookends() * @param int $page * @return array */ - public function calculateRange($page = 1) + public function calculateRange(int $page = 1): array { $this->currentPage = $page; @@ -252,7 +252,7 @@ public function calculateRange($page = 1) } // Check and calculate for any page ranges. - if (((null === $this->range) || ($this->range > $this->numberOfPages)) && (null === $this->total)) { + if ((($this->range === null) || ($this->range > $this->numberOfPages)) && ($this->total === null)) { $range = [ 'start' => 1, 'end' => $this->numberOfPages, diff --git a/src/Exception.php b/src/Exception.php index 1c57a57..9bdad1d 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -4,7 +4,7 @@ * * @link https://github.com/popphp/popphp-framework * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License */ @@ -19,8 +19,8 @@ * @category Pop * @package Pop\Paginator * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License - * @version 3.1.0 + * @version 4.0.0 */ class Exception extends \Exception {} diff --git a/src/Form.php b/src/Form.php index 307c10b..d6c9a00 100644 --- a/src/Form.php +++ b/src/Form.php @@ -4,7 +4,7 @@ * * @link https://github.com/popphp/popphp-framework * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License */ @@ -19,9 +19,9 @@ * @category Pop * @package Pop\Paginator * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License - * @version 3.1.0 + * @version 4.0.0 */ class Form extends AbstractPaginator { @@ -30,13 +30,13 @@ class Form extends AbstractPaginator * Input separator * @var string */ - protected $inputSeparator = 'of'; + protected string $inputSeparator = 'of'; /** * Page form property - * @var string + * @var ?string */ - protected $form = null; + protected ?string $form = null; /** * Constructor @@ -46,7 +46,7 @@ class Form extends AbstractPaginator * @param int $total * @param int $perPage */ - public function __construct($total, $perPage = 10) + public function __construct(int $total, int $perPage = 10) { parent::__construct($total, $perPage, 1); } @@ -57,7 +57,7 @@ public function __construct($total, $perPage = 10) * @param string $separator * @return Form */ - public function setInputSeparator($separator) + public function setInputSeparator(string $separator): Form { $this->inputSeparator = $separator; return $this; @@ -68,7 +68,7 @@ public function setInputSeparator($separator) * * @return string */ - public function getInputSeparator() + public function getInputSeparator(): string { return $this->inputSeparator; } @@ -76,12 +76,12 @@ public function getInputSeparator() /** * Get the page form string * - * @param int $page + * @param ?int $page * @return string */ - public function getFormString($page = null) + public function getFormString(?int $page = null): string { - if (null === $page) { + if ($page === null) { $page = (isset($_GET[$this->queryKey]) && ((int)$_GET[$this->queryKey] > 0)) ? (int)$_GET[$this->queryKey] : 1; } $this->calculateRange($page); @@ -96,7 +96,7 @@ public function getFormString($page = null) * @param int $page * @return void */ - protected function createForm($page = 1) + protected function createForm(int $page = 1): void { $this->currentPage = $page; @@ -134,11 +134,11 @@ protected function createForm($page = 1) // Calculate page range $pageRange = $this->calculateRange($page); - $form .= '
' . $this->inputSeparator . ' ' . $this->numberOfPages . '
'; - if (null !== $hidden) { + if ($hidden !== null) { $form .= '
' . $hidden . '
'; } @@ -149,22 +149,22 @@ protected function createForm($page = 1) for ($i = $pageRange['start']; $i <= $pageRange['end']; $i++) { if (($i == $pageRange['start']) && ($pageRange['prev'])) { - if (null !== $this->bookends['start']) { + if ($this->bookends['start'] !== null) { $startLinks .= "queryKey . "=1" . "{$query}\">" . $this->bookends['start'] . ""; } - if (null !== $this->bookends['previous']) { + if ($this->bookends['previous'] !== null) { $startLinks .= "queryKey . "=" . ($i - 1) . "{$query}\">" . $this->bookends['previous'] . ""; } } if (($i == $pageRange['end']) && ($pageRange['next'])) { - if (null !== $this->bookends['next']) { + if ($this->bookends['next'] !== null) { $endLinks .= "queryKey . "=" . ($i + 1) . "{$query}\">" . $this->bookends['next'] . ""; } - if (null !== $this->bookends['end']) { + if ($this->bookends['end'] !== null) { $endLinks .= "queryKey . "=" . $this->numberOfPages . "{$query}\">" . $this->bookends['end'] . ""; } @@ -179,7 +179,7 @@ protected function createForm($page = 1) * * @return string */ - public function __toString() + public function __toString(): string { if (empty($this->form)) { $this->getFormString(); diff --git a/src/Paginator.php b/src/Paginator.php index ad1baa0..7bb1a6f 100644 --- a/src/Paginator.php +++ b/src/Paginator.php @@ -4,7 +4,7 @@ * * @link https://github.com/popphp/popphp-framework * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License */ @@ -19,9 +19,9 @@ * @category Pop * @package Pop\Paginator * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License - * @version 3.1.0 + * @version 4.0.0 */ class Paginator { @@ -33,7 +33,7 @@ class Paginator * @param int $perPage * @return Form */ - public static function createForm($total, $perPage = 10) + public static function createForm(int $total, int $perPage = 10): Form { return new Form($total, $perPage); } @@ -46,7 +46,7 @@ public static function createForm($total, $perPage = 10) * @param int $range * @return Range */ - public static function createRange($total, $perPage = 10, $range = 10) + public static function createRange(int $total, int $perPage = 10, int $range = 10): Range { return new Range($total, $perPage, $range); } diff --git a/src/PaginatorInterface.php b/src/PaginatorInterface.php new file mode 100644 index 0000000..e279b2c --- /dev/null +++ b/src/PaginatorInterface.php @@ -0,0 +1,110 @@ + + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @license http://www.popphp.org/license New BSD License + */ + +/** + * @namespace + */ +namespace Pop\Paginator; + +/** + * Paginator interface + * + * @category Pop + * @package Pop\Paginator + * @author Nick Sagona, III + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @license http://www.popphp.org/license New BSD License + * @version 4.0.0 + */ +interface PaginatorInterface +{ + + /** + * Set the query key + * + * @param string $key + * @return PaginatorInterface + */ + public function setQueryKey(string $key): PaginatorInterface; + + /** + * Set the bookends + * + * @param array $bookends + * @return PaginatorInterface + */ + public function setBookends(array $bookends): PaginatorInterface; + + /** + * Get the content items total + * + * @return int + */ + public function getTotal(): int; + + /** + * Get the per page + * + * @return int + */ + public function getPerPage(): int; + + /** + * Get the page range + * + * @return int + */ + public function getRange(): int; + + /** + * Get the query key + * + * @return string + */ + public function getQueryKey(): string; + + /** + * Get the current page + * + * @return int + */ + public function getCurrentPage(): int; + + /** + * Get the number of pages + * + * @return int + */ + public function getNumberOfPages(): int; + + /** + * Get a bookend + * + * @param string $key + * @return string|null + */ + public function getBookend(string $key): string|null; + + /** + * Get the bookends + * + * @return array + */ + public function getBookends(): array; + + /** + * Calculate the page range + * + * @param int $page + * @return array + */ + public function calculateRange(int $page = 1): array; + +} diff --git a/src/Range.php b/src/Range.php index 9a7019e..32a17e6 100644 --- a/src/Range.php +++ b/src/Range.php @@ -4,7 +4,7 @@ * * @link https://github.com/popphp/popphp-framework * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License */ @@ -19,36 +19,36 @@ * @category Pop * @package Pop\Paginator * @author Nick Sagona, III - * @copyright Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com) + * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) * @license http://www.popphp.org/license New BSD License - * @version 3.1.0 + * @version 4.0.0 */ class Range extends AbstractPaginator { /** * Link separator - * @var string + * @var ?string */ - protected $separator = null; + protected ?string $separator = null; /** * Page links property * @var array */ - protected $links = []; + protected array $links = []; /** * Class 'on' name for page link tags - * @var string + * @var ?string */ - protected $classOn = null; + protected ?string $classOn = null; /** * Class 'off' name for page link tags - * @var string + * @var ?string */ - protected $classOff = null; + protected ?string $classOff = null; /** * Set the bookend separator @@ -56,7 +56,7 @@ class Range extends AbstractPaginator * @param string $sep * @return Range */ - public function setSeparator($sep) + public function setSeparator(string $sep): Range { $this->separator = $sep; return $this; @@ -68,7 +68,7 @@ public function setSeparator($sep) * @param string $class * @return Range */ - public function setClassOn($class) + public function setClassOn(string $class): Range { $this->classOn = $class; return $this; @@ -80,7 +80,7 @@ public function setClassOn($class) * @param string $class * @return Range */ - public function setClassOff($class) + public function setClassOff(string $class): Range { $this->classOff = $class; return $this; @@ -91,7 +91,7 @@ public function setClassOff($class) * * @return string */ - public function getSeparator() + public function getSeparator(): string { return $this->separator; } @@ -99,12 +99,12 @@ public function getSeparator() /** * Get the page links * - * @param int $page + * @param ?int $page * @return array */ - public function getLinkRange($page = null) + public function getLinkRange(?int $page = null): array { - if (null === $page) { + if ($page === null) { $page = (isset($_GET[$this->queryKey]) && ((int)$_GET[$this->queryKey] > 0)) ? (int)$_GET[$this->queryKey] : 1; } $this->calculateRange($page); @@ -118,7 +118,7 @@ public function getLinkRange($page = null) * * @return string */ - public function getClassOn() + public function getClassOn(): string { return $this->classOn; } @@ -128,7 +128,7 @@ public function getClassOn() * * @return string */ - public function getClassOff() + public function getClassOff(): string { return $this->classOff; } @@ -139,7 +139,7 @@ public function getClassOff() * @param int $page * @return void */ - public function createRange($page = 1) + public function createRange(int $page = 1): void { $this->currentPage = $page; @@ -171,19 +171,19 @@ public function createRange($page = 1) $newLink = null; $prevLink = null; $nextLink = null; - $classOff = (null !== $this->classOff) ? " class=\"{$this->classOff}\"" : null; - $classOn = (null !== $this->classOn) ? " class=\"{$this->classOn}\"" : null; + $classOff = ($this->classOff !== null) ? " class=\"{$this->classOff}\"" : null; + $classOn = ($this->classOn !== null) ? " class=\"{$this->classOn}\"" : null; $newLink = ($i == $page) ? "{$i}" : "queryKey . "={$i}{$query}\">{$i}"; if (($i == $pageRange['start']) && ($pageRange['prev'])) { - if (null !== $this->bookends['start']) { + if ($this->bookends['start'] !== null) { $startLink = "queryKey . "=1" . "{$query}\">" . $this->bookends['start'] . ""; $this->links[] = $startLink; } - if (null !== $this->bookends['previous']) { + if ($this->bookends['previous'] !== null) { $prevLink = "queryKey . "=" . ($i - 1) . "{$query}\">" . $this->bookends['previous'] . ""; $this->links[] = $prevLink; @@ -193,12 +193,12 @@ public function createRange($page = 1) $this->links[] = $newLink; if (($i == $pageRange['end']) && ($pageRange['next'])) { - if (null !== $this->bookends['next']) { + if ($this->bookends['next'] !== null) { $nextLink = "queryKey . "=" . ($i + 1) . "{$query}\">" . $this->bookends['next'] . ""; $this->links[] = $nextLink; } - if (null !== $this->bookends['end']) { + if ($this->bookends['end'] !== null) { $endLink = "queryKey . "=" . $this->numberOfPages . "{$query}\">" . $this->bookends['end'] . ""; $this->links[] = $endLink; @@ -210,21 +210,21 @@ public function createRange($page = 1) /** * Wrap page links in an HTML node * - * @param string $node - * @param string $classOn - * @param string $classOff + * @param string $node + * @param ?string $classOn + * @param ?string $classOff * @return array */ - public function wrapLinks($node, $classOn = null, $classOff = null) + public function wrapLinks(string $node, ?string $classOn = null, ?string $classOff = null): array { if (empty($this->links)) { $this->getLinkRange(); } - $classOff = (null !== $classOff) ? " class=\"{$classOff}\"" : null; - $classOn = (null !== $classOn) ? " class=\"{$classOn}\"" : null; + $classOff = ($classOff !== null) ? " class=\"{$classOff}\"" : null; + $classOn = ($classOn !== null) ? " class=\"{$classOn}\"" : null; foreach ($this->links as $i => $link) { - $this->links[$i] = '<' . $node . ((strpos($link, 'span') !== false) ? $classOff : $classOn) . '>' . $link . ''; + $this->links[$i] = '<' . $node . ((str_contains($link, 'span')) ? $classOff : $classOn) . '>' . $link . ''; } return $this->links; @@ -235,7 +235,7 @@ public function wrapLinks($node, $classOn = null, $classOff = null) * * @return string */ - public function __toString() + public function __toString(): string { if (empty($this->links)) { $this->getLinkRange();