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, $type = '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) && $this->isObjectArray($data)) $data = $data[0]; |
26
|
|
|
$content = isset($this->dataStorage[$element]) ? $this->dataStorage[$element] : []; |
27
|
|
|
$content[$type] = $data; |
28
|
|
|
$this->dataStorage[$element] = $content; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
private function isObjectArray(array $data) { |
32
|
|
|
return count($data) === 1 && isset($data[0]) && is_object($data[0]); |
33
|
|
|
} |
34
|
|
|
public function iteration($val, $element) { |
35
|
|
|
$data = $this->getData($element, 'iteration'); |
36
|
|
|
$value = $this->traverse($val, $data); |
37
|
|
|
return $value; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function key($val, $element) { |
|
|
|
|
41
|
|
|
$data = $this->getData($element, 'key'); |
42
|
|
|
return $data; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** 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*/ |
46
|
|
|
private function getData(\DomElement $element, $type = 'data') { |
47
|
|
|
while ($element) { |
48
|
|
|
if (isset($this->dataStorage[$element]) && isset($this->dataStorage[$element][$type])) return $this->dataStorage[$element][$type]; |
49
|
|
|
$element = $element->parentNode; |
50
|
|
|
} |
51
|
|
|
return $this->data; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function data($val, $element) { |
55
|
|
|
$data = $this->getData($element); |
56
|
|
|
$value = $this->traverse($val, $data); |
57
|
|
|
return $value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function traverse($name, $data) { |
61
|
|
|
$name[0] = str_replace(['[', ']'], ['.', ''], $name[0]); |
62
|
|
|
$parts = explode('.', $name[0]); |
63
|
|
|
$obj = $data; |
64
|
|
|
foreach ($parts as $part) { |
65
|
|
|
if ($part === '') continue; |
66
|
|
|
if (is_callable([$obj, $part])) $obj = call_user_func([$obj, $part]); |
67
|
|
|
else $obj = $this->ifNull($obj, $part); |
68
|
|
|
} |
69
|
|
|
return $obj; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function ifNull($obj, $key) { |
73
|
|
|
if (is_array($obj)) return isset($obj[$key]) ? $obj[$key] : null; |
74
|
|
|
else return isset($obj->$key) ? $obj->$key : null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function attr($val, $element) { |
78
|
|
|
return $element->getAttribute(trim($val[0])); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function templateSubsection($css, $doc, $element) { |
82
|
|
|
$xpathStr = (new \Transphporm\CssToXpath($css))->getXpath(); |
83
|
|
|
$xpath = new \DomXpath($doc); |
84
|
|
|
$nodes = $xpath->query($xpathStr); |
85
|
|
|
$result = []; |
86
|
|
|
|
87
|
|
|
foreach ($nodes as $node) { |
88
|
|
|
$result[] = $element->ownerDocument->importNode($node, true); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $result; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function template($val, $element) { |
95
|
|
|
$newTemplate = new \Transphporm\Builder($this->baseDir . $val[0]); |
96
|
|
|
$newTemplate->setLocale($this->locale); |
97
|
|
|
|
98
|
|
|
$doc = $newTemplate->output([], true)->body; |
99
|
|
|
|
100
|
|
|
if (isset($val[1])) return $this->templateSubsection($val[1], $doc, $element); |
101
|
|
|
|
102
|
|
|
$newNode = $element->ownerDocument->importNode($doc->documentElement, true); |
103
|
|
|
|
104
|
|
|
$result = []; |
105
|
|
|
|
106
|
|
|
if ($newNode->tagName === 'template') { |
107
|
|
|
foreach ($newNode->childNodes as $node) $result[] = $node->cloneNode(true); |
108
|
|
|
} |
109
|
|
|
//else $result[] = $newNode; |
|
|
|
|
110
|
|
|
|
111
|
|
|
return $result; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.