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
|
|
|
/** |
12
|
|
|
* NavigateRange. |
13
|
|
|
*/ |
14
|
|
|
class NavigateRange |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Configuration |
18
|
|
|
*/ |
19
|
|
|
protected $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
protected $left_offset = -1; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $right_offset = -1; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Configuration $config |
33
|
|
|
*/ |
34
|
12 |
|
public function __construct(Configuration $config) |
35
|
|
|
{ |
36
|
12 |
|
$this->config = $config; |
37
|
12 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return int |
41
|
|
|
*/ |
42
|
10 |
|
public function getLeftOffset() |
43
|
|
|
{ |
44
|
10 |
|
return $this->buildOffset()->left_offset; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
10 |
|
public function getRightOffset() |
51
|
|
|
{ |
52
|
10 |
|
return $this->buildOffset()->right_offset; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return NavigateRange |
57
|
|
|
*/ |
58
|
11 |
|
protected function buildOffset() |
59
|
|
|
{ |
60
|
11 |
|
if ($this->config->getTotalPages() == 1) { |
61
|
2 |
|
throw new \RangeException('Impossible calculate offsets from empty pages list'); |
62
|
|
|
} |
63
|
|
|
|
64
|
9 |
|
if ($this->left_offset < 0 && $this->right_offset < 0) { |
65
|
|
|
// definition of offset to the left and to the right of the selected page |
66
|
9 |
|
$this->left_offset = (int) floor(($this->config->getMaxNavigate() - 1) / 2); |
67
|
9 |
|
$this->right_offset = (int) ceil(($this->config->getMaxNavigate() - 1) / 2); |
68
|
|
|
|
69
|
|
|
// adjustment, if the offset is too large left |
70
|
9 |
|
if ($this->config->getCurrentPage() - $this->left_offset < 1) { |
71
|
4 |
|
$offset = (int) abs($this->config->getCurrentPage() - 1 - $this->left_offset); |
72
|
4 |
|
$this->left_offset = $this->left_offset - $offset; |
73
|
4 |
|
$this->right_offset = $this->right_offset + $offset; |
74
|
4 |
|
} |
75
|
|
|
|
76
|
|
|
// adjustment, if the offset is too large right |
77
|
9 |
|
if ($this->config->getCurrentPage() + $this->right_offset > $this->config->getTotalPages()) { |
78
|
4 |
|
$offset = (int) abs($this->config->getTotalPages() - $this->config->getCurrentPage() - $this->right_offset); |
79
|
4 |
|
$this->left_offset = $this->left_offset + $offset; |
80
|
4 |
|
$this->right_offset = $this->right_offset - $offset; |
81
|
4 |
|
} |
82
|
|
|
|
83
|
|
|
// left offset should point not lower of the first page |
84
|
9 |
|
if ($this->left_offset >= $this->config->getCurrentPage()) { |
85
|
2 |
|
$this->left_offset = $this->config->getCurrentPage() - 1; |
86
|
2 |
|
} |
87
|
9 |
|
} |
88
|
|
|
|
89
|
9 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|