1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Common Component. |
7
|
|
|
* |
8
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2016 Sourcefabric z.ú. |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Component\Common\Pagination; |
18
|
|
|
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
|
21
|
|
|
class PaginationData implements PaginationInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $pageNumber = 1; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $limit = 10; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $firstResult = 0; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $orderDirection = 'asc'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $orderFields = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
protected $orderAliases = []; |
52
|
|
|
|
53
|
4 |
|
public function __construct(Request $request = null) |
54
|
|
|
{ |
55
|
4 |
|
if (null !== $request) { |
56
|
4 |
|
$this->resolveFromRequest($request); |
57
|
|
|
} |
58
|
4 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
4 |
|
public function getPageNumber(): int |
64
|
|
|
{ |
65
|
4 |
|
return $this->pageNumber; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param int $pageNumber |
70
|
|
|
*/ |
71
|
4 |
|
public function setPageNumber(int $pageNumber) |
72
|
|
|
{ |
73
|
4 |
|
$this->pageNumber = $pageNumber; |
74
|
4 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
4 |
|
public function getLimit(): int |
80
|
|
|
{ |
81
|
4 |
|
return $this->limit; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int $limit |
86
|
|
|
*/ |
87
|
4 |
|
public function setLimit(int $limit) |
88
|
|
|
{ |
89
|
4 |
|
$this->limit = $limit; |
90
|
4 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return int |
94
|
|
|
*/ |
95
|
|
|
public function getFirstResult(): int |
96
|
|
|
{ |
97
|
|
|
if ($this->getPageNumber() > 1) { |
98
|
|
|
return $this->getPageNumber() * $this->getLimit(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->firstResult; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param int $firstResult |
106
|
|
|
*/ |
107
|
|
|
public function setFirstResult(int $firstResult) |
108
|
|
|
{ |
109
|
|
|
$this->firstResult = $firstResult; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array $order |
114
|
|
|
*/ |
115
|
|
|
public function setOrder(array $order) |
116
|
|
|
{ |
117
|
|
|
if (count($order) == 2 && in_array(strtolower($order[1]), ['asc', 'desc'])) { |
118
|
|
|
$this->orderDirection = $order[1]; |
119
|
|
|
$fields = array(); |
120
|
|
|
$aliases = array(); |
121
|
|
|
foreach (explode('+', $order[0]) as $sortFieldParameterName) { |
122
|
|
|
$parts = explode('.', $sortFieldParameterName, 2); |
123
|
|
|
// We have to prepend the field. Otherwise OrderByWalker will add |
124
|
|
|
// the order-by items in the wrong order |
125
|
|
|
array_unshift($fields, end($parts)); |
126
|
|
|
array_unshift($aliases, 2 <= count($parts) ? reset($parts) : false); |
127
|
|
|
} |
128
|
|
|
$this->orderFields = $fields; |
129
|
|
|
$this->orderAliases = $aliases; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getOrderDirection(): string |
137
|
|
|
{ |
138
|
|
|
return $this->orderDirection; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public function getOrderFields(): array |
145
|
|
|
{ |
146
|
|
|
return $this->orderFields; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public function getOrderAliases(): array |
153
|
|
|
{ |
154
|
|
|
return $this->orderAliases; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param Request $request |
159
|
|
|
*/ |
160
|
4 |
|
public function resolveFromRequest(Request $request) |
161
|
|
|
{ |
162
|
4 |
|
$this->setPageNumber((int) $request->get(self::PAGE_PARAMETER_NAME, 1)); |
163
|
4 |
|
$this->setLimit((int) $request->get(self::LIMIT_PARAMETER_NAME, 10)); |
164
|
4 |
|
} |
165
|
|
|
} |
166
|
|
|
|