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
|
|
|
/* |
13
|
|
|
Stores the last value e.g. |
14
|
|
|
"a" + "b" |
15
|
|
|
Will store "a" before reading the token for the + and perfoming the concatenate operation |
16
|
|
|
*/ |
17
|
|
|
private $last; |
18
|
|
|
private $data; |
19
|
|
|
private $result; |
20
|
|
|
private $traversing = false; |
21
|
|
|
|
22
|
|
|
private $tokenFuncs = [ |
23
|
|
|
Tokenizer::NOT => 'processComparator', |
24
|
|
|
Tokenizer::EQUALS => 'processComparator', |
25
|
|
|
Tokenizer::DOT => 'processDot', |
26
|
|
|
Tokenizer::OPEN_SQUARE_BRACKET => 'processSquareBracket', |
27
|
|
|
Tokenizer::ARG => 'processSeparator', |
28
|
|
|
Tokenizer::CONCAT => 'processSeparator', |
29
|
|
|
Tokenizer::SUBTRACT => 'processSeparator', |
30
|
|
|
Tokenizer::MULTIPLY => 'processSeparator', |
31
|
|
|
Tokenizer::DIVIDE => 'processSeparator', |
32
|
|
|
Tokenizer::NAME => 'processScalar', |
33
|
|
|
Tokenizer::NUMERIC => 'processString', |
34
|
|
|
Tokenizer::BOOL => 'processString', |
35
|
|
|
Tokenizer::STRING => 'processString', |
36
|
|
|
Tokenizer::OPEN_BRACKET => 'processBrackets' |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
public function __construct($data, $autoLookup = false) { |
40
|
|
|
$this->baseData = $data; |
41
|
|
|
$this->autoLookup = $autoLookup; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function parse($str) { |
45
|
|
|
$tokenizer = new Tokenizer($str); |
46
|
|
|
$tokens = $tokenizer->getTokens(); |
47
|
|
|
$this->result = $this->parseTokens($tokens, $this->baseData); |
48
|
|
|
return $this->result; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function parseTokens($tokens, $data = null) { |
52
|
|
|
$this->result = new ValueResult; |
53
|
|
|
$this->data = new ValueData($data ? $data : $this->baseData); |
54
|
|
|
$this->last = null; |
55
|
|
|
$this->traversing = false; |
56
|
|
|
|
57
|
|
|
if (count($tokens) <= 0) return [$data]; |
58
|
|
|
|
59
|
|
|
foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE, Tokenizer::NEW_LINE]) as $token) { |
60
|
|
|
$this->{$this->tokenFuncs[$token['type']]}($token); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->processLast(); |
64
|
|
|
return $this->result->getResult(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function processComparator($token) { |
68
|
|
|
$this->processLast(); |
69
|
|
|
|
70
|
|
|
if (!(in_array($this->result->getMode(), array_keys($this->tokenFuncs, 'processComparator')) && $token['type'] == Tokenizer::EQUALS)) { |
71
|
|
|
$this->result->setMode($token['type']); |
72
|
|
|
$this->last = null; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function traverseLast() { |
77
|
|
|
if ($this->last !== null) $this->data->traverse($this->last); |
78
|
|
|
else { |
79
|
|
|
$lastResult = $this->result->pop(); |
80
|
|
|
if ($lastResult) { |
81
|
|
|
$this->data = new ValueData($lastResult); |
82
|
|
|
return $lastResult; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
//Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value |
88
|
|
|
//Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar` |
89
|
|
|
private function processDot($token) { |
|
|
|
|
90
|
|
|
$lastResult = $this->traverseLast(); |
91
|
|
|
|
92
|
|
|
//When . is not preceeded by anything, treat it as part of the string instead of an operator |
93
|
|
|
// foo.bar is treated as looking up `bar` in `foo` whereas .foo is treated as the string ".foo" |
94
|
|
|
if ($lastResult) { |
95
|
|
|
$this->traversing = true; |
96
|
|
|
} |
97
|
|
|
else if ($this->last === null) { |
98
|
|
|
$this->processString(['value' => '.']); |
99
|
|
|
$this->result->setMode(Tokenizer::CONCAT); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->last = null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function hasFunction($name) { |
106
|
|
|
return $this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($name); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function processSquareBracket($token) { |
110
|
|
|
$parser = new Value($this->baseData, $this->autoLookup); |
111
|
|
|
if ($this->hasFunction($this->last)) { |
112
|
|
|
$this->callTransphpormFunctions($token); |
113
|
|
|
} |
114
|
|
|
else { |
115
|
|
|
$this->traverseLast(); |
116
|
|
|
$this->last = $parser->parseTokens($token['value'], null)[0]; |
117
|
|
|
if (!is_bool($this->last)) $this->traversing = true; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function processSeparator($token) { |
122
|
|
|
$this->result->setMode($token['type']); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function processScalar($token) { |
126
|
|
|
$this->last = $token['value']; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function processString($token) { |
130
|
|
|
$this->result->processValue($token['value']); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function processBrackets($token) { |
134
|
|
|
if ($this->hasFunction($this->last) |
135
|
|
|
&& !$this->data->methodExists($this->last)) { |
136
|
|
|
$this->callTransphpormFunctions($token); |
137
|
|
|
} |
138
|
|
|
else { |
139
|
|
|
$this->processNested($token); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function processNested($token) { |
144
|
|
|
$parser = new Value($this->baseData, $this->autoLookup); |
145
|
|
|
$funcResult = $this->data->parseNested($parser, $token, $this->last); |
146
|
|
|
$this->result->processValue($funcResult); |
147
|
|
|
$this->last = null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
private function callTransphpormFunctions($token) { |
151
|
|
|
$this->result->processValue($this->baseData->{$this->last}($token['value'])); |
152
|
|
|
foreach ($this->result->getResult() as $i => $value) { |
153
|
|
|
if (is_scalar($value)) { |
154
|
|
|
$val = $this->data->read($value); |
155
|
|
|
if ($val) $this->result[$i] = $val; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
$this->last = null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
//Applies the current operation to whatever is in $last based on $mode |
162
|
|
|
private function processLast() { |
163
|
|
|
if ($this->last !== null) { |
164
|
|
|
try { |
165
|
|
|
$value = $this->data->extract($this->last, $this->autoLookup, $this->traversing); |
166
|
|
|
$this->result->processValue($value); |
167
|
|
|
} |
168
|
|
|
catch (\UnexpectedValueException $e) { |
169
|
|
|
$this->processLastUnexpected(); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
private function processLastUnexpected() { |
175
|
|
|
if (!($this->autoLookup || $this->traversing)) { |
176
|
|
|
$this->result->processValue($this->last); |
177
|
|
|
} |
178
|
|
|
else { |
179
|
|
|
$this->result->clear(); |
180
|
|
|
$this->result[0] = false; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.