1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hateoas\Representation; |
4
|
|
|
|
5
|
|
|
use Hateoas\Configuration\Annotation as Hateoas; |
6
|
|
|
use Hateoas\Configuration\Embedded; |
7
|
|
|
use Hateoas\Configuration\Exclusion; |
8
|
|
|
use Hateoas\Configuration\Metadata\ClassMetadataInterface; |
9
|
|
|
use Hateoas\Configuration\Relation; |
10
|
|
|
use JMS\Serializer\Annotation as Serializer; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @Serializer\ExclusionPolicy("all") |
14
|
|
|
* @Serializer\XmlRoot("collection") |
15
|
|
|
* |
16
|
|
|
* @Hateoas\RelationProvider("getRelations") |
17
|
|
|
* |
18
|
|
|
* @author Adrien Brault <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class CollectionRepresentation |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $rel; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var mixed |
29
|
|
|
*/ |
30
|
|
|
private $resources; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var null|string |
34
|
|
|
*/ |
35
|
|
|
private $xmlElementName; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var null|Exclusion |
39
|
|
|
*/ |
40
|
|
|
private $exclusion; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var null|Exclusion |
44
|
|
|
*/ |
45
|
|
|
private $embedExclusion; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $relations; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $rel |
54
|
|
|
* @param string $xmlElementName |
55
|
|
|
*/ |
56
|
|
|
public function __construct( |
57
|
|
|
$resources, |
58
|
|
|
$rel = null, |
59
|
|
|
$xmlElementName = null, |
60
|
|
|
Exclusion $exclusion = null, |
61
|
|
|
Exclusion $embedExclusion = null, |
62
|
|
|
array $relations = array() |
63
|
|
|
) { |
64
|
|
|
if ($resources instanceof \Traversable) { |
65
|
|
|
$resources = iterator_to_array($resources); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->resources = $resources; |
69
|
|
|
$this->rel = $rel ?: 'items'; |
70
|
|
|
$this->xmlElementName = $xmlElementName; |
71
|
|
|
$this->exclusion = $exclusion; |
72
|
|
|
$this->embedExclusion = $embedExclusion; |
73
|
|
|
$this->relations = $relations; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getRel() |
80
|
|
|
{ |
81
|
|
|
return $this->rel; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $rel |
86
|
|
|
*/ |
87
|
|
|
public function setRel($rel) |
88
|
|
|
{ |
89
|
|
|
$this->rel = $rel; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function getResources() |
96
|
|
|
{ |
97
|
|
|
return $this->resources; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return null|string |
102
|
|
|
*/ |
103
|
|
|
public function getXmlElementName() |
104
|
|
|
{ |
105
|
|
|
return $this->xmlElementName; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param null|string $xmlElementName |
110
|
|
|
*/ |
111
|
|
|
public function setXmlElementName($xmlElementName) |
112
|
|
|
{ |
113
|
|
|
$this->xmlElementName = $xmlElementName; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getRelations($object, ClassMetadataInterface $classMetadata) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
return array_merge(array( |
119
|
|
|
new Relation( |
120
|
|
|
$this->rel, |
121
|
|
|
null, |
122
|
|
|
new Embedded( |
123
|
|
|
'expr(object.getResources())', |
124
|
|
|
$this->xmlElementName, |
125
|
|
|
$this->embedExclusion |
126
|
|
|
), |
127
|
|
|
array(), |
128
|
|
|
$this->exclusion |
129
|
|
|
) |
130
|
|
|
), $this->relations); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.