| 1 |  |  | <?php declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Created by PhpStorm. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * User: alex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Date: 26/10/14 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Time: 00:26 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace FeedIo\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use FeedIo\Feed\NodeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use FeedIo\RuleAbstract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Description extends RuleAbstract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const NODE_NAME = 'description'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param  NodeInterface $node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @param  \DOMElement   $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 12 |  |     public function setProperty(NodeInterface $node, \DOMElement $element) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 12 |  |         $string = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 12 |  |         foreach ($element->childNodes as $childNode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 12 |  |             if ($childNode->nodeType == XML_CDATA_SECTION_NODE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 $string .= $childNode->textContent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 12 |  |                 $string .= $element->ownerDocument->saveXML($childNode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 12 |  |         $node->setDescription($this->processString($string, $node)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param string $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param NodeInterface $node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 12 |  |     protected function processString(string $input, NodeInterface $node): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 12 |  |         return str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 12 |  |             ['href="/', 'src="/'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 12 |  |             ["href=\"{$node->getHost()}/", "src=\"{$node->getHost()}/"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 12 |  |             htmlspecialchars_decode($input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @inheritDoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 5 |  |     protected function hasValue(NodeInterface $node) : bool | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 5 |  |         return !! $node->getDescription(); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @inheritDoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 5 |  |     protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 5 |  |         $description = htmlspecialchars($node->getDescription()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 5 |  |         $element = $document->createElement($this->getNodeName(), $description); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 5 |  |         if ($description !== $node->getDescription() && $this->getNodeName() != 'description') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $element->setAttribute('type', 'html'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 5 |  |         $rootElement->appendChild($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 5 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  |