1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Pagination\Twig\Behaviour; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
abstract class AbstractBehaviour implements BehaviourInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param int $totalPages |
11
|
|
|
* @param int $currentPage |
12
|
|
|
* @param int|string $omittedPagesIndicator |
13
|
|
|
* |
14
|
|
|
* @throws InvalidArgumentException |
15
|
|
|
* When pagination data is invalid |
16
|
|
|
*/ |
17
|
|
|
protected function guardPaginationData($totalPages, $currentPage, $omittedPagesIndicator = -1) |
18
|
|
|
{ |
19
|
|
|
$this->guardTotalPagesMinimumValue($totalPages); |
20
|
|
|
$this->guardCurrentPageMinimumValue($currentPage); |
21
|
|
|
$this->guardCurrentPageExistsInTotalPages($totalPages, $currentPage); |
22
|
|
|
$this->guardOmittedPagesIndicatorType($omittedPagesIndicator); |
23
|
|
|
$this->guardOmittedPagesIndicatorIntValue($totalPages, $omittedPagesIndicator); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param int $totalPages |
28
|
|
|
* |
29
|
|
|
* @throws InvalidArgumentException |
30
|
|
|
* If total number of pages is lower than 1 |
31
|
|
|
*/ |
32
|
|
|
private function guardTotalPagesMinimumValue($totalPages) |
33
|
|
|
{ |
34
|
|
|
if ($totalPages < 1) { |
35
|
|
|
throw new InvalidArgumentException(sprintf('Total number of pages (%d) should not be lower than 1.', $totalPages)); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param int $currentPage |
41
|
|
|
* |
42
|
|
|
* @throws InvalidArgumentException |
43
|
|
|
* If current page is lower than 1 |
44
|
|
|
*/ |
45
|
|
|
private function guardCurrentPageMinimumValue($currentPage) |
46
|
|
|
{ |
47
|
|
|
if ($currentPage < 1) { |
48
|
|
|
throw new InvalidArgumentException(sprintf('Current page (%d) should not be lower than 1.', $currentPage)); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param int $totalPages |
54
|
|
|
* @param int $currentPage |
55
|
|
|
* |
56
|
|
|
* @throws InvalidArgumentException |
57
|
|
|
* If current page is higher than total number of pages |
58
|
|
|
*/ |
59
|
|
|
private function guardCurrentPageExistsInTotalPages($totalPages, $currentPage) |
60
|
|
|
{ |
61
|
|
|
if ($currentPage > $totalPages) { |
62
|
|
|
throw new InvalidArgumentException(sprintf('Current page (%d) should not be higher than total number of pages (%d).', $currentPage, $totalPages)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param int|string $indicator |
68
|
|
|
* |
69
|
|
|
* @throws InvalidArgumentException |
70
|
|
|
* If omitted pages indicator is not an int or a string |
71
|
|
|
*/ |
72
|
|
|
private function guardOmittedPagesIndicatorType($indicator) |
73
|
|
|
{ |
74
|
|
|
if (!is_int($indicator) && !is_string($indicator)) { |
75
|
|
|
throw new InvalidArgumentException('Omitted pages indicator should either be a string or an int.'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $totalPages |
81
|
|
|
* @param int|string $indicator |
82
|
|
|
* |
83
|
|
|
* @throws InvalidArgumentException |
84
|
|
|
* If omitted pages indicator is an int in the range of 1 and the total |
85
|
|
|
* number of pages |
86
|
|
|
*/ |
87
|
|
|
private function guardOmittedPagesIndicatorIntValue($totalPages, $indicator) |
88
|
|
|
{ |
89
|
|
|
if (is_int($indicator) && $indicator >= 1 && $indicator <= $totalPages) { |
90
|
|
|
throw new InvalidArgumentException(sprintf('Omitted pages indicator (%d) should not be between 1 and total number of pages (%d).', $indicator, $totalPages)); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|