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 $baseData; |
11
|
|
|
private $autoLookup; |
12
|
|
|
private $tokens; |
|
|
|
|
13
|
|
|
private $mode; |
14
|
|
|
private $last; |
15
|
|
|
private $data; |
16
|
|
|
private $result; |
17
|
|
|
private $element; |
18
|
|
|
|
19
|
|
|
public function __construct($data, $autoLookup = false) { |
20
|
|
|
$this->baseData = $data; |
21
|
|
|
$this->autoLookup = $autoLookup; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function parse($str, $element = null, $returnTokens = false) { |
25
|
|
|
$tokenizer = new Tokenizer($str); |
26
|
|
|
$tokens = $tokenizer->getTokens(); |
27
|
|
|
if ($returnTokens) return $tokens; |
28
|
|
|
$this->result = $this->parseTokens($tokens, $element, $this->baseData); |
29
|
|
|
return $this->result; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function parseTokens($tokens, $element, $data) { |
33
|
|
|
$this->result = []; |
34
|
|
|
$this->mode = Tokenizer::ARG; |
35
|
|
|
$this->data = $data; |
36
|
|
|
$this->last = null; |
37
|
|
|
$this->element = $element; |
38
|
|
|
|
39
|
|
|
if (empty($tokens)) return [$this->data]; |
40
|
|
|
|
41
|
|
|
$tokenFuncs = [ |
42
|
|
|
Tokenizer::NOT => 'processComparator', |
43
|
|
|
Tokenizer::EQUALS => 'processComparator', |
44
|
|
|
Tokenizer::DOT => 'processDot', |
45
|
|
|
Tokenizer::OPEN_SQUARE_BRACKET => 'processSquareBracket', |
46
|
|
|
Tokenizer::ARG => 'processSeparator', |
47
|
|
|
Tokenizer::CONCAT => 'processSeparator', |
48
|
|
|
Tokenizer::NAME => 'processScalar', |
49
|
|
|
Tokenizer::NUMERIC => 'processScalar', |
50
|
|
|
Tokenizer::BOOL => 'processScalar', |
51
|
|
|
Tokenizer::STRING => 'processString', |
52
|
|
|
Tokenizer::OPEN_BRACKET => 'processBrackets' |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
foreach ($tokens as $token) { |
56
|
|
|
$this->{$tokenFuncs[$token['type']]}($token); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->processLast(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function processComparator($token) { |
63
|
|
|
$this->result = $this->processLast(); |
64
|
|
|
|
65
|
|
|
if ($this->mode == Tokenizer::NOT && $token['type'] == Tokenizer::EQUALS) { |
66
|
|
|
$this->mode = Tokenizer::NOT; |
67
|
|
|
} |
68
|
|
|
else $this->mode = $token['type']; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
//Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value |
73
|
|
|
private function moveLastToData() { |
74
|
|
|
if (isset($this->data->{$this->last})) $this->data = $this->data->{$this->last}; |
75
|
|
View Code Duplication |
else if (is_array($this->data) && isset($this->data[$this->last])) $this->data = $this->data[$this->last]; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
//Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar` |
79
|
|
|
private function processDot($token) { |
|
|
|
|
80
|
|
|
if ($this->last !== null) $this->moveLastToData(); |
81
|
|
|
else $this->data = array_pop($this->result); |
82
|
|
|
|
83
|
|
|
$this->last = null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function processSquareBracket($token) { |
87
|
|
|
if ($this->last !== null) $this->moveLastToData(); |
88
|
|
|
|
89
|
|
|
$parser = new Value($this->baseData, $this->autoLookup); |
90
|
|
|
$this->last = $parser->parseTokens($token['value'], $this->element, null)[0]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function processSeparator($token) { |
94
|
|
|
$this->mode = $token['type']; |
95
|
|
|
//if ($this->last !== null) $this->result = $this->processValue($this->result, $this->mode, $this->last); |
|
|
|
|
96
|
|
|
$this->result = $this->processLast(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function processScalar($token) { |
100
|
|
|
$this->last = $token['value']; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function processString($token) { |
104
|
|
|
$this->result = $this->processValue($token['value']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function processBrackets($token) { |
108
|
|
|
if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) { |
109
|
|
|
$this->callTransphpormFunctions($token); |
110
|
|
|
} |
111
|
|
|
else if ($this->data instanceof \Transphporm\Functionset) { |
112
|
|
|
$this->result = $this->processValue($this->data->{$this->last}($token['value'], $this->element)); |
113
|
|
|
$this->last = null; |
114
|
|
|
} |
115
|
|
|
else { |
116
|
|
|
$parser = new Value($this->baseData, $this->autoLookup); |
117
|
|
|
$args = $parser->parseTokens($token['value'], $this->element, $this->data); |
118
|
|
|
if ($args[0] == $this->data) $args = []; |
119
|
|
|
$funcResult = $this->callFunc($this->last, $args, $this->element, $this->data); |
120
|
|
|
$this->result = $this->processValue($funcResult); |
121
|
|
|
$this->last = null; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function callTransphpormFunctions($token) { |
126
|
|
|
$this->result = $this->processValue($this->baseData->{$this->last}($token['value'], $this->element)); |
127
|
|
|
foreach ($this->result as $i => $value) { |
128
|
|
|
if (is_array($this->data)) { |
129
|
|
|
if (isset($this->data[$value])) $this->result[$i] = $this->data[$value]; |
130
|
|
|
} |
131
|
|
|
else if (is_scalar($value) && isset($this->data->$value)) $this->result[$i] = $this->data->$value; |
132
|
|
|
} |
133
|
|
|
$this->last = null; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
//Processes the last entry down an object graph using foo.bar.baz and adds it to the result |
137
|
|
|
private function processLast() { |
138
|
|
|
if ($this->last !== null) { |
139
|
|
|
try { |
140
|
|
|
$this->result = $this->extractLast($this->result); |
141
|
|
|
} |
142
|
|
|
catch (\UnexpectedValueException $e) { |
143
|
|
|
if (!$this->autoLookup) { |
144
|
|
|
$this->result = $this->processValue($this->last); |
145
|
|
|
} |
146
|
|
|
else $this->result = [false]; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
return $this->result; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function extractLast($result) { |
|
|
|
|
153
|
|
|
if ($this->autoLookup && isset($this->data->{$this->last})) { |
154
|
|
|
return $this->processValue($this->data->{$this->last}); |
155
|
|
|
} |
156
|
|
View Code Duplication |
else if (is_array($this->data) && isset($this->data[$this->last])) { |
|
|
|
|
157
|
|
|
return $this->processValue($this->data[$this->last]); |
158
|
|
|
} |
159
|
|
|
throw new \UnexpectedValueException('Not found'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
private function processValue($newValue) { |
163
|
|
|
if ($this->mode == Tokenizer::ARG) { |
164
|
|
|
$this->result[] = $newValue; |
165
|
|
|
} |
166
|
|
|
else if ($this->mode == Tokenizer::CONCAT) { |
167
|
|
|
$this->result[count($this->result)-1] .= $newValue; |
168
|
|
|
} |
169
|
|
|
else if ($this->mode == Tokenizer::NOT) { |
170
|
|
|
$this->result[count($this->result)-1] = $this->result[count($this->result)-1] != $newValue; |
171
|
|
|
} |
172
|
|
|
else if ($this->mode == Tokenizer::EQUALS) { |
173
|
|
|
$this->result[count($this->result)-1] = $this->result[count($this->result)-1] == $newValue; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->result; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
private function callFunc($name, $args, $element, $data) { |
|
|
|
|
180
|
|
|
if ($this->data instanceof \Transphporm\FunctionSet) return $this->data->$name($args, $element); |
181
|
|
|
else return $this->callFuncOnObject($this->data, $name, $args, $element); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
private function callFuncOnObject($obj, $func, $args, $element) { |
|
|
|
|
185
|
|
|
if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args); |
186
|
|
|
else if (isset($obj->$func) && is_array($obj->$func)) { |
|
|
|
|
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
else return call_user_func_array([$obj, $func], $args); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.