|
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 Aura\SqlQuery\Common\Select; |
|
11
|
|
|
use Aura\SqlQuery\Common\SelectInterface; |
|
12
|
|
|
use Pagerfanta\Exception\LogicException; |
|
13
|
|
|
use Pagerfanta\Pagerfanta; |
|
14
|
|
|
use Pagerfanta\View\ViewInterface; |
|
15
|
|
|
use Ray\AuraSqlModule\Annotation\PagerViewOption; |
|
16
|
|
|
use Ray\AuraSqlModule\Exception\NotInitialized; |
|
17
|
|
|
|
|
18
|
|
|
class AuraSqlQueryPager implements AuraSqlQueryPagerInterface, \ArrayAccess |
|
19
|
|
|
{ |
|
20
|
|
|
private $pdo; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ViewInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $view; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var RouteGeneratorInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $routeGenerator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $viewOptions; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var SelectInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $select; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var int |
|
44
|
|
|
*/ |
|
45
|
|
|
private $paging; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param ViewInterface $view |
|
49
|
|
|
* @param array $viewOptions |
|
50
|
|
|
* |
|
51
|
|
|
* @PagerViewOption("viewOptions") |
|
52
|
|
|
*/ |
|
53
|
8 |
|
public function __construct(ViewInterface $view, array $viewOptions) |
|
54
|
|
|
{ |
|
55
|
8 |
|
$this->view = $view; |
|
56
|
8 |
|
$this->viewOptions = $viewOptions; |
|
57
|
8 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function init(ExtendedPdoInterface $pdo, SelectInterface $select, $paging, RouteGeneratorInterface $routeGenerator) |
|
63
|
|
|
{ |
|
64
|
3 |
|
$this->pdo = $pdo; |
|
65
|
3 |
|
$this->select = $select; |
|
66
|
3 |
|
$this->paging = $paging; |
|
67
|
3 |
|
$this->routeGenerator = $routeGenerator; |
|
68
|
3 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
6 |
|
public function offsetGet($page) |
|
74
|
|
|
{ |
|
75
|
6 |
|
if (! $this->routeGenerator instanceof RouteGeneratorInterface) { |
|
76
|
1 |
|
throw new NotInitialized(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
5 |
|
$countQueryBuilderModifier = function (Select $select) { |
|
80
|
5 |
|
foreach (\array_keys($select->getCols()) as $key) { |
|
81
|
5 |
|
$select->removeCol($key); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
5 |
|
return $select->cols(['COUNT(*) AS total_results'])->resetOrderBy()->limit(1); |
|
85
|
5 |
|
}; |
|
86
|
5 |
|
$pagerfanta = new Pagerfanta(new AuraSqlQueryAdapter($this->pdo, $this->select, $countQueryBuilderModifier)); |
|
|
|
|
|
|
87
|
5 |
|
$pagerfanta->setMaxPerPage($this->paging); |
|
88
|
5 |
|
$pagerfanta->setCurrentPage($page); |
|
89
|
|
|
|
|
90
|
5 |
|
$pager = new Page($pagerfanta, $this->routeGenerator, $this->view, $this->viewOptions); |
|
91
|
5 |
|
$pager->maxPerPage = $pagerfanta->getMaxPerPage(); |
|
92
|
5 |
|
$pager->current = $pagerfanta->getCurrentPage(); |
|
93
|
5 |
|
$pager->hasNext = $pagerfanta->hasNextPage(); |
|
94
|
5 |
|
$pager->hasPrevious = $pagerfanta->hasPreviousPage(); |
|
95
|
5 |
|
$pager->data = $pagerfanta->getCurrentPageResults(); |
|
96
|
5 |
|
$pager->total = $pagerfanta->getNbResults(); |
|
97
|
|
|
|
|
98
|
5 |
|
return $pager; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function offsetExists($offset) |
|
105
|
|
|
{ |
|
106
|
1 |
|
throw new LogicException('unsupported'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function offsetSet($offset, $value) |
|
113
|
|
|
{ |
|
114
|
1 |
|
throw new LogicException('read only'); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inheritdoc} |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function offsetUnset($offset) |
|
121
|
|
|
{ |
|
122
|
1 |
|
throw new LogicException('read only'); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.