|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* soluble-flexstore library |
|
5
|
|
|
* |
|
6
|
|
|
* @author Vanvelthem Sébastien |
|
7
|
|
|
* @link https://github.com/belgattitude/soluble-flexstore |
|
8
|
|
|
* @copyright Copyright (c) 20016-2017 Vanvelthem Sébastien |
|
9
|
|
|
* @license MIT License https://github.com/belgattitude/soluble-flexstore/blob/master/LICENSE.md |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Soluble\FlexStore\Helper; |
|
14
|
|
|
|
|
15
|
|
|
use Soluble\FlexStore\Exception; |
|
16
|
|
|
use Zend\Paginator\Paginator as ZendPaginator; |
|
17
|
|
|
use Zend\Paginator\ScrollingStyle\ScrollingStyleInterface; |
|
18
|
|
|
|
|
19
|
|
|
class Paginator extends ZendPaginator |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Default set of scrolling. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $scrollingTypes = [ |
|
27
|
|
|
'all' => 'Zend\Paginator\ScrollingStyle\All', |
|
28
|
|
|
'elastic' => 'Zend\Paginator\ScrollingStyle\Elastic', |
|
29
|
|
|
'jumping' => 'Zend\Paginator\ScrollingStyle\Jumping', |
|
30
|
|
|
'sliding' => 'Zend\Paginator\ScrollingStyle\Sliding', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param int $totalRows |
|
35
|
|
|
* @param int $limit |
|
36
|
|
|
* @param int $offset |
|
37
|
|
|
* |
|
38
|
|
|
* @throws Exception\InvalidUsageException |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function __construct($totalRows, $limit, $offset = 0) |
|
41
|
|
|
{ |
|
42
|
2 |
|
$totalRows = filter_var($totalRows, FILTER_VALIDATE_INT); |
|
43
|
2 |
|
$limit = filter_var($limit, FILTER_VALIDATE_INT); |
|
44
|
2 |
|
$offset = filter_var($offset, FILTER_VALIDATE_INT); |
|
45
|
|
|
|
|
46
|
2 |
|
if (!is_int($limit) || $limit < 0) { |
|
47
|
1 |
|
throw new Exception\InvalidUsageException(__METHOD__ . ' expects limit to be an integer greater than 0'); |
|
48
|
|
|
} |
|
49
|
2 |
|
if (!is_int($totalRows) || $totalRows < 0) { |
|
50
|
1 |
|
throw new Exception\InvalidUsageException(__METHOD__ . ' expects total rows to be an integer greater than 0'); |
|
51
|
|
|
} |
|
52
|
2 |
|
if (!is_int($offset) || $offset < 0) { |
|
53
|
1 |
|
throw new Exception\InvalidUsageException(__METHOD__ . ' expects offset to be an integer greater than 0'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
if (class_exists('\Zend\Paginator\Adapter\NullFill')) { |
|
57
|
1 |
|
$adapter = new \Zend\Paginator\Adapter\NullFill($totalRows); |
|
58
|
1 |
|
} else { |
|
59
|
|
|
throw new Exception\RuntimeException(__METHOD__ . " Missing Zend\Paginator\Adapter."); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
parent::__construct($adapter); |
|
63
|
1 |
|
$this->setItemCountPerPage($limit); |
|
64
|
1 |
|
$this->setCurrentPageNumber(ceil(($offset + 1) / $limit)); |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Loads a scrolling style. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $scrollingStyle |
|
71
|
|
|
* |
|
72
|
|
|
* @return ScrollingStyleInterface |
|
73
|
|
|
* |
|
74
|
|
|
* @throws Exception\InvalidArgumentException |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function _loadScrollingStyle($scrollingStyle = null) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($scrollingStyle === null) { |
|
79
|
|
|
$scrollingStyle = static::$defaultScrollingStyle; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
switch (strtolower(gettype($scrollingStyle))) { |
|
83
|
|
|
case 'object': |
|
84
|
|
|
if (!$scrollingStyle instanceof ScrollingStyleInterface) { |
|
85
|
|
|
throw new Exception\InvalidArgumentException( |
|
86
|
|
|
'Scrolling style must implement Zend\Paginator\ScrollingStyle\ScrollingStyleInterface' |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $scrollingStyle; |
|
91
|
|
|
|
|
92
|
|
|
case 'string': |
|
93
|
|
|
if (!array_key_exists(strtolower($scrollingStyle), $this->scrollingTypes)) { |
|
94
|
|
|
throw new Exception\InvalidArgumentException( |
|
95
|
|
|
"Scrolling type '$scrollingStyle' is not supported, look for (" . |
|
96
|
|
|
implode(',', array_keys($this->scrollingTypes)) . |
|
97
|
|
|
')' |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
$cls = $this->scrollingTypes[strtolower($scrollingStyle)]; |
|
101
|
|
|
|
|
102
|
|
|
return new $cls(); |
|
103
|
|
|
|
|
104
|
|
|
case 'null': |
|
105
|
|
|
// Fall through to default case |
|
106
|
|
|
|
|
107
|
|
|
default: |
|
108
|
|
|
throw new Exception\InvalidArgumentException( |
|
109
|
|
|
'Scrolling style must be a class ' . |
|
110
|
|
|
'name or object implementing Zend\Paginator\ScrollingStyle\ScrollingStyleInterface' |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|