|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stratadox\CardGame\Infrastructure\Rest\Serializer; |
|
6
|
|
|
|
|
7
|
|
|
use DOMNode; |
|
8
|
|
|
use DOMNodeList; |
|
9
|
|
|
use Hateoas\Model\Embedded; |
|
10
|
|
|
use Hateoas\Serializer\SerializerInterface; |
|
11
|
|
|
use JMS\Serializer\SerializationContext; |
|
12
|
|
|
use JMS\Serializer\Visitor\SerializationVisitorInterface; |
|
13
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
|
14
|
|
|
use LogicException; |
|
15
|
|
|
use RuntimeException; |
|
16
|
|
|
|
|
17
|
|
|
class LinksFirstXmlHalSerializer implements SerializerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var SerializerInterface */ |
|
20
|
|
|
private $embeddedSerializer; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(SerializerInterface $embeddedSerializer) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->embeddedSerializer = $embeddedSerializer; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function serializeLinks( |
|
28
|
|
|
array $links, |
|
29
|
|
|
SerializationVisitorInterface $visitor, |
|
30
|
|
|
SerializationContext $context |
|
31
|
|
|
): void { |
|
32
|
|
|
if (!$visitor instanceof XmlSerializationVisitor) { |
|
33
|
|
|
throw new LogicException( |
|
34
|
|
|
'XML Serializers can be visited only by Xml Visitors' |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
/** @var DOMNode $node */ |
|
38
|
|
|
$node = $visitor->getCurrentNode(); |
|
39
|
|
|
if (null === $node) { |
|
40
|
|
|
throw new RuntimeException('Nothing to serialize!'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
foreach ($links as $link) { |
|
44
|
|
|
if ('self' === $link->getRel()) { |
|
45
|
|
|
foreach ($link->getAttributes() as $key => $value) { |
|
46
|
|
|
$node->setAttribute($key, $value); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$node->setAttribute('href', $link->getHref()); |
|
50
|
|
|
|
|
51
|
|
|
continue; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$linkNode = $visitor->getDocument()->createElement('link'); |
|
55
|
|
|
$node->insertBefore($linkNode, $this->firstNonLinkChild($node->childNodes)); |
|
56
|
|
|
|
|
57
|
|
|
$linkNode->setAttribute('rel', $link->getRel()); |
|
58
|
|
|
$linkNode->setAttribute('href', $link->getHref()); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($link->getAttributes() as $attributeName => $attributeValue) { |
|
61
|
|
|
$linkNode->setAttribute($attributeName, $attributeValue); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param Embedded[] $embeddeds |
|
68
|
|
|
*/ |
|
69
|
|
|
public function serializeEmbeddeds(array $embeddeds, SerializationVisitorInterface $visitor, SerializationContext $context): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->embeddedSerializer->serializeEmbeddeds($embeddeds, $visitor, $context); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function firstNonLinkChild(DOMNodeList $nodes): ?DOMNode |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var DOMNode $node */ |
|
77
|
|
|
foreach ($nodes as $node) { |
|
78
|
|
|
if ($node->nodeName !== 'link') { |
|
79
|
|
|
return $node; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|