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
|
|
|
/** Parses "string" and function(args) e.g. data(foo) or iteration(bar) */ |
9
|
|
|
class Value { |
10
|
|
|
private $data; |
11
|
|
|
private $autoLookup; |
12
|
|
|
private $tokens; |
|
|
|
|
13
|
|
|
|
14
|
|
|
public function __construct($data, $autoLookup = false) { |
15
|
|
|
$this->data = $data; |
16
|
|
|
$this->autoLookup = $autoLookup; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function parse($str, $element = null, $returnTokens = false) { |
20
|
|
|
$tokenizer = new Tokenizer($str); |
21
|
|
|
$tokens = $tokenizer->getTokens(); |
22
|
|
|
if ($returnTokens) return $tokens; |
23
|
|
|
$result = $this->parseTokens($tokens, $element, $this->data); |
24
|
|
|
return $result; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function parseTokens($tokens, $element, $data) { |
28
|
|
|
$result = []; |
29
|
|
|
$mode = Tokenizer::ARG; |
30
|
|
|
$last = null; |
31
|
|
|
|
32
|
|
|
if (empty($tokens)) return [$data]; |
33
|
|
|
|
34
|
|
|
foreach ($tokens as $token) { |
35
|
|
|
if (is_string($token)) throw new \Exception($token); |
36
|
|
|
|
37
|
|
|
if (in_array($token['type'], [Tokenizer::NOT, Tokenizer::EQUALS])) { |
38
|
|
|
// ($last !== null) $result = $this->processValue($result, $mode, $last); |
|
|
|
|
39
|
|
|
|
40
|
|
|
$result = $this->processLast($last, $result, $mode, $data); |
41
|
|
|
|
42
|
|
|
if ($mode == Tokenizer::NOT && $token['type'] == Tokenizer::EQUALS) { |
43
|
|
|
$mode = Tokenizer::NOT; |
44
|
|
|
} |
45
|
|
|
else $mode = $token['type']; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($token['type'] === Tokenizer::DOT) { |
49
|
|
View Code Duplication |
if ($last !== null) { |
|
|
|
|
50
|
|
|
if (isset($data->$last)) $data = $data->$last; |
51
|
|
|
else if (is_array($data) && isset($data[$last])) $data = $data[$last]; |
52
|
|
|
} |
53
|
|
|
else $data = array_pop($result); |
54
|
|
|
|
55
|
|
|
$last = null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($token['type'] == Tokenizer::OPEN_SQUARE_BRACKET) { |
59
|
|
|
if ($last !== null) { |
60
|
|
|
if (isset($data->$last)) $data = $data->$last; |
61
|
|
|
else if (is_array($data) && isset($data[$last])) $data = $data[$last]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$last = $this->parseTokens($token['value'], $element, null)[0]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (in_array($token['type'], [Tokenizer::ARG, Tokenizer::CONCAT])) { |
68
|
|
|
$mode = $token['type']; |
69
|
|
|
//if ($last !== null) $result = $this->processValue($result, $mode, $last); |
|
|
|
|
70
|
|
|
$result = $this->processLast($last, $result, $mode, $data); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($token['type'] === Tokenizer::STRING) { |
74
|
|
|
$result = $this->processValue($result, $mode, $token['value']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (in_array($token['type'], [Tokenizer::NAME, Tokenizer::NUMERIC, Tokenizer::BOOL])) { |
78
|
|
|
$last = $token['value']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($token['type'] == Tokenizer::OPEN_BRACKET) { |
82
|
|
|
|
83
|
|
|
if ($this->data instanceof \Transphporm\Functionset && ($last == 'data' || $last == 'iteration' || $last == 'attr')) { |
84
|
|
|
$result = $this->processValue($result, $mode, $this->data->$last($token['value'], $element)); |
85
|
|
|
|
86
|
|
|
foreach ($result as $i => $value) { |
87
|
|
View Code Duplication |
if (is_array($data)) { |
|
|
|
|
88
|
|
|
if (isset($data[$value])) $result[$i] = $data[$value]; |
89
|
|
|
} |
90
|
|
|
else if (is_scalar($value) && isset($data->$value)) $result[$i] = $data->$value; |
91
|
|
|
} |
92
|
|
|
$last = null; |
93
|
|
|
} |
94
|
|
|
else if ($data instanceof \Transphporm\Functionset) { |
95
|
|
|
$result = $this->processValue($result, $mode, $data->$last($token['value'], $element)); |
96
|
|
|
$last = null; |
97
|
|
|
} |
98
|
|
|
else { |
99
|
|
|
$args = $this->parseTokens($token['value'], $element, $data); |
100
|
|
|
if ($args[0] == $data) $args = []; |
101
|
|
|
$funcResult = $this->callFunc($last, $args, $element, $data); |
102
|
|
|
$result = $this->processValue($result, $mode, $funcResult); |
103
|
|
|
$last = null; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
/* |
|
|
|
|
107
|
|
|
if ($token['type'] == Tokenizer::OPEN_SQUARE_BRACKET) { |
108
|
|
|
if ($this->autoLookup === true) { |
109
|
|
|
$result = $this->processValue($result, $mode, $data->$last($token['value'], $element)); |
110
|
|
|
$last = null; |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
}//*/ |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->processLast($last, $result, $mode, $data); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function processLast($last, $result, $mode, $data) { |
120
|
|
|
if ($last !== null) { |
121
|
|
|
if ($this->autoLookup && isset($data->$last)) { |
122
|
|
|
$result = $this->processValue($result, $mode, $data->$last); |
123
|
|
|
} |
124
|
|
|
else if (is_array($data) && isset($data[$last])) { |
125
|
|
|
$result = $this->processValue($result, $mode, $data[$last]); |
126
|
|
|
} |
127
|
|
|
else if (!$this->autoLookup) { |
128
|
|
|
$result = $this->processValue($result, $mode, $last); |
129
|
|
|
} |
130
|
|
|
else $result = [false]; |
131
|
|
|
} |
132
|
|
|
return $result; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function processValue($result, $mode, $newValue) { |
136
|
|
|
if ($mode == Tokenizer::ARG) { |
137
|
|
|
$result[] = $newValue; |
138
|
|
|
} |
139
|
|
|
else if ($mode == Tokenizer::CONCAT) { |
140
|
|
|
$result[count($result)-1] .= $newValue; |
141
|
|
|
} |
142
|
|
|
else if ($mode == Tokenizer::NOT) { |
143
|
|
|
$result[count($result)-1] = $result[count($result)-1] != $newValue; |
144
|
|
|
} |
145
|
|
|
else if ($mode == Tokenizer::EQUALS) { |
146
|
|
|
$result[count($result)-1] = $result[count($result)-1] == $newValue; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $result; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function callFunc($name, $args, $element, $data) { |
153
|
|
|
if ($data instanceof \Transphporm\FunctionSet) return $data->$name($args, $element); |
154
|
|
|
else return $this->callFuncOnObject($data, $name, $args, $element); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
private function callFuncOnObject($obj, $func, $args, $element) { |
|
|
|
|
158
|
|
|
if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args); |
159
|
|
|
else if (isset($obj->$func) && is_array($obj->$func)) { |
|
|
|
|
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
else return call_user_func_array([$obj, $func], $args); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.