1 | <?php |
||
10 | final class XmlSerializer implements SerializerInterface |
||
11 | { |
||
12 | /** |
||
13 | * <shortcode name="NAME"> |
||
14 | * <bbCode>BBCODE</bbCode> |
||
15 | * <parameters> |
||
16 | * <parameter name="KEY">VALUE</parameter> |
||
17 | * <parameter name="KEY">VALUE</parameter> |
||
18 | * </parameters> |
||
19 | * <content>CONTENT></content> |
||
20 | * </shortcode> |
||
21 | * |
||
22 | * @param ShortcodeInterface $shortcode |
||
23 | * |
||
24 | * @return string |
||
25 | */ |
||
26 | 8 | public function serialize(ShortcodeInterface $shortcode) |
|
52 | |||
53 | /** |
||
54 | * @param \DOMDocument $doc |
||
55 | * @param string $name |
||
56 | * @param string|null $content |
||
57 | * |
||
58 | * @return \DOMElement |
||
59 | */ |
||
60 | 8 | private function createCDataNode(\DOMDocument $doc, $name, $content) |
|
61 | { |
||
62 | 8 | $node = $doc->createElement($name); |
|
63 | |||
64 | 8 | if(null !== $content) { |
|
65 | 2 | $node->appendChild($doc->createCDATASection($content)); |
|
66 | 2 | } |
|
67 | |||
68 | 8 | return $node; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @param string $text |
||
73 | * |
||
74 | * @return Shortcode |
||
75 | */ |
||
76 | 18 | public function unserialize($text) |
|
77 | { |
||
78 | 18 | $xml = new \DOMDocument(); |
|
79 | 18 | $internalErrors = libxml_use_internal_errors(true); |
|
80 | 18 | if(!$text || !$xml->loadXML($text)) { |
|
81 | 2 | libxml_use_internal_errors($internalErrors); |
|
82 | 2 | throw new \InvalidArgumentException('Failed to parse provided XML!'); |
|
83 | } |
||
84 | 16 | libxml_use_internal_errors($internalErrors); |
|
85 | |||
86 | 16 | $xpath = new \DOMXPath($xml); |
|
87 | 16 | $shortcode = $xpath->query('/shortcode'); |
|
88 | 16 | if($shortcode->length !== 1) { |
|
89 | 1 | throw new \InvalidArgumentException('Invalid shortcode XML!'); |
|
90 | } |
||
91 | 15 | $name = $this->getAttribute($shortcode->item(0), 'name'); |
|
92 | |||
93 | 13 | $bbCode = $this->getValue($xpath->query('/shortcode/bbCode')); |
|
94 | 13 | $content = $this->getValue($xpath->query('/shortcode/content')); |
|
95 | |||
96 | 13 | $parameters = array(); |
|
97 | 13 | $elements = $xpath->query('/shortcode/parameters/parameter'); |
|
98 | 13 | for($i = 0; $i < $elements->length; $i++) { |
|
99 | 7 | $node = $elements->item($i); |
|
100 | |||
101 | 7 | $parameters[$this->getAttribute($node, 'name')] = $node->hasChildNodes() ? $node->nodeValue : null; |
|
102 | 5 | } |
|
103 | |||
104 | 11 | return new Shortcode($name, $parameters, $content, $bbCode); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param \DOMNodeList $node |
||
109 | * |
||
110 | * @return string|null |
||
111 | */ |
||
112 | 13 | private function getValue(\DOMNodeList $node) |
|
118 | |||
119 | /** |
||
120 | * @param \DOMNode $node |
||
121 | * @param string $name |
||
122 | * @psalm-suppress UnusedParam |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 15 | private function getAttribute(\DOMNode $node, $name) |
|
141 | } |
||
142 |