|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hateoas\Representation; |
|
4
|
|
|
|
|
5
|
|
|
use Hateoas\Configuration\Annotation as Hateoas; |
|
6
|
|
|
use JMS\Serializer\Annotation as Serializer; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @Serializer\ExclusionPolicy("all") |
|
10
|
|
|
* |
|
11
|
|
|
* @author Premi Giorgio <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class AbstractSegmentedRepresentation extends RouteAwareRepresentation |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var int |
|
17
|
|
|
* |
|
18
|
|
|
* @Serializer\Expose |
|
19
|
|
|
* @Serializer\Type("integer") |
|
20
|
|
|
* @Serializer\XmlAttribute |
|
21
|
|
|
*/ |
|
22
|
|
|
private $limit; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
* |
|
27
|
|
|
* @Serializer\Expose |
|
28
|
|
|
* @Serializer\Type("integer") |
|
29
|
|
|
* @Serializer\XmlAttribute |
|
30
|
|
|
*/ |
|
31
|
|
|
private $total; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $limitParameterName; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct( |
|
39
|
|
|
$inline, |
|
40
|
|
|
$route, |
|
41
|
|
|
array $parameters = array(), |
|
42
|
|
|
$limit, |
|
43
|
|
|
$total = null, |
|
44
|
|
|
$limitParameterName = null, |
|
45
|
|
|
$absolute = false |
|
46
|
|
|
) { |
|
47
|
|
|
parent::__construct($inline, $route, $parameters, $absolute); |
|
48
|
|
|
|
|
49
|
|
|
$this->total = $total; |
|
50
|
|
|
$this->limit = $limit; |
|
51
|
|
|
$this->limitParameterName = $limitParameterName ?: 'limit'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return int |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getLimit() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->limit; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param null $limit |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getParameters($limit = null) |
|
67
|
|
|
{ |
|
68
|
|
|
$parameters = parent::getParameters(); |
|
69
|
|
|
|
|
70
|
|
|
$parameters[$this->limitParameterName] = null === $limit ? $this->getLimit() : $limit; |
|
71
|
|
|
|
|
72
|
|
|
return $parameters; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return int |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getTotal() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->total; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getLimitParameterName() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->limitParameterName; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $key |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function moveParameterToEnd(array &$parameters, $key) |
|
95
|
|
|
{ |
|
96
|
|
|
if (! array_key_exists($key, $parameters)) { |
|
97
|
|
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$value = $parameters[$key]; |
|
101
|
|
|
unset($parameters[$key]); |
|
102
|
|
|
$parameters[$key] = $value; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|