1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hateoas\Serializer; |
4
|
|
|
|
5
|
|
|
use Hateoas\Model\Embedded; |
6
|
|
|
use Hateoas\Representation\Resource; |
7
|
|
|
use JMS\Serializer\SerializationContext; |
8
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Adrien Brault <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class XmlHalSerializer implements XmlSerializerInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
|
|
public function serializeLinks(array $links, XmlSerializationVisitor $visitor, SerializationContext $context) |
19
|
|
|
{ |
20
|
|
|
foreach ($links as $link) { |
21
|
|
|
if ('self' === $link->getRel()) { |
22
|
|
|
foreach ($link->getAttributes() as $key => $value) { |
23
|
|
|
$visitor->getCurrentNode()->setAttribute($key, $value); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$visitor->getCurrentNode()->setAttribute('href', $link->getHref()); |
27
|
|
|
|
28
|
|
|
continue; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$linkNode = $visitor->getDocument()->createElement('link'); |
32
|
|
|
$visitor->getCurrentNode()->appendChild($linkNode); |
33
|
|
|
|
34
|
|
|
$linkNode->setAttribute('rel', $link->getRel()); |
35
|
|
|
$linkNode->setAttribute('href', $link->getHref()); |
36
|
|
|
|
37
|
|
|
foreach ($link->getAttributes() as $attributeName => $attributeValue) { |
38
|
|
|
$linkNode->setAttribute($attributeName, $attributeValue); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function serializeEmbeddeds(array $embeddeds, XmlSerializationVisitor $visitor, SerializationContext $context) |
47
|
|
|
{ |
48
|
|
|
foreach ($embeddeds as $embedded) { |
49
|
|
|
if ($embedded->getData() instanceof \Traversable || is_array($embedded->getData())) { |
50
|
|
View Code Duplication |
foreach ($embedded->getData() as $data) { |
|
|
|
|
51
|
|
|
$entryNode = $visitor->getDocument()->createElement('resource'); |
52
|
|
|
|
53
|
|
|
$visitor->getCurrentNode()->appendChild($entryNode); |
54
|
|
|
$visitor->setCurrentNode($entryNode); |
55
|
|
|
$visitor->getCurrentNode()->setAttribute('rel', $embedded->getRel()); |
56
|
|
|
|
57
|
|
|
$this->acceptDataAndAppend($embedded, $data, $visitor, $context); |
58
|
|
|
|
59
|
|
|
$visitor->revertCurrentNode(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$entryNode = $visitor->getDocument()->createElement('resource'); |
66
|
|
|
|
67
|
|
|
$visitor->getCurrentNode()->appendChild($entryNode); |
68
|
|
|
$visitor->setCurrentNode($entryNode); |
69
|
|
|
$visitor->getCurrentNode()->setAttribute('rel', $embedded->getRel()); |
70
|
|
|
|
71
|
|
|
$this->acceptDataAndAppend($embedded, $embedded->getData(), $visitor, $context); |
72
|
|
|
|
73
|
|
|
$visitor->revertCurrentNode(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
private function acceptDataAndAppend(Embedded $embedded, $data, XmlSerializationVisitor $visitor, SerializationContext $context) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$context->pushPropertyMetadata($embedded->getMetadata()); |
80
|
|
|
|
81
|
|
|
if (null !== $node = $context->accept($data)) { |
82
|
|
|
$visitor->getCurrentNode()->appendChild($node); |
83
|
|
|
} |
84
|
|
|
$context->popPropertyMetadata(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.