1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Foo\Grid\Pagination; |
6
|
|
|
|
7
|
|
|
use Foo\Grid\Action\Button; |
8
|
|
|
use Foo\Grid\Collection\Actions; |
9
|
|
|
use Foo\Grid\Component\Component; |
10
|
|
|
|
11
|
|
|
class Pagination extends Component implements IPagination |
12
|
|
|
{ |
13
|
|
|
/** @var int */ |
14
|
|
|
protected $from = 0; |
15
|
|
|
|
16
|
|
|
/** @var int */ |
17
|
|
|
protected $to = 0; |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
protected $pageSize = 0; |
21
|
|
|
|
22
|
|
|
/** @var int */ |
23
|
|
|
protected $totalCount = 0; |
24
|
|
|
|
25
|
|
|
/** @var int */ |
26
|
|
|
protected $numberCount = 5; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
protected $pageSizes = [10, 50, 200]; |
30
|
|
|
|
31
|
|
|
/** @var array */ |
32
|
|
|
protected $attributes = ['class' => 'hello']; |
33
|
|
|
|
34
|
|
|
/** @var Actions */ |
35
|
|
|
protected $buttons; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param int $from |
39
|
|
|
* @param int $pageSize |
40
|
|
|
* @param int $totalCount |
41
|
|
|
* @param int $maxNumButtons |
|
|
|
|
42
|
|
|
* @param array $pageSizes |
43
|
|
|
* @param array $attributes |
44
|
|
|
*/ |
45
|
15 |
|
public function __construct( |
46
|
|
|
$from, |
47
|
|
|
$pageSize, |
48
|
|
|
$totalCount, |
49
|
|
|
$numberCount = 5, |
50
|
|
|
$pageSizes = [10, 50, 200], |
51
|
|
|
$attributes = [] |
52
|
|
|
) { |
53
|
15 |
|
$this->from = $from; |
54
|
15 |
|
$this->pageSize = $pageSize; |
55
|
15 |
|
$this->totalCount = $totalCount; |
56
|
15 |
|
$this->pageSizes = $pageSizes; |
57
|
15 |
|
$this->numberCount = $numberCount; |
58
|
|
|
|
59
|
15 |
|
$this->to = min($this->totalCount, $this->from + $this->pageSize - 1); |
60
|
15 |
|
$this->buttons = new Actions(); |
61
|
|
|
|
62
|
15 |
|
parent::__construct('', 'div', $attributes); |
63
|
|
|
|
64
|
15 |
|
$this->appendToAttribute(Component::ATTRIBUTE_CLASS, 'grid-pagination'); |
65
|
15 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
3 |
|
public function getTo(): int |
71
|
|
|
{ |
72
|
3 |
|
return min($this->totalCount, $this->from + $this->pageSize - 1); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
9 |
|
public function getMinPageNumber(): int |
79
|
|
|
{ |
80
|
9 |
|
$currentPage = (int)floor($this->from / $this->pageSize); |
81
|
9 |
|
$minPage = (int)($currentPage - floor($this->numberCount / 2)); |
82
|
9 |
|
$result = (int)max($minPage, 0) + 1; |
83
|
|
|
|
84
|
9 |
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
9 |
|
public function getMaxPageNumber(): int |
91
|
|
|
{ |
92
|
9 |
|
$currentPage = (int)floor($this->from / $this->pageSize); |
93
|
9 |
|
$maxPage = (int)($currentPage + floor($this->numberCount / 2) + 1); |
94
|
9 |
|
$result = (int)min($maxPage, floor($this->totalCount / $this->pageSize)); |
95
|
|
|
|
96
|
9 |
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
6 |
|
public function getPageNumbers(): array |
103
|
|
|
{ |
104
|
6 |
|
$minPageNumber = $this->getMinPageNumber(); |
105
|
6 |
|
$maxPageNumber = $this->getMaxPageNumber(); |
106
|
|
|
|
107
|
6 |
|
$numbers = []; |
108
|
6 |
|
for ($i = 0; ($i < $this->numberCount) && ($minPageNumber + $i <= $maxPageNumber); $i++) { |
109
|
6 |
|
$numbers[] = $minPageNumber + $i; |
110
|
|
|
} |
111
|
|
|
|
112
|
6 |
|
return $numbers; |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
public function createButtons() |
116
|
|
|
{ |
117
|
3 |
|
$numbers = $this->getPageNumbers(); |
118
|
|
|
|
119
|
3 |
|
$lastIndex = count($numbers) - 1; |
120
|
|
|
|
121
|
3 |
|
if ($this->from < 1) { |
122
|
|
|
$this->buttons[] = new Button('<<'); |
123
|
|
|
$this->buttons[] = new Button('<'); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
if ($numbers[0] > 1) { |
127
|
2 |
|
$this->buttons[] = new Button('...'); |
128
|
|
|
} |
129
|
|
|
|
130
|
3 |
|
foreach ($numbers as $number) { |
131
|
3 |
|
$this->buttons[] = new Button("$number"); |
132
|
|
|
} |
133
|
|
|
|
134
|
3 |
|
if ($numbers[$lastIndex] * $this->pageSize < $this->totalCount) { |
135
|
2 |
|
$this->buttons[] = new Button('...'); |
136
|
|
|
} |
137
|
|
|
|
138
|
3 |
|
if ($this->to < $this->totalCount) { |
139
|
2 |
|
$this->buttons[] = new Button('>'); |
140
|
2 |
|
$this->buttons[] = new Button('>>'); |
141
|
|
|
} |
142
|
3 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
3 |
|
public function __toString(): string |
148
|
|
|
{ |
149
|
3 |
|
$this->createButtons(); |
150
|
|
|
|
151
|
3 |
|
$this->content = (string)$this->buttons; |
152
|
|
|
|
153
|
3 |
|
return parent::__toString(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.