|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Ray.AuraSqlModule package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Ray\AuraSqlModule\Pagerfanta; |
|
8
|
|
|
|
|
9
|
|
|
use Aura\Sql\ExtendedPdoInterface; |
|
10
|
|
|
use Pagerfanta\Exception\LogicException; |
|
11
|
|
|
use Pagerfanta\Pagerfanta; |
|
12
|
|
|
use Pagerfanta\View\ViewInterface; |
|
13
|
|
|
use Ray\AuraSqlModule\Annotation\PagerViewOption; |
|
14
|
|
|
use Ray\AuraSqlModule\Exception\NotInitialized; |
|
15
|
|
|
|
|
16
|
|
|
class AuraSqlPager implements AuraSqlPagerInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ViewInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $view; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var RouteGeneratorInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $routeGenerator; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $viewOptions; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var ExtendedPdoInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $pdo; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $sql; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
private $params; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var int |
|
50
|
|
|
*/ |
|
51
|
|
|
private $paging; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param ViewInterface $view |
|
55
|
|
|
* @param array $viewOptions |
|
56
|
|
|
* |
|
57
|
|
|
* @PagerViewOption("viewOptions") |
|
58
|
|
|
*/ |
|
59
|
8 |
|
public function __construct(ViewInterface $view, array $viewOptions) |
|
60
|
|
|
{ |
|
61
|
8 |
|
$this->view = $view; |
|
62
|
8 |
|
$this->viewOptions = $viewOptions; |
|
63
|
8 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
3 |
|
public function init(ExtendedPdoInterface $pdo, $sql, array $params, $paging, RouteGeneratorInterface $routeGenerator) |
|
69
|
|
|
{ |
|
70
|
3 |
|
$this->pdo = $pdo; |
|
71
|
3 |
|
$this->sql = $sql; |
|
72
|
3 |
|
$this->params = $params; |
|
73
|
3 |
|
$this->paging = $paging; |
|
74
|
3 |
|
$this->routeGenerator = $routeGenerator; |
|
75
|
3 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function offsetExists($offset) |
|
81
|
|
|
{ |
|
82
|
1 |
|
throw new LogicException('unsupported'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
5 |
|
public function offsetGet($currentPage) |
|
89
|
|
|
{ |
|
90
|
5 |
|
if (! $this->routeGenerator instanceof RouteGeneratorInterface) { |
|
91
|
1 |
|
throw new NotInitialized(); |
|
92
|
|
|
} |
|
93
|
4 |
|
$pagerfanta = new Pagerfanta(new ExtendedPdoAdapter($this->pdo, $this->sql, $this->params)); |
|
94
|
4 |
|
$pagerfanta->setMaxPerPage($this->paging); |
|
95
|
4 |
|
$pagerfanta->setCurrentPage($currentPage); |
|
96
|
4 |
|
$page = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions); |
|
97
|
4 |
|
$page->maxPerPage = $pagerfanta->getMaxPerPage(); |
|
98
|
4 |
|
$page->current = $pagerfanta->getCurrentPage(); |
|
99
|
4 |
|
$page->hasNext = $pagerfanta->hasNextPage(); |
|
100
|
4 |
|
$page->hasPrevious = $pagerfanta->hasPreviousPage(); |
|
101
|
4 |
|
$page->data = $pagerfanta->getCurrentPageResults(); |
|
102
|
4 |
|
$page->total = $pagerfanta->getNbResults(); |
|
103
|
|
|
|
|
104
|
4 |
|
return $page; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritdoc} |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function offsetSet($offset, $value) |
|
111
|
|
|
{ |
|
112
|
1 |
|
throw new LogicException('read only'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* {@inheritdoc} |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function offsetUnset($offset) |
|
119
|
|
|
{ |
|
120
|
1 |
|
throw new LogicException('read only'); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|