Completed
Push — master ( 7b88cf...211030 )
by Richard
02:32
created

ValueData::callFuncOnObject()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 3
eloc 3
nc 2
nop 3
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\Parser;
8
/** Holds the data used by `ValueParser` */
9
class ValueData {
10
	private $data;
11
12
	public function __construct($data) {
13
		$this->data = $data;
14
	}
15
16
	public function traverse($key) {
17
		if (isset($this->data->{$key})) $this->data = $this->data->{$key};
18
		else if (is_array($this->data) && isset($this->data[$key])) $this->data = $this->data[$key];
19
	}
20
21
	public function read($value) {
22
		if (is_array($this->data)) {
23
			if (isset($this->data[$value])) return $this->data[$value];
24
		}
25
		else if (isset($this->data->$value)) return $this->data->$value;
26
		else return false;
27
	}
28
29
	public function call($func, $args) {
30
		return $this->data->$func(...$args);
31
	}
32
33
	public function parseNested($parser, $token, $funcName) {
34
		$args = $parser->parseTokens($token['value'], $this->data);
35
		if ($args[0] == $this->data) $args = [];
36
		return $this->callFuncOnObject($this->data, $funcName, $args);
37
	}
38
39
	private function callFuncOnObject($obj, $func, $args) {
40
		if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args);
41
		else return call_user_func_array([$obj, $func], $args);
42
	}
43
44
	public function extract($last, $autoLookup) {
45
		$value = $this->read($last);
46
		if ($value && ($autoLookup || is_array($this->data)) ) {
47
			return $value;
48
		}
49
		throw new \UnexpectedValueException('Not found');
50
	}
51
}
52