| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* @description     Transformation Style Sheets - Revolutionising PHP templating    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author          Tom Butler [email protected]                                             * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright       2015 Tom Butler <[email protected]> | https://r.je/                      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @version         1.0                                                             */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Transphporm\Property; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Content implements \Transphporm\Property { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	private $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	private $headers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	private $formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	public function __construct($data, &$headers, \Transphporm\Hook\Formatter $formatter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 		$this->data = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		$this->headers = &$headers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 		$this->formatter = $formatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	public function run($value, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		if ($element->getAttribute('transphporm') === 'remove') return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		$value = $this->formatter->format($value, $rules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		if (!$this->processPseudo($value, $element, $pseudoMatcher)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			//Remove the current contents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 			$this->removeAllChildren($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			//Now make a text node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			else $this->appendContent($element, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	private function getContentMode($rules) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	private function processPseudo($value, $element, $pseudoMatcher) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$pseudoContent = ['attr', 'header', 'before', 'after']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		foreach ($pseudoContent as $pseudo) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			if ($pseudoMatcher->hasFunction($pseudo)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 				$this->$pseudo($value, $pseudoMatcher->getFuncArgs($pseudo), $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 				return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	private function getNode($node, $document) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		foreach ($node as $n) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			if ($n instanceof \DomElement) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 				$new = $document->importNode($n, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 				$new->setAttribute('transphporm', 'added'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 				if ($n instanceof \DomText) $n = $n->nodeValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 				$new = $document->createElement('text'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 				$new->appendChild($document->createTextNode($n)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 				$new->setAttribute('transphporm', 'text'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			yield $new; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	/** Functions for writing to pseudo elements, attr, before, after, header */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	private function attr($value, $pseudoArgs, $element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$element->setAttribute($pseudoArgs, implode('', $value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	private function header($value, $pseudoArgs, $element) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$this->headers[] = [$pseudoArgs, implode('', $value)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 	private function before($value, $pseudoArgs, $element) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 		foreach ($this->getNode($value, $element->ownerDocument) as $node) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 			$element->insertBefore($node, $element->firstChild);	 | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 		return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	private function after($value, $pseudoArgs, $element) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		 foreach ($this->getNode($value, $element->ownerDocument) as $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		 		$element->appendChild($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		}			  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	private function removeAdded($e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		$remove = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		while ($e = $e->previousSibling && !in_array($e->getAttribute('transphporm'), [null, 'remove'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			$remove[] = $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		foreach ($remove as $r) $r->parentNode->removeChild($r); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	private function replaceContent($element, $content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		//If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		$this->removeAdded($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		foreach ($this->getNode($content, $element->ownerDocument) as $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			$element->parentNode->insertBefore($node, $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		}		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		$element->setAttribute('transphporm', 'remove'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	private function appendContent($element, $content) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		foreach ($this->getNode($content, $element->ownerDocument) as $node) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			$element->appendChild($node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	private function removeAllChildren($element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		while ($element->hasChildNodes()) $element->removeChild($element->firstChild); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 112 |  |  | } | 
            
                        
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.