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
|
|
View Code Duplication |
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 (is_scalar($value) && isset($this->data->$value)) return $this->data->$value; |
26
|
|
|
else return false; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function isFunctionSet() { |
30
|
|
|
return $this->data instanceof \Transphporm\Functioset; |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function call($func, $args) { |
34
|
|
|
return $this->data->$func(...$args); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function parseNested($parser, $token, $funcName) { |
38
|
|
|
$args = $parser->parseTokens($token['value'], $this->data); |
39
|
|
|
if ($args[0] == $this->data) $args = []; |
40
|
|
|
return $this->callFunc($funcName, $args, $this->data); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function callFunc($name, $args, $data) { |
|
|
|
|
44
|
|
|
return $this->callFuncOnObject($this->data, $name, $args); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function callFuncOnObject($obj, $func, $args) { |
48
|
|
|
if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args); |
49
|
|
|
else return call_user_func_array([$obj, $func], $args); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function extract($last, $autoLookup) { |
53
|
|
|
if ($autoLookup && isset($this->data->{$last})) { |
54
|
|
|
return $this->data->{$last}; |
55
|
|
|
} |
56
|
|
View Code Duplication |
else if (is_array($this->data) && isset($this->data[$last])) { |
|
|
|
|
57
|
|
|
return $this->data[$last]; |
58
|
|
|
} |
59
|
|
|
throw new \UnexpectedValueException('Not found'); |
60
|
|
|
} |
61
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.