1 | <?php |
||
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) { |
||
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) { |
||
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); |
||
120 | |||
121 | private function processNested($token) { |
||
127 | |||
128 | private function callTransphpormFunctions($token) { |
||
138 | |||
139 | //Applies the current operation to whatever is in $last based on $mode |
||
140 | private function processLast() { |
||
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.