|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) 2013 Janos Szurovecz |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
7
|
|
|
* the Software without restriction, including without limitation the rights to |
|
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
|
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
|
10
|
|
|
* so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
13
|
|
|
* copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21
|
|
|
* SOFTWARE. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace predaddy\presentation; |
|
25
|
|
|
|
|
26
|
|
|
use InvalidArgumentException; |
|
27
|
|
|
use precore\lang\Object; |
|
28
|
|
|
use precore\lang\ObjectInterface; |
|
29
|
|
|
use precore\util\Objects; |
|
30
|
|
|
use precore\util\Preconditions; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Default implementation of Pageable. |
|
34
|
|
|
* The first page number is 0. |
|
35
|
|
|
* |
|
36
|
|
|
* @author Janos Szurovecz <[email protected]> |
|
37
|
|
|
*/ |
|
38
|
|
|
class PageRequest extends Object implements Pageable |
|
39
|
|
|
{ |
|
40
|
|
|
private $page; |
|
41
|
|
|
private $size; |
|
42
|
|
|
private $sort; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param int $page Started from 0 |
|
46
|
|
|
* @param int $size Size of the page |
|
47
|
|
|
* @param Sort $sort |
|
48
|
|
|
* @throws InvalidArgumentException if $page is < 0 |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($page, $size, Sort $sort = null) |
|
51
|
|
|
{ |
|
52
|
|
|
Preconditions::checkArgument(0 <= $page, 'Page must be at least 0'); |
|
53
|
|
|
$this->page = (int) $page; |
|
54
|
|
|
$this->size = (int) $size; |
|
55
|
|
|
$this->sort = $sort; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return Pageable |
|
60
|
|
|
*/ |
|
61
|
|
|
public function first() |
|
62
|
|
|
{ |
|
63
|
|
|
return new PageRequest(0, $this->size, $this->sort); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return int |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getOffset() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->page * $this->size; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getPageNumber() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->page; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return int |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getPageSize() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->size; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return Sort |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getSort() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->sort; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function hasPrevious() |
|
99
|
|
|
{ |
|
100
|
|
|
return 0 < $this->page; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return Pageable |
|
105
|
|
|
*/ |
|
106
|
|
|
public function next() |
|
107
|
|
|
{ |
|
108
|
|
|
return new PageRequest($this->page + 1, $this->size, $this->sort); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Pageable |
|
113
|
|
|
*/ |
|
114
|
|
|
public function previousOrFirst() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->hasPrevious() |
|
117
|
|
|
? new PageRequest($this->page - 1, $this->size, $this->sort) |
|
118
|
|
|
: $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function equals(ObjectInterface $object = null) |
|
122
|
|
|
{ |
|
123
|
|
|
if ($object === $this) { |
|
124
|
|
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
/* @var $object PageRequest */ |
|
127
|
|
|
return $object !== null |
|
128
|
|
|
&& $this->getClassName() === $object->getClassName() |
|
129
|
|
|
&& Objects::equal($this->page, $object->page) |
|
130
|
|
|
&& Objects::equal($this->size, $object->size) |
|
131
|
|
|
&& Objects::equal($this->sort, $object->sort); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function toString() |
|
135
|
|
|
{ |
|
136
|
|
|
return Objects::toStringHelper($this) |
|
137
|
|
|
->add('page', $this->page) |
|
138
|
|
|
->add('size', $this->size) |
|
139
|
|
|
->add('sort', $this->sort) |
|
140
|
|
|
->toString(); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|