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\Hook; |
8
|
|
|
/* Handles data() and iteration() function calls from the stylesheet */ |
9
|
|
|
class DataFunction { |
10
|
|
|
private $dataStorage; |
11
|
|
|
private $data; |
12
|
|
|
private $baseDir; |
13
|
|
|
|
14
|
|
|
public function __construct(\SplObjectStorage $objectStorage, $data, $baseDir, $tss) { |
15
|
|
|
$this->dataStorage = $objectStorage; |
16
|
|
|
$this->data = $data; |
17
|
|
|
$this->baseDir = $baseDir; |
18
|
|
|
$this->tss = $tss; |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setBaseDir($dir) { |
|
|
|
|
22
|
|
|
$this->baseDir = $baseDir; |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** Binds data to an element */ |
26
|
|
|
public function bind(\DomNode $element, $data, $type = 'data') { |
27
|
|
|
//This is a bit of a hack to workaround #24, might need a better way of doing this if it causes a problem |
28
|
|
|
if (is_array($data) && $this->isObjectArray($data)) $data = $data[0]; |
29
|
|
|
$content = isset($this->dataStorage[$element]) ? $this->dataStorage[$element] : []; |
30
|
|
|
$content[$type] = $data; |
31
|
|
|
$this->dataStorage[$element] = $content; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function isObjectArray(array $data) { |
35
|
|
|
return count($data) === 1 && isset($data[0]) && is_object($data[0]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function iteration($val, $element) { |
39
|
|
|
$data = $this->getData($element, 'iteration'); |
40
|
|
|
$value = $this->traverse($val, $data, $element); |
41
|
|
|
return $value; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function key($val, $element) { |
|
|
|
|
45
|
|
|
$data = $this->getData($element, 'key'); |
46
|
|
|
return $data; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** 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*/ |
50
|
|
|
public function getData(\DomElement $element = null, $type = 'data') { |
51
|
|
|
while ($element) { |
52
|
|
|
if (isset($this->dataStorage[$element]) && isset($this->dataStorage[$element][$type])) return $this->dataStorage[$element][$type]; |
53
|
|
|
$element = $element->parentNode; |
54
|
|
|
} |
55
|
|
|
return $this->data; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function data($val, \DomElement $element = null) { |
59
|
|
|
$data = $this->getData($element); |
60
|
|
|
$value = $this->traverse($val, $data, $element); |
61
|
|
|
return $value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function traverse($name, $data, $element) { |
65
|
|
|
$name[0] = str_replace(['[', ']'], ['.', ''], $name[0]); |
66
|
|
|
$parts = explode('.', $name[0]); |
67
|
|
|
$obj = $data; |
68
|
|
|
$valueParser = new \Transphporm\Parser\Value($this); |
69
|
|
|
|
70
|
|
|
foreach ($parts as $part) { |
71
|
|
|
if ($part === '') continue; |
72
|
|
|
$part = $valueParser->parse($part, $element)[0]; |
73
|
|
|
$funcResult = $this->traverseObj($part, $obj, $valueParser, $element); |
74
|
|
|
|
75
|
|
|
if ($funcResult !== false) $obj = $funcResult; |
76
|
|
|
|
77
|
|
|
else $obj = $this->ifNull($obj, $part); |
78
|
|
|
} |
79
|
|
|
return $obj; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function traverseObj($part, $obj, $valueParser, $element) { |
83
|
|
|
if (strpos($part, '(') !== false) { |
84
|
|
|
$subObjParser = new \Transphporm\Parser\Value($obj, $valueParser, false); |
85
|
|
|
return $subObjParser->parse($part, $element)[0]; |
86
|
|
|
} |
87
|
|
|
else if (method_exists($obj, $part)) return call_user_func([$obj, $part]); |
88
|
|
|
else return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function ifNull($obj, $key) { |
92
|
|
|
if (is_array($obj)) return isset($obj[$key]) ? $obj[$key] : null; |
93
|
|
|
else return isset($obj->$key) ? $obj->$key : null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function attr($val, $element) { |
97
|
|
|
return $element->getAttribute(trim($val[0])); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function templateSubsection($css, $doc, $element) { |
101
|
|
|
$xpathStr = (new \Transphporm\Parser\CssToXpath($css, new \Transphporm\Parser\Value($this)))->getXpath(); |
102
|
|
|
$xpath = new \DomXpath($doc); |
103
|
|
|
$nodes = $xpath->query($xpathStr); |
104
|
|
|
$result = []; |
105
|
|
|
|
106
|
|
|
foreach ($nodes as $node) { |
107
|
|
|
$result[] = $element->ownerDocument->importNode($node, true); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function template($val, $element) { |
114
|
|
|
$newTemplate = new \Transphporm\Builder($this->baseDir . $val[0], $this->tss); |
115
|
|
|
$data = $this->getData($element); |
116
|
|
|
|
117
|
|
|
$doc = $newTemplate->output($data, true)->body; |
118
|
|
|
|
119
|
|
|
if (isset($val[1])) return $this->templateSubsection($val[1], $doc, $element); |
120
|
|
|
|
121
|
|
|
$newNode = $element->ownerDocument->importNode($doc->documentElement, true); |
122
|
|
|
$result = []; |
123
|
|
|
|
124
|
|
|
if ($newNode->tagName === 'template') { |
125
|
|
|
foreach ($newNode->childNodes as $node) { |
126
|
|
|
$clone = $node->cloneNode(true); |
127
|
|
|
if ($clone instanceof \DomElement) $clone->setAttribute('transphporm', 'includedtemplate'); |
128
|
|
|
$result[] = $clone; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
//else $result[] = $newNode; |
|
|
|
|
132
|
|
|
|
133
|
|
|
return $result; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: