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 0.9 */ |
7
|
|
|
namespace Transphporm\Hook; |
8
|
|
|
/* Handles data() and iteration() functions in the CDS */ |
9
|
|
|
class DataFunction { |
10
|
|
|
private $dataStorage; |
11
|
|
|
private $data; |
12
|
|
|
private $locale; |
13
|
|
|
private $baseDir; |
14
|
|
|
|
15
|
|
|
public function __construct(\SplObjectStorage $objectStorage, $data, $locale, $baseDir) { |
16
|
|
|
$this->dataStorage = $objectStorage; |
17
|
|
|
$this->data = $data; |
18
|
|
|
$this->locale = $locale; |
19
|
|
|
$this->baseDir = $baseDir; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** Binds data to an element */ |
23
|
|
|
public function bind(\DomElement $element, $data) { |
24
|
|
|
//This is a bit of a hack to workaround #24, might need a better way of doing this if it causes a problem |
25
|
|
|
if (is_array($data) && count($data) === 1 && isset($data[0]) && is_object($data[0])) $data = $data[0]; |
26
|
|
|
$this->dataStorage[$element] = $data; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function iteration($val, $element) { |
30
|
|
|
$data = $this->getData($element); |
31
|
|
|
$value = $this->traverse($val, $data); |
32
|
|
|
return $value; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** Returns the data that has been bound to $element, or, if no data is bound to $element climb the DOM tree to find the data bound to a parent node*/ |
36
|
|
|
private function getData(\DomElement $element) { |
37
|
|
|
while ($element) { |
38
|
|
|
if (isset($this->dataStorage[$element])) return $this->dataStorage[$element]; |
39
|
|
|
$element = $element->parentNode; |
40
|
|
|
} |
41
|
|
|
return $this->data; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function data($val, $element) { |
45
|
|
|
$data = $this->getData($element); |
46
|
|
|
$value = $this->traverse($val, $data); |
47
|
|
|
return $value; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function traverse($name, $data) { |
51
|
|
|
$name[0] = str_replace(['[', ']'], ['.', ''], $name[0]); |
52
|
|
|
$parts = explode('.', $name[0]); |
53
|
|
|
$obj = $data; |
54
|
|
|
foreach ($parts as $part) { |
55
|
|
|
if ($part === '') continue; |
56
|
|
|
if (is_callable([$obj, $part])) $obj = call_user_func([$obj, $part]); |
57
|
|
|
else $obj = $this->ifNull($obj, $part); |
58
|
|
|
} |
59
|
|
|
return $obj; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function ifNull($obj, $key) { |
63
|
|
|
if (is_array($obj)) return isset($obj[$key]) ? $obj[$key] : null; |
64
|
|
|
else return isset($obj->$key) ? $obj->$key : null; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function attr($val, $element) { |
68
|
|
|
return $element->getAttribute(trim($val[0])); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function templateSubsection($css, $doc, $element) { |
72
|
|
|
$xpathStr = (new \Transphporm\CssToXpath($css))->getXpath(); |
73
|
|
|
$xpath = new \DomXpath($doc); |
74
|
|
|
$nodes = $xpath->query($xpathStr); |
75
|
|
|
$result = []; |
76
|
|
|
|
77
|
|
|
foreach ($nodes as $node) { |
78
|
|
|
$result[] = $element->ownerDocument->importNode($node, true); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $result; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function template($val, $element) { |
85
|
|
|
$newTemplate = new \Transphporm\Builder($this->baseDir . $val[0]); |
86
|
|
|
$newTemplate->setLocale($this->locale); |
87
|
|
|
|
88
|
|
|
$doc = $newTemplate->output([], true)->body; |
89
|
|
|
|
90
|
|
|
if (isset($val[1])) return $this->templateSubsection($val[1], $doc, $element); |
91
|
|
|
|
92
|
|
|
$newNode = $element->ownerDocument->importNode($doc->documentElement, true); |
93
|
|
|
|
94
|
|
|
$result = []; |
95
|
|
|
|
96
|
|
|
if ($newNode->tagName === 'template') { |
97
|
|
|
foreach ($newNode->childNodes as $node) $result[] = $node->cloneNode(true); |
98
|
|
|
} |
99
|
|
|
//else $result[] = $newNode; |
|
|
|
|
100
|
|
|
|
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
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.