1 | <?php |
||
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) { |
||
81 | |||
82 | private function header($value, $pseudoArgs, $element) { |
||
85 | |||
86 | private function before($value, $pseudoArgs, $element) { |
||
92 | |||
93 | private function after($value, $pseudoArgs, $element) { |
||
98 | |||
99 | private function replaceContent($element, $content) { |
||
106 | |||
107 | private function appendContent($element, $content) { |
||
112 | |||
113 | private function removeAllChildren($element) { |
||
116 | } |
||
117 |
This check marks private properties in classes that are never used. Those properties can be removed.