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