-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionPaginator.php
163 lines (146 loc) · 4.64 KB
/
SessionPaginator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <[email protected]>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\Paginator;
use Arikaim\Core\Utils\Utils;
use Arikaim\Core\Paginator\Paginator;
use Arikaim\Core\Paginator\PaginatorFactory;
use Arikaim\Core\Http\Session;
/**
* Paginator session helper
*/
class SessionPaginator
{
/**
* Return view type
*
* @param string|null $namespace
* @param string|null $default
* @return string
*/
public static function getViewType(?string $namespace = null, ?string $default = Paginator::TABLE_VIEW)
{
return Session::get(Utils::createKey('paginator.view.type',$namespace),$default);
}
/**
* Set view type
*
* @param string|null $namespace
* @param string $type
* @return void
*/
public static function setViewType(string $type, ?string $namespace = null): void
{
$type = (empty($type) == true) ? Paginator::TABLE_VIEW : $type;
Session::set(Utils::createKey('paginator.view.type',$namespace),$type);
}
/**
* Get rows per page
*
* @param string|null $namespace
* @return int
*/
public static function getRowsPerPage(?string $namespace = null): int
{
$rows = Session::get(Utils::createKey('paginator.page',$namespace),Paginator::DEFAULT_PER_PAGE);
return (empty($rows) == true) ? Paginator::DEFAULT_PER_PAGE : (int)$rows;
}
/**
* Set rows per page value
*
* @param string|null $namespace
* @param integer $rows
* @return void
*/
public static function setRowsPerPage(int $rows, ?string $namespace = null): void
{
$rows = ($rows < 1) ? 1 : $rows;
Session::set(Utils::createKey('paginator.page',$namespace),$rows);
}
/**
* Return current page
*
* @param string|null $namespace
* @return integer
*/
public static function getCurrentPage(?string $namespace = null): int
{
$page = (integer)Session::get(Utils::createKey('paginator.current.page',$namespace),1);
return ($page < 1) ? 1 : $page;
}
/**
* Set current page
*
* @param string|null $namespace
* @param integer $page
* @return void
*/
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);
}
/**
* Create paginator
*
* @param object|array|json $source
* @param string|null $namespace
* @param integer|null $pageSize
* @param integer|null $currentPage
* @return mixed
*/
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;
$paginator = PaginatorFactory::create($source,$currentPage,$pageSize);
$data = $paginator->toArray();
if ($paginator->getItemsCount() == 0 && $currentPage > 1) {
Self::setCurrentPage(1,$namespace);
$paginator = PaginatorFactory::create($source,1,$pageSize);
$data = $paginator->toArray();
}
Self::savePaginator($namespace,$data['paginator']);
return $paginator;
}
/**
* Save paginator array in session
*
* @param string|null $namespace
* @param array $data
* @return void
*/
public static function savePaginator(?string $namespace, $data): void
{
Session::set(Utils::createKey('paginator',$namespace),$data);
}
/**
* Read paginator data from session
*
* @param string|null $namespace
* @return array|null
*/
public static function getPaginator(?string $namespace): ?array
{
$paginator = Session::get(Utils::createKey('paginator',$namespace),[]);
$paginator['current_page'] = Self::getCurrentPage($namespace);
return $paginator;
}
/**
* Clear paginator data
*
* @param string|null $namespace
* @return void
*/
public static function clearPaginator(?string $namespace): void
{
Session::remove(Utils::createKey('paginator',$namespace));
Self::setCurrentPage(1,$namespace);
}
}