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, \Transphporm\Hook\PropertyHook $rule) { |
21
|
|
|
if ($element->getAttribute('transphporm') === 'remove') return; |
22
|
|
|
|
23
|
|
|
$value = $this->formatter->format($value, $rule->getRules()); |
24
|
|
|
if (!$this->processPseudo($value, $element, $rule)) { |
25
|
|
|
//Remove the current contents |
26
|
|
|
$this->removeAllChildren($element); |
27
|
|
|
//Now make a text node |
28
|
|
|
if ($this->getContentMode($rule->getRules()) === '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, $rule) { |
38
|
|
|
return $this->pseudoAttr($value, $element, $rule) || $this->pseudoHeader($value, $element, $rule) || $this->pseudoBefore($value, $element, $rule) || $this->pseudoAfter($value, $element, $rule); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function pseudoAttr($value, $element, $rule) { |
42
|
|
|
if ($attr = $rule->getPseudoMatcher()->attr()) { |
43
|
|
|
$element->setAttribute($attr, implode('', $value)); |
44
|
|
|
return true; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function pseudoHeader($value, $element, $rule) { |
49
|
|
|
if ($header = $rule->getPseudoMatcher()->header($element)) { |
50
|
|
|
$this->headers[] = [$header, implode('', $value)]; |
51
|
|
|
return true; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function getNode($node, $document) { |
56
|
|
|
foreach ($node as $n) { |
57
|
|
|
if ($n instanceof \DomElement) { |
58
|
|
|
$new = $document->importNode($n, true); |
59
|
|
|
$new->setAttribute('transphporm', 'added'); |
60
|
|
|
} |
61
|
|
|
else { |
62
|
|
|
if ($n instanceof \DomText) $n = $n->nodeValue; |
63
|
|
|
$new = $document->createElement('text'); |
64
|
|
|
$new->appendChild($document->createTextNode($n)); |
65
|
|
|
$new->setAttribute('transphporm', 'text'); |
66
|
|
|
} |
67
|
|
|
yield $new; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
private function pseudoBefore($value, $element, $rule) { |
|
|
|
|
72
|
|
|
if (in_array('before', $rule->getPseudoMatcher()->getPseudo())) { |
73
|
|
|
foreach ($this->getNode($value, $element->ownerDocument) as $node) { |
74
|
|
|
$element->insertBefore($node, $element->firstChild); |
75
|
|
|
} |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
private function pseudoAfter($value, $element, $rule) { |
|
|
|
|
81
|
|
|
if (in_array('after', $rule->getPseudoMatcher()->getPseudo())) { |
82
|
|
|
foreach ($this->getNode($value, $element->ownerDocument) as $node) { |
83
|
|
|
$element->appendChild($node); |
84
|
|
|
} |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function removeAdded($e) { |
90
|
|
|
$remove = []; |
91
|
|
|
while ($e = $e->previousSibling && !in_array($e->getAttribute('transphporm'), [null, 'remove'])) { |
|
|
|
|
92
|
|
|
$remove[] = $e; |
93
|
|
|
} |
94
|
|
|
foreach ($remove as $r) $r->parentNode->removeChild($r); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function replaceContent($element, $content) { |
98
|
|
|
//If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. |
99
|
|
|
$this->removeAdded($element); |
100
|
|
|
foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
101
|
|
|
$element->parentNode->insertBefore($node, $element); |
102
|
|
|
} |
103
|
|
|
$element->setAttribute('transphporm', 'remove'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function appendContent($element, $content) { |
107
|
|
|
foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
108
|
|
|
$element->appendChild($node); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function removeAllChildren($element) { |
113
|
|
|
while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
114
|
|
|
} |
115
|
|
|
} |
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.