Completed
Push — master ( 1099f2...b313d2 )
by Tom
10:52 queued 20s
created

DataFunction::template()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 2 Features 0
Metric Value
c 9
b 2
f 0
dl 0
loc 23
rs 8.5906
cc 5
eloc 13
nc 3
nop 2
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;
0 ignored issues
show
Bug introduced by
The property tss does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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) {
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...
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
	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];
49
			$element = $element->parentNode;
50
		}
51
		return $this->data;
52
	}
53
54
	public function data($val, \DomElement $element = null) {
55
		$data = $this->getData($element);
56
		$value = $this->traverse($val, $data, $element);
57
		return $value;
58
	}
59
60
	private function traverse($name, $data, $element) {
61
		$name[0] = str_replace(['[', ']'], ['.', ''], $name[0]);
62
		$parts = explode('.', $name[0]);
63
		$obj = $data;
64
		$valueParser = new \Transphporm\Parser\Value($this);
65
66
		foreach ($parts as $part) {
67
			if ($part === '') continue;
68
			$part = $valueParser->parse($part, $element)[0];
69
			$funcResult = $this->traverseObj($part, $obj, $valueParser, $element);
70
			
71
			if ($funcResult !== false) $obj = $funcResult;
72
			
73
			else $obj = $this->ifNull($obj, $part);
74
		}
75
		return $obj;
76
	}
77
78
	private function traverseObj($part, $obj, $valueParser, $element) {
79
		if (strpos($part, '(') !== false) {
80
			$subObjParser = new \Transphporm\Parser\Value($obj, $valueParser, false);
81
			return $subObjParser->parse($part, $element);
82
		}
83
		else if (method_exists($obj, $part)) return call_user_func([$obj, $part]); 
84
		else return false;
85
	}
86
87
	private function ifNull($obj, $key) {
88
		if (is_array($obj)) return isset($obj[$key]) ? $obj[$key] : null;
89
		else return isset($obj->$key) ? $obj->$key : null;
90
	}
91
92
	public function attr($val, $element) {
93
		return $element->getAttribute(trim($val[0]));
94
	}
95
96
	private function templateSubsection($css, $doc, $element) {
97
		$xpathStr = (new \Transphporm\Parser\CssToXpath($css, new \Transphporm\Parser\Value($this)))->getXpath();
98
		$xpath = new \DomXpath($doc);
99
		$nodes = $xpath->query($xpathStr);
100
		$result = [];
101
102
		foreach ($nodes as $node) {
103
			$result[] = $element->ownerDocument->importNode($node, true);
104
		}
105
106
		return $result;
107
	}
108
109
	public function template($val, $element) {
110
		$newTemplate = new \Transphporm\Builder($this->baseDir . $val[0], $this->tss);
111
		$data = $this->getData($element);
112
113
		$doc = $newTemplate->output($data, true)->body;
114
115
		if (isset($val[1])) return $this->templateSubsection($val[1], $doc, $element);
116
		
117
		$newNode = $element->ownerDocument->importNode($doc->documentElement, true);
118
	//	$this->bind($newNode, $data);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
119
		$result = [];
120
121
		if ($newNode->tagName === 'template') {
122
			foreach ($newNode->childNodes as $node) {
123
				$clone = $node->cloneNode(true);
124
				if ($clone instanceof \DomElement) $clone->setAttribute('transphporm', 'includedtemplate');
125
				$result[] = $clone;
126
			}
127
		}		
128
		//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...
129
130
		return $result;
131
	}
132
}
133