Completed
Push — master ( 247d83...132dc2 )
by Tom
02:31
created

Data::ifNull()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 9.2
cc 4
eloc 3
nc 4
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\TSSFunction;
8
/* Handles data() and iteration() function calls from the stylesheet */
9
class Data implements \Transphporm\TSSFunction{
10
	private $data;
11
	private $dataType;
0 ignored issues
show
Unused Code introduced by
The property $dataType is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The property dataKey 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...
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
}