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 = new ValueData($data); |
52
|
|
|
$this->last = null; |
53
|
|
|
|
54
|
|
|
if (empty($tokens)) return [$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
|
|
|
|
76
|
|
|
//Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar` |
77
|
|
|
private function processDot($token) { |
|
|
|
|
78
|
|
|
if ($this->last !== null) $this->data->traverse($this->last); |
79
|
|
|
else $this->data = new ValueData($this->result->pop()); |
80
|
|
|
|
81
|
|
|
$this->last = null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function processSquareBracket($token) { |
85
|
|
|
if ($this->last !== null) $this->data->traverse($this->last); |
86
|
|
|
$parser = new Value($this->baseData, $this->autoLookup); |
87
|
|
|
$this->last = $parser->parseTokens($token['value'], null)[0]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function processSeparator($token) { |
91
|
|
|
$this->result->setMode($token['type']); |
92
|
|
|
//if ($this->last !== null) $this->result = $this->processValue($this->result, $this->mode, $this->last); |
|
|
|
|
93
|
|
|
$this->result = $this->processLast(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function processScalar($token) { |
97
|
|
|
$this->last = $token['value']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function processString($token) { |
101
|
|
|
$this->result->processValue($token['value']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function processBrackets($token) { |
105
|
|
|
if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) { |
106
|
|
|
$this->callTransphpormFunctions($token); |
107
|
|
|
} |
108
|
|
|
else if ($this->data->isFunctionSet()) { |
109
|
|
|
$this->result = $this->result->processValue($this->data->call($this->last, [$token['value']])); |
|
|
|
|
110
|
|
|
$this->last = null; |
111
|
|
|
} |
112
|
|
|
else { |
113
|
|
|
$this->processNested($token); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function processNested($token) { |
118
|
|
|
$parser = new Value($this->baseData, $this->autoLookup); |
119
|
|
|
$funcResult = $this->data->parseNested($parser, $token, $this->last); |
120
|
|
|
$this->result->processValue($funcResult); |
121
|
|
|
$this->last = null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function callTransphpormFunctions($token) { |
125
|
|
|
$this->result->processValue($this->baseData->{$this->last}($token['value'])); |
126
|
|
|
foreach ($this->result->getResult() as $i => $value) { |
127
|
|
|
$val = $this->data->read($value); |
128
|
|
|
if ($val) $this->result[$i] = $val; |
129
|
|
|
} |
130
|
|
|
$this->last = null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
//Applies the current operation to whatever is in $last based on $mode |
134
|
|
|
private function processLast() { |
135
|
|
|
if ($this->last !== null) { |
136
|
|
|
try { |
137
|
|
|
$value = $this->data->extract($this->last, $this->autoLookup); |
138
|
|
|
$this->result->processValue($value); |
139
|
|
|
} |
140
|
|
|
catch (\UnexpectedValueException $e) { |
141
|
|
|
if (!$this->autoLookup) { |
142
|
|
|
$this->result->processValue($this->last); |
143
|
|
|
} |
144
|
|
|
else { |
145
|
|
|
$this->result->clear(); |
146
|
|
|
$this->result[0] = false; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
return $this->result; |
151
|
|
|
} |
152
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.