These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Hateoas\Serializer; |
||
4 | |||
5 | use Hateoas\Model\Embedded; |
||
6 | use Hateoas\Util\ClassUtils; |
||
7 | use JMS\Serializer\SerializationContext; |
||
8 | use JMS\Serializer\XmlSerializationVisitor; |
||
9 | use Metadata\MetadataFactoryInterface; |
||
10 | |||
11 | /** |
||
12 | * @author Adrien Brault <[email protected]> |
||
13 | */ |
||
14 | class XmlSerializer implements XmlSerializerInterface, JMSSerializerMetadataAwareInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var MetadataFactoryInterface |
||
18 | */ |
||
19 | private $metadataFactory; |
||
20 | |||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | public function setMetadataFactory(MetadataFactoryInterface $metadataFactory) |
||
25 | { |
||
26 | $this->metadataFactory = $metadataFactory; |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | public function serializeLinks(array $links, XmlSerializationVisitor $visitor, SerializationContext $context) |
||
33 | { |
||
34 | View Code Duplication | foreach ($links as $link) { |
|
0 ignored issues
–
show
|
|||
35 | $linkNode = $visitor->getDocument()->createElement('link'); |
||
36 | $visitor->getCurrentNode()->appendChild($linkNode); |
||
37 | |||
38 | $linkNode->setAttribute('rel', $link->getRel()); |
||
39 | $linkNode->setAttribute('href', $link->getHref()); |
||
40 | |||
41 | foreach ($link->getAttributes() as $attributeName => $attributeValue) { |
||
42 | $linkNode->setAttribute($attributeName, $attributeValue); |
||
43 | } |
||
44 | } |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function serializeEmbeddeds(array $embeddeds, XmlSerializationVisitor $visitor, SerializationContext $context) |
||
51 | { |
||
52 | foreach ($embeddeds as $embedded) { |
||
53 | $entryNode = $visitor->getDocument()->createElement($this->getElementName($embedded->getData(), $embedded)); |
||
54 | |||
55 | $visitor->getCurrentNode()->appendChild($entryNode); |
||
56 | $visitor->setCurrentNode($entryNode); |
||
57 | $visitor->getCurrentNode()->setAttribute('rel', $embedded->getRel()); |
||
58 | |||
59 | if ($embedded->getData() instanceof \Traversable || is_array($embedded->getData())) { |
||
60 | foreach ($embedded->getData() as $entry) { |
||
61 | $entryNode = $visitor->getDocument()->createElement($this->getElementName($entry)); |
||
62 | |||
63 | $visitor->getCurrentNode()->appendChild($entryNode); |
||
64 | $visitor->setCurrentNode($entryNode); |
||
65 | |||
66 | $this->acceptDataAndAppend($embedded, $entry, $visitor, $context); |
||
67 | |||
68 | $visitor->revertCurrentNode(); |
||
69 | } |
||
70 | } else { |
||
71 | $this->acceptDataAndAppend($embedded, $embedded->getData(), $visitor, $context); |
||
72 | } |
||
73 | |||
74 | $visitor->revertCurrentNode(); |
||
75 | } |
||
76 | } |
||
77 | |||
78 | private function getElementName($data, Embedded $embedded = null) |
||
79 | { |
||
80 | $elementName = null; |
||
81 | |||
82 | if (null !== $embedded) { |
||
83 | $elementName = $embedded->getXmlElementName(); |
||
84 | } |
||
85 | |||
86 | if (null == $elementName && is_object($data)) { |
||
87 | $metadata = $this->metadataFactory->getMetadataForClass(ClassUtils::getClass($data)); |
||
88 | $elementName = $metadata->xmlRootName; |
||
89 | } |
||
90 | |||
91 | return $elementName ?: 'entry'; |
||
92 | } |
||
93 | |||
94 | View Code Duplication | private function acceptDataAndAppend(Embedded $embedded, $data, XmlSerializationVisitor $visitor, SerializationContext $context) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
95 | { |
||
96 | $context->pushPropertyMetadata($embedded->getMetadata()); |
||
97 | |||
98 | if (null !== $node = $context->accept($data)) { |
||
99 | $visitor->getCurrentNode()->appendChild($node); |
||
100 | } |
||
101 | |||
102 | $context->popPropertyMetadata(); |
||
103 | } |
||
104 | } |
||
105 |
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.