Completed
Push — master ( cba662...4c587b )
by Tom
02:23
created

DataFunction::processNestedFunc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 4
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) {
15
		$this->dataStorage = $objectStorage;
16
		$this->data = $data;
17
		$this->baseDir = $baseDir;
18
	}
19
20
	/** Binds data to an element */
21
	public function bind(\DomElement $element, $data, $type = 'data') {
22
		//This is a bit of a hack to workaround #24, might need a better way of doing this if it causes a problem
23
		if (is_array($data) && $this->isObjectArray($data)) $data = $data[0];
24
		$content = isset($this->dataStorage[$element]) ? $this->dataStorage[$element] : [];
25
		$content[$type] = $data;
26
		$this->dataStorage[$element] = $content;
27
	}
28
29
	private function isObjectArray(array $data) {
30
		return count($data) === 1 && isset($data[0]) && is_object($data[0]);
31
	}
32
33
	public function iteration($val, $element) {
34
		$data = $this->getData($element, 'iteration');
35
		$value = $this->traverse($val, $data, $element);
36
		return $value;
37
	}
38
39
	public function key($val, $element) {
0 ignored issues
show
Unused Code introduced by
The parameter $val is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
		$data = $this->getData($element, 'key');
41
		return $data;
42
	}
43
44
	/** 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*/
45
	private function getData(\DomElement $element = null, $type = 'data') {
46
		while ($element) {
47
			if (isset($this->dataStorage[$element]) && isset($this->dataStorage[$element][$type])) return $this->dataStorage[$element][$type];
48
			$element = $element->parentNode;
49
		}
50
		return $this->data;
51
	}
52
53
	public function data($val, \DomElement $element = null) {
54
		$data = $this->getData($element);
55
		$value = $this->traverse($val, $data, $element);
56
		return $value;			
57
	}
58
59
	private function traverse($name, $data, $element) {
60
		$name[0] = str_replace(['[', ']'], ['.', ''], $name[0]);
61
		$parts = explode('.', $name[0]);
62
		$obj = $data;
63
		$valueParser = new \Transphporm\Parser\Value($this);
64
65
		foreach ($parts as $part) {
66
			if ($part === '') continue;
67
			$part = $valueParser->parse($part, $element)[0];
68
			$funcResult = $this->traverseObj($part, $obj, $valueParser, $element);
69
			
70
			if ($funcResult !== false) $obj = $funcResult;
71
			
72
			else $obj = $this->ifNull($obj, $part);
73
		}
74
		return $obj;
75
	}
76
77
	private function traverseObj($part, $obj, $valueParser, $element) {
78
		if (strpos($part, '(') !== false) {
79
			$subObjParser = new \Transphporm\Parser\Value($obj, $valueParser, false);
80
			return $subObjParser->parse($part, $element);
81
		}
82
		else if (method_exists($obj, $part)) return call_user_func([$obj, $part]); 
83
		else return false;
84
	}
85
86
	private function ifNull($obj, $key) {
87
		if (is_array($obj)) return isset($obj[$key]) ? $obj[$key] : null;
88
		else return isset($obj->$key) ? $obj->$key : null;
89
	}
90
91
	public function attr($val, $element) {
92
		return $element->getAttribute(trim($val[0]));
93
	}
94
95
	private function templateSubsection($css, $doc, $element) {
96
		$xpathStr = (new \Transphporm\Parser\CssToXpath($css, new \Transphporm\Parser\Value($this)))->getXpath();
97
		$xpath = new \DomXpath($doc);
98
		$nodes = $xpath->query($xpathStr);
99
		$result = [];
100
101
		foreach ($nodes as $node) {
102
			$result[] = $element->ownerDocument->importNode($node, true);
103
		}
104
105
		return $result;
106
	}
107
108
	public function template($val, $element) {
109
		$newTemplate = new \Transphporm\Builder($this->baseDir . $val[0]);
110
111
		$doc = $newTemplate->output([], true)->body;
112
113
		if (isset($val[1])) return $this->templateSubsection($val[1], $doc, $element);
114
		
115
		$newNode = $element->ownerDocument->importNode($doc->documentElement, true);
116
117
		$result = [];
118
119
		if ($newNode->tagName === 'template') {
120
			foreach ($newNode->childNodes as $node) $result[] = $node->cloneNode(true);
121
		}		
122
		//else $result[] = $newNode;
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
123
124
		return $result;
125
	}
126
}
127