1 | <?php |
||
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 | /** Binds data to an element */ |
||
22 | public function bind(\DomNode $element, $data, $type = 'data') { |
||
23 | //This is a bit of a hack to workaround #24, might need a better way of doing this if it causes a problem |
||
24 | if (is_array($data) && $this->isObjectArray($data)) $data = $data[0]; |
||
25 | $content = isset($this->dataStorage[$element]) ? $this->dataStorage[$element] : []; |
||
26 | $content[$type] = $data; |
||
27 | $this->dataStorage[$element] = $content; |
||
28 | } |
||
29 | |||
30 | private function isObjectArray(array $data) { |
||
31 | return count($data) === 1 && isset($data[0]) && is_object($data[0]); |
||
32 | } |
||
33 | |||
34 | public function iteration($val, $element) { |
||
35 | $data = $this->getData($element, 'iteration'); |
||
36 | $value = $this->traverse($val, $data, $element); |
||
37 | return $value; |
||
38 | } |
||
39 | |||
40 | public function key($val, $element) { |
||
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 | public function getData(\DomElement $element = null, $type = 'data') { |
||
47 | while ($element) { |
||
48 | if (isset($this->dataStorage[$element]) && isset($this->dataStorage[$element][$type])) return $this->dataStorage[$element][$type]; |
||
53 | |||
54 | public function data($val, \DomElement $element = null) { |
||
59 | |||
60 | private function traverse($name, $data, $element) { |
||
77 | |||
78 | private function traverseObj($part, $obj, $valueParser, $element) { |
||
86 | |||
87 | private function ifNull($obj, $key) { |
||
91 | |||
92 | public function attr($val, $element) { |
||
95 | |||
96 | private function templateSubsection($css, $doc, $element) { |
||
108 | |||
109 | public function template($val, $element) { |
||
132 | } |
||
133 |
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: