Skip to content

Commit

Permalink
=
Browse files Browse the repository at this point in the history
  • Loading branch information
arikaim-repository committed Jan 17, 2021
1 parent b6d8fd0 commit 2769ca9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 49 deletions.
2 changes: 1 addition & 1 deletion ArrayPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ArrayPaginator extends Paginator implements PaginatorInterface
* @param integer $page
* @param integer $perPage
*/
public function __construct($items, $page, $perPage = Paginator::DEFAULT_PER_PAGE)
public function __construct(array $items, int $page = 1, int $perPage = Paginator::DEFAULT_PER_PAGE)
{
$this->currentPage = $page;
$this->perPage = $perPage;
Expand Down
6 changes: 3 additions & 3 deletions DbPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class DbPaginator extends Paginator implements PaginatorInterface
* @param integer $page
* @param integer $perPage
*/
public function __construct($builder, $page, $perPage = Paginator::DEFAULT_PER_PAGE)
public function __construct($builder, int $page = 1, int $perPage = Paginator::DEFAULT_PER_PAGE)
{
$this->total = $builder->toBase()->getCountForPagination();
$this->items = $this->total ? $builder->forPage($page,$perPage)->get(['*']) : [];
$this->items = $this->total ? $builder->forPage($page,$perPage)->get(['*']) : [];
$this->currentPage = $page;
$this->perPage = $perPage;
$this->lastPage = $this->calcLastPage();
}
}
}
2 changes: 1 addition & 1 deletion FeedsPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FeedsPaginator extends Paginator implements PaginatorInterface
* @param integer $page
* @param string|integer $perPage
*/
public function __construct($source, $page = 1, $perPage = Paginator::DEFAULT_PER_PAGE)
public function __construct($source, int $page = 1, int $perPage = Paginator::DEFAULT_PER_PAGE)
{
$this->currentPage = $page;
$this->perPage = $perPage;
Expand Down
3 changes: 2 additions & 1 deletion JsonPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class JsonPaginator extends ArrayPaginator implements PaginatorInterface
* @param integer $page
* @param integer $perPage
*/
public function __construct($json, $page, $perPage = Paginator::DEFAULT_PER_PAGE)
public function __construct(string $json, int $page = 1, int $perPage = Paginator::DEFAULT_PER_PAGE)
{
$items = \json_decode($json,true);

parent::__construct($items,$page,$perPage);
}
}
47 changes: 26 additions & 21 deletions Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,25 @@ class Paginator implements PaginatorInterface
/**
* Constructor
*/
public function __construct()
public function __construct(
int $currentPage = 1,
array $items = [],
int $perPage = Self::DEFAULT_PER_PAGE,
int $lastPage = 1,
int $total = 1
)
{
$this->currentPage = 1;
$this->lastPage = 1;
$this->items = [];

$this->perPage = Self::DEFAULT_PER_PAGE;
$this->total = 0;
$this->currentPage = $currentPage;
$this->lastPage = $lastPage;
$this->items = $items;
$this->perPage = $perPage;
$this->total = $total;
}

/**
* Return items
*
* @return array
* @return mixed
*/
public function getItems()
{
Expand All @@ -96,7 +101,7 @@ public function getItems()
*
* @return integer
*/
public function getCurrentPage()
public function getCurrentPage(): int
{
if (empty($this->currentPage) == true) {
return 1;
Expand All @@ -115,15 +120,15 @@ public function getCurrentPage()
*/
public function getFirstItem()
{
return (isset($this->items[0]) == true) ? $this->items[0] : null;
return $this->items[0] ?? null;
}

/**
* Get total items
*
* @return integer
*/
public function getTotalItems()
public function getTotalItems(): int
{
return (empty($this->total) == true) ? 0 : $this->total;
}
Expand All @@ -143,7 +148,7 @@ public function getLastItem()
*
* @return integer
*/
public function getLastPage()
public function getLastPage(): int
{
return $this->lastPage;
}
Expand All @@ -153,7 +158,7 @@ public function getLastPage()
*
* @return integer
*/
public function getPerPage()
public function getPerPage(): int
{
return $this->perPage;
}
Expand All @@ -163,7 +168,7 @@ public function getPerPage()
*
* @return integer
*/
public function getItemsCount()
public function getItemsCount(): int
{
return \count($this->items);
}
Expand All @@ -173,7 +178,7 @@ public function getItemsCount()
*
* @return array
*/
public function toArray()
public function toArray(): array
{
return [
'paginator' => $this->getPaginatorData(),
Expand All @@ -186,7 +191,7 @@ public function toArray()
*
* @return array
*/
public function getPaginatorData()
public function getPaginatorData(): array
{
return [
'current_page' => $this->getCurrentPage(),
Expand All @@ -199,12 +204,12 @@ public function getPaginatorData()
/**
* Create paginator
*
* @param object|array|json $source
* @param object|array|string $source
* @param integer $page
* @param integer|null $perPage
* @return PaginatorInterface
*/
public static function create($source, $page = 1, $perPage = Self::DEFAULT_PER_PAGE)
public static function create($source, int $page = 1, int $perPage = Self::DEFAULT_PER_PAGE)
{
if (\is_null($source) == true || empty($source) == true) {
return new Self();
Expand Down Expand Up @@ -244,7 +249,7 @@ public static function create($source, $page = 1, $perPage = Self::DEFAULT_PER_P
* @param array $items
* @return array
*/
protected function sliceItems($items)
protected function sliceItems(array $items)
{
$offset = ($this->currentPage - 1) * $this->getPerPage();

Expand All @@ -256,8 +261,8 @@ protected function sliceItems($items)
*
* @return integer
*/
protected function calcLastPage()
protected function calcLastPage(): int
{
return \max((int)\ceil($this->total / $this->perPage), 1);
return \max((int)\ceil($this->total / $this->perPage),1);
}
}
6 changes: 3 additions & 3 deletions PaginatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function getItems();
*
* @return integer
*/
public function getCurrentPage();
public function getCurrentPage(): int;

/**
* Return first item
Expand All @@ -40,12 +40,12 @@ public function getFirstItem();
*
* @return array
*/
public function toArray();
public function toArray(): array;

/**
* Return items count
*
* @return integer
*/
public function getItemsCount();
public function getItemsCount(): int;
}
40 changes: 21 additions & 19 deletions SessionPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class SessionPaginator
* Return view type
*
* @param string|null $namespace
* @param string $default
* @param string|null $default
* @return string
*/
public static function getViewType($namespace = null, $default = Paginator::TABLE_VIEW)
public static function getViewType(?string $namespace = null, ?string $default = Paginator::TABLE_VIEW)
{
return Session::get(Utils::createKey('paginator.view.type',$namespace),$default);
}
Expand All @@ -37,22 +37,24 @@ public static function getViewType($namespace = null, $default = Paginator::TABL
* @param string $type
* @return void
*/
public static function setViewType($type, $namespace = null)
public static function setViewType(string $type, ?string $namespace = null): void
{
$type = (empty($type) == true) ? Paginator::TABLE_VIEW : $type;

return Session::set(Utils::createKey('paginator.view.type',$namespace),$type);
Session::set(Utils::createKey('paginator.view.type',$namespace),$type);
}

/**
* Get rows per page
*
* @param string|null $namespace
* @return void
* @return int
*/
public static function getRowsPerPage($namespace = null)
public static function getRowsPerPage(?string $namespace = null): int
{
return Session::get(Utils::createKey('paginator.page',$namespace),Paginator::DEFAULT_PER_PAGE);
$rows = Session::get(Utils::createKey('paginator.page',$namespace),Paginator::DEFAULT_PER_PAGE);

return (empty($rows) == true) ? Paginator::DEFAULT_PER_PAGE : (int)$rows;
}

/**
Expand All @@ -62,7 +64,7 @@ public static function getRowsPerPage($namespace = null)
* @param integer $rows
* @return void
*/
public static function setRowsPerPage($rows, $namespace = null)
public static function setRowsPerPage(int $rows, ?string $namespace = null): void
{
$rows = ($rows < 1) ? 1 : $rows;
Session::set(Utils::createKey('paginator.page',$namespace),$rows);
Expand All @@ -74,7 +76,7 @@ public static function setRowsPerPage($rows, $namespace = null)
* @param string|null $namespace
* @return integer
*/
public static function getCurrentPage($namespace = null)
public static function getCurrentPage(?string $namespace = null): int
{
$page = (integer)Session::get(Utils::createKey('paginator.current.page',$namespace),1);
return ($page < 1) ? 1 : $page;
Expand All @@ -87,7 +89,7 @@ public static function getCurrentPage($namespace = null)
* @param integer $page
* @return void
*/
public static function setCurrentPage($page, $namespace = null)
public static function setCurrentPage(int $page, ?string $namespace = null): void
{
$page = ($page < 1 || empty($page) == true) ? 1 : $page;
Session::set(Utils::createKey('paginator.current.page',$namespace),$page);
Expand All @@ -96,12 +98,13 @@ public static function setCurrentPage($page, $namespace = null)
/**
* Create paginator
*
* @param object|array|json $source
* @param string|null $namespace
* @param integer|null $pageSize
* @param object|array|json $source
* @param integer|null $currentPage
* @return array
*/
public static function create($source, $namespace = null, $pageSize = null, $currentPage = null)
public static function create($source, ?string $namespace = null, ?int $pageSize = null, ?int $currentPage = null)
{
$pageSize = (empty($pageSize) == true) ? Self::getRowsPerPage($namespace) : $pageSize;
$currentPage = (empty($currentPage) == true) ? Self::getCurrentPage($namespace) : $currentPage;
Expand All @@ -113,8 +116,7 @@ public static function create($source, $namespace = null, $pageSize = null, $cur
Self::setCurrentPage(1,$namespace);
$paginator = Paginator::create($source,1,$pageSize);
$data = $paginator->toArray();
}

}
Self::savePaginator($namespace,$data['paginator']);

return $paginator;
Expand All @@ -127,7 +129,7 @@ public static function create($source, $namespace = null, $pageSize = null, $cur
* @param array $data
* @return void
*/
public static function savePaginator($namespace, $data)
public static function savePaginator(?string $namespace, $data): void
{
Session::set(Utils::createKey('paginator',$namespace),$data);
}
Expand All @@ -136,9 +138,9 @@ public static function savePaginator($namespace, $data)
* Read paginator data from session
*
* @param string|null $namespace
* @return array
* @return array|null
*/
public static function getPaginator($namespace)
public static function getPaginator(?string $namespace): ?array
{
$paginator = Session::get(Utils::createKey('paginator',$namespace),[]);
$paginator['current_page'] = Self::getCurrentPage($namespace);
Expand All @@ -149,10 +151,10 @@ public static function getPaginator($namespace)
/**
* Clear paginator data
*
* @param string $namespace
* @param string|null $namespace
* @return void
*/
public static function clearPaginator($namespace)
public static function clearPaginator(?string $namespace): void
{
Session::remove(Utils::createKey('paginator',$namespace));
Self::setCurrentPage(1,$namespace);
Expand Down

0 comments on commit 2769ca9

Please sign in to comment.