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\TSSFunction; |
8
|
|
|
/* Handles data() and iteration() function calls from the stylesheet */ |
9
|
|
|
class Data implements \Transphporm\TSSFunction{ |
10
|
|
|
private $data; |
11
|
|
|
private $dataType; |
|
|
|
|
12
|
|
|
private $functionSet; |
13
|
|
|
|
14
|
|
|
public function __construct(\Transphporm\Hook\ElementData $data, \Transphporm\FunctionSet $functionSet, $dataKey = 'data') { |
15
|
|
|
$this->data = $data; |
16
|
|
|
$this->dataKey = $dataKey; |
|
|
|
|
17
|
|
|
$this->functionSet = $functionSet; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
private function traverse($name, $data, $element) { |
21
|
|
|
$name = str_replace(['[', ']'], ['.', ''], $name); |
22
|
|
|
$parts = explode('.', $name); |
23
|
|
|
$obj = $data; |
24
|
|
|
|
25
|
|
|
$valueParser = new \Transphporm\Parser\Value($this->functionSet); |
26
|
|
|
|
27
|
|
|
foreach ($parts as $part) { |
28
|
|
|
if ($part === '') continue; |
29
|
|
|
$part = $valueParser->parse($part, $element)[0]; |
30
|
|
|
|
31
|
|
|
$funcResult = $this->traverseObj($part, $obj, $valueParser, $element); |
32
|
|
|
|
33
|
|
|
if ($funcResult !== false) $obj = $funcResult; |
34
|
|
|
|
35
|
|
|
else $obj = $this->ifNull($obj, $part); |
36
|
|
|
} |
37
|
|
|
return $obj; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function traverseObj($part, $obj, $valueParser, $element) { |
41
|
|
|
if (strpos($part, '(') !== false) { |
42
|
|
|
$subObjParser = new \Transphporm\Parser\Value($obj, $valueParser, false); |
43
|
|
|
return $subObjParser->parse($part, $element)[0]; |
44
|
|
|
} |
45
|
|
|
else if (method_exists($obj, $part)) return call_user_func([$obj, $part]); |
46
|
|
|
else return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function ifNull($obj, $key) { |
50
|
|
|
if (is_array($obj)) return isset($obj[$key]) ? $obj[$key] : null; |
51
|
|
|
else return isset($obj->$key) ? $obj->$key : null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
public function run(array $args, \DomElement $element) { |
56
|
|
|
$data = $this->data->getData($element, $this->dataKey); |
57
|
|
|
$value = $this->traverse($args[0], $data, $element); |
58
|
|
|
return $value; |
59
|
|
|
} |
60
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.