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
|
|
|
//var_dump($value); |
44
|
|
|
$element->setAttribute($attr, implode('', $value)); |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function pseudoHeader($value, $element, $rule) { |
50
|
|
|
if ($header = $rule->getPseudoMatcher()->header($element)) { |
51
|
|
|
$this->headers[] = [$header, implode('', $value)]; |
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function getNode($node, $document) { |
57
|
|
|
foreach ($node as $n) { |
58
|
|
|
if ($n instanceof \DomNode) yield $document->importNode($n, true); |
59
|
|
|
else yield $document->createTextNode($n); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
private function pseudoBefore($value, $element, $rule) { |
|
|
|
|
64
|
|
|
if (in_array('before', $rule->getPseudoMatcher()->getPseudo())) { |
65
|
|
|
foreach ($this->getNode($value, $element->ownerDocument) as $node) { |
66
|
|
|
$element->insertBefore($node, $element->firstChild); |
67
|
|
|
} |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
private function pseudoAfter($value, $element, $rule) { |
|
|
|
|
73
|
|
|
if (in_array('after', $rule->getPseudoMatcher()->getPseudo())) { |
74
|
|
|
foreach ($this->getNode($value, $element->ownerDocument) as $node) { |
75
|
|
|
$element->appendChild($node); |
76
|
|
|
} |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function appendToIfNode($element, $content, $appendTo) { |
82
|
|
|
if (isset($content[0]) && $content[0] instanceof \DomNode) { |
83
|
|
|
foreach ($content as $node) { |
84
|
|
|
$node = $element->ownerDocument->importNode($node, true); |
85
|
|
|
$appendTo->appendChild($node); |
86
|
|
|
} |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function replaceContent($element, $content) { |
93
|
|
View Code Duplication |
if (!$this->appendToIfNode($element, $content, $element->parentNode)) { |
|
|
|
|
94
|
|
|
$element->parentNode->appendChild($element->ownerDocument->createElement('span', implode('', $content))); |
95
|
|
|
} |
96
|
|
|
$element->setAttribute('transphporm', 'remove'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function appendContent($element, $content) { |
100
|
|
View Code Duplication |
if (!$this->appendToIfNode($element, $content, $element)) { |
|
|
|
|
101
|
|
|
$element->appendChild($element->ownerDocument->createTextNode(implode('', $content))); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function removeAllChildren($element) { |
106
|
|
|
while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
107
|
|
|
} |
108
|
|
|
} |
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.