1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\PaginationBundle\Service; |
10
|
|
|
|
11
|
|
|
class NavigateRange |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Configuration |
15
|
|
|
*/ |
16
|
|
|
protected $config; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
protected $left_offset = -1; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $right_offset = -1; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Configuration $config |
30
|
|
|
*/ |
31
|
11 |
|
public function __construct(Configuration $config) |
32
|
|
|
{ |
33
|
11 |
|
$this->config = $config; |
34
|
11 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return int |
38
|
|
|
*/ |
39
|
10 |
|
public function getLeftOffset() |
40
|
|
|
{ |
41
|
10 |
|
return $this->buildOffset()->left_offset; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return int |
46
|
|
|
*/ |
47
|
10 |
|
public function getRightOffset() |
48
|
|
|
{ |
49
|
10 |
|
return $this->buildOffset()->right_offset; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return NavigateRange |
54
|
|
|
*/ |
55
|
10 |
|
protected function buildOffset() |
56
|
|
|
{ |
57
|
10 |
|
if ($this->left_offset < 0) { |
58
|
10 |
|
$this |
59
|
10 |
|
->definitionOffset() |
60
|
10 |
|
->adjustmentLargeLeftOffset() |
61
|
10 |
|
->adjustmentLargeRightOffset() |
62
|
10 |
|
->adjustmentLowerLeftOffset(); |
63
|
10 |
|
} |
64
|
|
|
|
65
|
10 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Definition of offset to the left and to the right of the selected page. |
70
|
|
|
* |
71
|
|
|
* @return NavigateRange |
72
|
|
|
*/ |
73
|
10 |
|
protected function definitionOffset() |
74
|
|
|
{ |
75
|
10 |
|
$this->left_offset = (int) floor(($this->config->getMaxNavigate() - 1) / 2); |
76
|
10 |
|
$this->right_offset = (int) ceil(($this->config->getMaxNavigate() - 1) / 2); |
77
|
|
|
|
78
|
10 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Adjustment, if the offset is too large left. |
83
|
|
|
* |
84
|
|
|
* @return NavigateRange |
85
|
|
|
*/ |
86
|
10 |
|
protected function adjustmentLargeLeftOffset() |
87
|
|
|
{ |
88
|
10 |
|
if ($this->config->getCurrentPage() - $this->left_offset < 1) { |
89
|
5 |
|
$offset = (int) abs($this->config->getCurrentPage() - 1 - $this->left_offset); |
90
|
5 |
|
$this->left_offset = $this->left_offset - $offset; |
91
|
5 |
|
$this->right_offset = $this->right_offset + $offset; |
92
|
5 |
|
} |
93
|
|
|
|
94
|
10 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Adjustment, if the offset is too large right. |
99
|
|
|
* |
100
|
|
|
* @return NavigateRange |
101
|
|
|
*/ |
102
|
10 |
|
protected function adjustmentLargeRightOffset() |
103
|
|
|
{ |
104
|
10 |
|
if ($this->config->getCurrentPage() + $this->right_offset > $this->config->getTotalPages()) { |
105
|
5 |
|
$offset = (int) abs( |
106
|
5 |
|
$this->config->getTotalPages() - |
107
|
5 |
|
$this->config->getCurrentPage() - |
108
|
5 |
|
$this->right_offset |
109
|
5 |
|
); |
110
|
5 |
|
$this->left_offset = $this->left_offset + $offset; |
111
|
5 |
|
$this->right_offset = $this->right_offset - $offset; |
112
|
5 |
|
} |
113
|
|
|
|
114
|
10 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Left offset should point not lower of the first page. |
119
|
|
|
* |
120
|
|
|
* @return NavigateRange |
121
|
|
|
*/ |
122
|
10 |
|
protected function adjustmentLowerLeftOffset() |
123
|
|
|
{ |
124
|
10 |
|
if ($this->left_offset >= $this->config->getCurrentPage()) { |
125
|
3 |
|
$this->left_offset = $this->config->getCurrentPage() - 1; |
126
|
3 |
|
} |
127
|
|
|
|
128
|
10 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|