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
|
|
|
public function __construct(&$headers, \Transphporm\Hook\Formatter $formatter) { |
14
|
|
|
$this->headers = &$headers; |
15
|
|
|
$this->formatter = $formatter; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
19
|
|
|
if (!$this->shouldRun($element)) return false; |
20
|
|
|
$values = $this->formatter->format($values, $rules); |
21
|
|
|
|
22
|
|
|
if (!$this->processPseudo($values, $element, $pseudoMatcher)) { |
23
|
|
|
//Remove the current contents |
24
|
|
|
$this->removeAllChildren($element); |
25
|
|
|
//Now make a text node |
26
|
|
|
if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $values); |
27
|
|
|
else $this->appendContent($element, $values); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function shouldRun($element) { |
32
|
|
|
do { |
33
|
|
|
if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
34
|
|
|
} |
35
|
|
|
while (($element = $element->parentNode) instanceof \DomElement); |
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function getContentMode($rules) { |
40
|
|
|
return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function processPseudo($value, $element, $pseudoMatcher) { |
44
|
|
|
$pseudoContent = ['attr', 'header', 'before', 'after']; |
45
|
|
|
foreach ($pseudoContent as $pseudo) { |
46
|
|
|
if ($pseudoMatcher->hasFunction($pseudo)) { |
47
|
|
|
$this->$pseudo($value, $pseudoMatcher->getFuncArgs($pseudo, $element)[0], $element); |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function getNode($node, $document) { |
55
|
|
|
foreach ($node as $n) { |
56
|
|
|
if (is_array($n)) { |
57
|
|
|
foreach ($this->getNode($n, $document) as $new) yield $new; |
58
|
|
|
} |
59
|
|
|
else { |
60
|
|
|
if ($n instanceof \DomElement) { |
61
|
|
|
$new = $document->importNode($n, true); |
62
|
|
|
//Removing this might cause problems with caching... |
63
|
|
|
//$new->setAttribute('transphporm', 'added'); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
else { |
66
|
|
|
if ($n instanceof \DomText) $n = $n->nodeValue; |
67
|
|
|
$new = $document->createElement('text'); |
68
|
|
|
|
69
|
|
|
$new->appendChild($document->createTextNode($n)); |
70
|
|
|
$new->setAttribute('transphporm', 'text'); |
71
|
|
|
} |
72
|
|
|
yield $new; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** Functions for writing to pseudo elements, attr, before, after, header */ |
78
|
|
|
private function attr($value, $pseudoArgs, $element) { |
79
|
|
|
$element->setAttribute($pseudoArgs, implode('', $value)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function header($value, $pseudoArgs, $element) { |
|
|
|
|
83
|
|
|
$this->headers[] = [$pseudoArgs, implode('', $value)]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function before($value, $pseudoArgs, $element) { |
|
|
|
|
87
|
|
|
foreach ($this->getNode($value, $element->ownerDocument) as $node) { |
88
|
|
|
$element->insertBefore($node, $element->firstChild); |
89
|
|
|
} |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function after($value, $pseudoArgs, $element) { |
|
|
|
|
94
|
|
|
foreach ($this->getNode($value, $element->ownerDocument) as $node) { |
95
|
|
|
$element->appendChild($node); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function replaceContent($element, $content) { |
100
|
|
|
//If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. |
101
|
|
|
foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
102
|
|
|
$element->parentNode->insertBefore($node, $element); |
103
|
|
|
} |
104
|
|
|
$element->setAttribute('transphporm', 'remove'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function appendContent($element, $content) { |
108
|
|
|
foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
109
|
|
|
$element->appendChild($node); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function removeAllChildren($element) { |
114
|
|
|
while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.