Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mazanax committed Jan 12, 2018
1 parent 8d081ae commit ca8c705
Showing 1 changed file with 74 additions and 20 deletions.
94 changes: 74 additions & 20 deletions src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@

class Paginator
{
const DEFAULT_ON_PAGE = 20;
const DEFAULT_SEPARATOR = null;
const DEFAULT_SECTION_SIZE = 5;

const OPTION_ON_PAGE = 'on_page';
const OPTION_SEPARATOR = 'separator';
const OPTION_SECTION_SIZE = 'section_size';

private $onPage = self::DEFAULT_ON_PAGE;
private $separator = self::DEFAULT_SEPARATOR;
private $sectionSize = self::DEFAULT_SECTION_SIZE;
private $separator;
private $onPage = 20;
private $sectionSize = 5;

/**
* @param array $options
Expand Down Expand Up @@ -42,12 +38,28 @@ public function getPages(int $currentPage, int $itemsCount): array
{
$firstPage = 1;
$lastPage = (int)ceil($itemsCount / $this->onPage);
$limit = $this->sectionSize - 1;
$partSize = (int)floor($this->sectionSize / 2);

$separator = $this->separator && $lastPage > $this->sectionSize ? [$this->separator] : [];
$firstSection = [$firstPage];
$lastSection = [$lastPage];
$commonSection = $this->buildCommonSection($firstPage, $lastPage, $currentPage);
$firstSection = array_diff([$firstPage], $commonSection);
$lastSection = array_diff([$lastPage], $commonSection);

$firstSeparator = $this->getFirstSeparator($firstSection, $currentPage, $lastPage);
$lastSeparator = $this->getLastSeparator($lastSection, $currentPage, $lastPage);

return array_merge($firstSection, $firstSeparator, $commonSection, $lastSeparator, $lastSection);
}

/**
* @param int $firstPage
* @param int $lastPage
* @param int $currentPage
*
* @return array
*/
private function buildCommonSection(int $firstPage, int $lastPage, int $currentPage): array
{
$limit = $this->sectionSize - 1;
$partSize = $this->getPartSize();

if ($currentPage >= $firstPage + $limit && $currentPage <= $lastPage - $limit) {
$commonSection = range($currentPage - $partSize, $currentPage + $partSize);
Expand All @@ -57,18 +69,60 @@ public function getPages(int $currentPage, int $itemsCount): array
$commonSection = range(max($firstPage, $lastPage - $limit), $lastPage);
}

$firstSection = array_diff($firstSection, $commonSection);
$lastSection = array_diff($lastSection, $commonSection);
return $commonSection;
}

$firstSeparator = !empty($firstSection) ? $separator : [];
$lastSeparator = !empty($lastSection) ? $separator : [];
if ($currentPage - $partSize - 1 <= $firstPage) {
$firstSeparator = [];
/**
* @param array $firstSection
* @param int $currentPage
* @param int $lastPage
*
* @return array
*/
private function getFirstSeparator(array $firstSection, int $currentPage, int $lastPage): array
{
$partSize = $this->getPartSize();

if ($currentPage - $partSize - 1 <= 1) {
return [];
}

return !empty($firstSection) ? $this->getSeparator($lastPage) : [];
}

/**
* @param array $lastSection
* @param int $currentPage
* @param int $lastPage
*
* @return array
*/
private function getLastSeparator(array $lastSection, int $currentPage, int $lastPage): array
{
$partSize = $this->getPartSize();

if ($currentPage + $partSize + 1 >= $lastPage) {
$lastSeparator = [];
return [];
}

return array_merge($firstSection, $firstSeparator, $commonSection, $lastSeparator, $lastSection);
return !empty($lastSection) ? $this->getSeparator($lastPage) : [];
}

/**
* @param int $lastPage
*
* @return array
*/
private function getSeparator(int $lastPage): array
{
return $this->separator && $lastPage > $this->sectionSize ? [$this->separator] : [];
}

/**
* @return int
*/
private function getPartSize(): int
{
return (int)floor($this->sectionSize / 2);
}
}

0 comments on commit ca8c705

Please sign in to comment.