1 | <?php |
||
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) { |
||
19 | |||
20 | public function run($value, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
||
21 | if ($this->isIncludedTemplate($element)) return false; |
||
22 | if ($element->getAttribute('transphporm') === 'remove') return false; |
||
23 | |||
24 | $value = $this->formatter->format($value, $rules); |
||
25 | if (!$this->processPseudo($value, $element, $pseudoMatcher)) { |
||
26 | //Remove the current contents |
||
27 | $this->removeAllChildren($element); |
||
28 | //Now make a text node |
||
29 | if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $value); |
||
30 | else $this->appendContent($element, $value); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | private function isIncludedTemplate($element) { |
||
35 | do { |
||
36 | if ($element->getAttribute('transphporm') == 'includedtemplate') return true; |
||
37 | } |
||
38 | while (($element = $element->parentNode) instanceof \DomElement); |
||
39 | return false; |
||
40 | } |
||
41 | |||
42 | private function getContentMode($rules) { |
||
43 | return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append'; |
||
44 | } |
||
45 | |||
46 | private function processPseudo($value, $element, $pseudoMatcher) { |
||
47 | $pseudoContent = ['attr', 'header', 'before', 'after']; |
||
48 | foreach ($pseudoContent as $pseudo) { |
||
49 | if ($pseudoMatcher->hasFunction($pseudo)) { |
||
50 | $this->$pseudo($value, $pseudoMatcher->getFuncArgs($pseudo), $element); |
||
51 | return true; |
||
52 | } |
||
53 | } |
||
54 | return false; |
||
55 | } |
||
56 | |||
57 | private function getNode($node, $document) { |
||
58 | foreach ($node as $n) { |
||
59 | if ($n instanceof \DomElement) { |
||
60 | $new = $document->importNode($n, true); |
||
61 | //Removing this might cause problems with caching... |
||
62 | //$new->setAttribute('transphporm', 'added'); |
||
|
|||
63 | } |
||
64 | else { |
||
65 | if ($n instanceof \DomText) $n = $n->nodeValue; |
||
66 | $new = $document->createElement('text'); |
||
67 | $new->appendChild($document->createTextNode($n)); |
||
68 | $new->setAttribute('transphporm', 'text'); |
||
69 | } |
||
70 | yield $new; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | /** Functions for writing to pseudo elements, attr, before, after, header */ |
||
75 | private function attr($value, $pseudoArgs, $element) { |
||
78 | |||
79 | private function header($value, $pseudoArgs, $element) { |
||
82 | |||
83 | private function before($value, $pseudoArgs, $element) { |
||
89 | |||
90 | private function after($value, $pseudoArgs, $element) { |
||
95 | |||
96 | private function removeAdded($e) { |
||
103 | |||
104 | private function replaceContent($element, $content) { |
||
112 | |||
113 | private function appendContent($element, $content) { |
||
118 | |||
119 | private function removeAllChildren($element) { |
||
122 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.