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