| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Thunder\Shortcode\Serializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Thunder\Shortcode\Shortcode\Shortcode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Thunder\Shortcode\Shortcode\ShortcodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Tomasz Kowalczyk <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 8 |  |         $doc = new \DOMDocument('1.0', 'UTF-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 8 |  |         $doc->preserveWhiteSpace = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 8 |  |         $doc->formatOutput = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 8 |  |         $code = $doc->createElement('shortcode'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 8 |  |         $code->setAttribute('name', $shortcode->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 8 |  |         $xml = $doc->appendChild($code); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 8 |  |         $xml->appendChild($this->createCDataNode($doc, 'bbCode', $shortcode->getBbCode())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 8 |  |         $parameters = $xml->appendChild($doc->createElement('parameters')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 8 |  |         foreach($shortcode->getParameters() as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 4 |  |             $parameter = $doc->createElement('parameter'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 4 |  |             $parameter->setAttribute('name', $key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 4 |  |             if(null !== $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |                 $parameter->appendChild($doc->createCDATASection($value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 4 |  |             $parameters->appendChild($parameter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 8 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 8 |  |         $xml->appendChild($this->createCDataNode($doc, 'content', $shortcode->getContent())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 8 |  |         return $doc->saveXML(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 13 |  |         return $node->length === 1 && $node->item(0)->hasChildNodes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 13 |  |             ? $node->item(0)->nodeValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 13 |  |             : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |         /** | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |          * @var \DOMNode $attribute | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |          * @psalm-suppress NullReference | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |          */ | 
            
                                                        
            
                                    
            
            
                | 132 | 15 |  |         $attribute = $node->attributes->getNamedItem($name); | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |         /** @psalm-suppress DocblockTypeContradiction */ | 
            
                                                        
            
                                    
            
            
                | 135 | 15 |  |         if(!$attribute || !$attribute->nodeValue) { | 
            
                                                        
            
                                    
            
            
                | 136 | 4 |  |             throw new \InvalidArgumentException('Invalid shortcode XML!'); | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 139 | 13 |  |         return $attribute->nodeValue; | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 141 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  |