Completed
Push — master ( 653716...26bcfc )
by Tom
02:35
created

DataFunction::setBaseDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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
	public function setBaseDir($dir) {
0 ignored issues
show
Unused Code introduced by
The parameter $dir 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...
22
		$this->baseDir = $baseDir;
0 ignored issues
show
Bug introduced by
The variable $baseDir does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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) {
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...
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;
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...
132
133
		return $result;
134
	}
135
}
136