Completed
Push — master ( be6b9d...0e6890 )
by Tom
03:08
created

Content::processPseudo()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 9.2
cc 4
eloc 2
nc 4
nop 3
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         0.9                                                             */
7
namespace Transphporm\Property;
8
class Content implements \Transphporm\Property {
9
	private $data;
10
	private $headers;
11
	private $formatter; 
12
13
	public function __construct($data, &$headers, \Transphporm\Hook\Formatter $formatter) {
14
		$this->data = $data;
15
		$this->headers = &$headers;
16
		$this->formatter = $formatter;
17
	}
18
19
	public function run($value, \DomElement $element, \Transphporm\Hook\Rule $rule) {
20
		$value = $this->formatter->format($value, $rule->getRules());
21
		if (!$this->processPseudo($value, $element, $rule)) {
22
			//Remove the current contents
23
			$this->removeAllChildren($element);
24
			//Now make a text node
25
			if ($this->getContentMode($rule->getRules()) === 'replace') $this->replaceContent($element, $value);
26
			else $this->appendContent($element, $value);
27
		}
28
	}
29
30
	private function getContentMode($rules) {
31
		return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append';
32
	}
33
34
	private function processPseudo($value, $element, $rule) {
35
		return $this->pseudoAttr($value, $element, $rule) || $this->pseudoHeader($value, $element, $rule) || $this->pseudoBefore($value, $element, $rule) || $this->pseudoAfter($value, $element, $rule);
36
	}
37
38
	private function pseudoAttr($value, $element, $rule) {
39
		if ($attr = $rule->getPseudoMatcher()->attr()) {
40
			$element->setAttribute($attr, implode('', $value));
41
			return true;
42
		}
43
	}
44
45
	private function pseudoHeader($value, $element, $rule) {
46
		if ($header = $rule->getPseudoMatcher()->header($element)) {
47
			$this->headers[] = [$header, implode('', $value)];
48
			return true;
49
		}
50
	}
51
52
	private function pseudoBefore($value, $element, $rule) {
53 View Code Duplication
		if (in_array('before', $rule->getPseudoMatcher()->getPseudo())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
			$element->firstChild->nodeValue = implode('', $value) . $element->firstChild->nodeValue;
55
			return true;
56
		}
57
	}
58
59
	private function pseudoAfter($value, $element, $rule) {
60 View Code Duplication
		 if (in_array('after', $rule->getPseudoMatcher()->getPseudo())) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
		 	$element->firstChild->nodeValue .= implode('', $value);
62
		 	return true;
63
		 }		 
64
	}
65
66
	private function appendToIfNode($element, $content, $appendTo) {
67
		if (isset($content[0]) && $content[0] instanceof \DomNode) {
68
			foreach ($content as $node) {
69
				$node = $element->ownerDocument->importNode($node, true);
70
				$appendTo->appendChild($node);
71
			}
72
			return true;
73
		}
74
		return false;
75
	}
76
77
	private function replaceContent($element, $content) {
78 View Code Duplication
		if (!$this->appendToIfNode($element, $content, $element->parentNode)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
			$element->parentNode->appendChild($element->ownerDocument->createElement('span', implode('', $content)));
80
		}		
81
		$element->setAttribute('transphporm', 'remove');
82
	}
83
84
	private function appendContent($element, $content) {
85 View Code Duplication
		if (!$this->appendToIfNode($element, $content, $element)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
			$element->appendChild($element->ownerDocument->createTextNode(implode('', $content)));
87
		}
88
	}
89
90
	private function removeAllChildren($element) {
91
		while ($element->hasChildNodes()) $element->removeChild($element->firstChild);
92
	}
93
}