@@ -52,7 +52,9 @@ discard block |
||
52 | 52 | public function parse($str, $element = null, $returnTokens = false) { |
53 | 53 | $tokenizer = new Tokenizer($str); |
54 | 54 | $tokens = $tokenizer->getTokens(); |
55 | - if ($returnTokens) return $tokens; |
|
55 | + if ($returnTokens) { |
|
56 | + return $tokens; |
|
57 | + } |
|
56 | 58 | $this->result = $this->parseTokens($tokens, $element, $this->baseData); |
57 | 59 | return $this->result; |
58 | 60 | } |
@@ -64,7 +66,9 @@ discard block |
||
64 | 66 | $this->last = null; |
65 | 67 | $this->element = $element; |
66 | 68 | |
67 | - if (empty($tokens)) return [$this->data]; |
|
69 | + if (empty($tokens)) { |
|
70 | + return [$this->data]; |
|
71 | + } |
|
68 | 72 | |
69 | 73 | foreach ($tokens as $token) { |
70 | 74 | $this->{$this->tokenFuncs[$token['type']]}($token); |
@@ -78,27 +82,36 @@ discard block |
||
78 | 82 | |
79 | 83 | if ($this->mode == Tokenizer::NOT && $token['type'] == Tokenizer::EQUALS) { |
80 | 84 | $this->mode = Tokenizer::NOT; |
85 | + } else { |
|
86 | + $this->mode = $token['type']; |
|
81 | 87 | } |
82 | - else $this->mode = $token['type']; |
|
83 | 88 | } |
84 | 89 | |
85 | 90 | |
86 | 91 | //Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value |
87 | 92 | private function moveLastToData() { |
88 | - if (isset($this->data->{$this->last})) $this->data = $this->data->{$this->last}; |
|
89 | - else if (is_array($this->data) && isset($this->data[$this->last])) $this->data = $this->data[$this->last]; |
|
93 | + if (isset($this->data->{$this->last})) { |
|
94 | + $this->data = $this->data->{$this->last}; |
|
95 | + } else if (is_array($this->data) && isset($this->data[$this->last])) { |
|
96 | + $this->data = $this->data[$this->last]; |
|
97 | + } |
|
90 | 98 | } |
91 | 99 | |
92 | 100 | //Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar` |
93 | 101 | private function processDot($token) { |
94 | - if ($this->last !== null) $this->moveLastToData(); |
|
95 | - else $this->data = array_pop($this->result); |
|
102 | + if ($this->last !== null) { |
|
103 | + $this->moveLastToData(); |
|
104 | + } else { |
|
105 | + $this->data = array_pop($this->result); |
|
106 | + } |
|
96 | 107 | |
97 | 108 | $this->last = null; |
98 | 109 | } |
99 | 110 | |
100 | 111 | private function processSquareBracket($token) { |
101 | - if ($this->last !== null) $this->moveLastToData(); |
|
112 | + if ($this->last !== null) { |
|
113 | + $this->moveLastToData(); |
|
114 | + } |
|
102 | 115 | |
103 | 116 | $parser = new Value($this->baseData, $this->autoLookup); |
104 | 117 | $this->last = $parser->parseTokens($token['value'], $this->element, null)[0]; |
@@ -121,15 +134,15 @@ discard block |
||
121 | 134 | private function processBrackets($token) { |
122 | 135 | if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) { |
123 | 136 | $this->callTransphpormFunctions($token); |
124 | - } |
|
125 | - else if ($this->data instanceof \Transphporm\Functionset) { |
|
137 | + } else if ($this->data instanceof \Transphporm\Functionset) { |
|
126 | 138 | $this->result = $this->processValue($this->data->{$this->last}($token['value'], $this->element)); |
127 | 139 | $this->last = null; |
128 | - } |
|
129 | - else { |
|
140 | + } else { |
|
130 | 141 | $parser = new Value($this->baseData, $this->autoLookup); |
131 | 142 | $args = $parser->parseTokens($token['value'], $this->element, $this->data); |
132 | - if ($args[0] == $this->data) $args = []; |
|
143 | + if ($args[0] == $this->data) { |
|
144 | + $args = []; |
|
145 | + } |
|
133 | 146 | $funcResult = $this->callFunc($this->last, $args, $this->element, $this->data); |
134 | 147 | $this->result = $this->processValue($funcResult); |
135 | 148 | $this->last = null; |
@@ -140,9 +153,12 @@ discard block |
||
140 | 153 | $this->result = $this->processValue($this->baseData->{$this->last}($token['value'], $this->element)); |
141 | 154 | foreach ($this->result as $i => $value) { |
142 | 155 | if (is_array($this->data)) { |
143 | - if (isset($this->data[$value])) $this->result[$i] = $this->data[$value]; |
|
156 | + if (isset($this->data[$value])) { |
|
157 | + $this->result[$i] = $this->data[$value]; |
|
158 | + } |
|
159 | + } else if (is_scalar($value) && isset($this->data->$value)) { |
|
160 | + $this->result[$i] = $this->data->$value; |
|
144 | 161 | } |
145 | - else if (is_scalar($value) && isset($this->data->$value)) $this->result[$i] = $this->data->$value; |
|
146 | 162 | } |
147 | 163 | $this->last = null; |
148 | 164 | } |
@@ -152,12 +168,12 @@ discard block |
||
152 | 168 | if ($this->last !== null) { |
153 | 169 | try { |
154 | 170 | $this->result = $this->extractLast($this->result); |
155 | - } |
|
156 | - catch (\UnexpectedValueException $e) { |
|
171 | + } catch (\UnexpectedValueException $e) { |
|
157 | 172 | if (!$this->autoLookup) { |
158 | 173 | $this->result = $this->processValue($this->last); |
174 | + } else { |
|
175 | + $this->result = [false]; |
|
159 | 176 | } |
160 | - else $this->result = [false]; |
|
161 | 177 | } |
162 | 178 | } |
163 | 179 | return $this->result; |
@@ -169,8 +185,7 @@ discard block |
||
169 | 185 | private function extractLast($result) { |
170 | 186 | if ($this->autoLookup && isset($this->data->{$this->last})) { |
171 | 187 | return $this->processValue($this->data->{$this->last}); |
172 | - } |
|
173 | - else if (is_array($this->data) && isset($this->data[$this->last])) { |
|
188 | + } else if (is_array($this->data) && isset($this->data[$this->last])) { |
|
174 | 189 | return $this->processValue($this->data[$this->last]); |
175 | 190 | } |
176 | 191 | throw new \UnexpectedValueException('Not found'); |
@@ -181,14 +196,11 @@ discard block |
||
181 | 196 | private function processValue($newValue) { |
182 | 197 | if ($this->mode == Tokenizer::ARG) { |
183 | 198 | $this->result[] = $newValue; |
184 | - } |
|
185 | - else if ($this->mode == Tokenizer::CONCAT) { |
|
199 | + } else if ($this->mode == Tokenizer::CONCAT) { |
|
186 | 200 | $this->result[count($this->result)-1] .= $newValue; |
187 | - } |
|
188 | - else if ($this->mode == Tokenizer::NOT) { |
|
201 | + } else if ($this->mode == Tokenizer::NOT) { |
|
189 | 202 | $this->result[count($this->result)-1] = $this->result[count($this->result)-1] != $newValue; |
190 | - } |
|
191 | - else if ($this->mode == Tokenizer::EQUALS) { |
|
203 | + } else if ($this->mode == Tokenizer::EQUALS) { |
|
192 | 204 | $this->result[count($this->result)-1] = $this->result[count($this->result)-1] == $newValue; |
193 | 205 | } |
194 | 206 | |
@@ -196,12 +208,18 @@ discard block |
||
196 | 208 | } |
197 | 209 | |
198 | 210 | private function callFunc($name, $args, $element, $data) { |
199 | - if ($this->data instanceof \Transphporm\FunctionSet) return $this->data->$name($args, $element); |
|
200 | - else return $this->callFuncOnObject($this->data, $name, $args, $element); |
|
211 | + if ($this->data instanceof \Transphporm\FunctionSet) { |
|
212 | + return $this->data->$name($args, $element); |
|
213 | + } else { |
|
214 | + return $this->callFuncOnObject($this->data, $name, $args, $element); |
|
215 | + } |
|
201 | 216 | } |
202 | 217 | |
203 | 218 | private function callFuncOnObject($obj, $func, $args, $element) { |
204 | - if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args); |
|
205 | - else return call_user_func_array([$obj, $func], $args); |
|
219 | + if (isset($obj->$func) && is_callable($obj->$func)) { |
|
220 | + return call_user_func_array($obj->$func, $args); |
|
221 | + } else { |
|
222 | + return call_user_func_array([$obj, $func], $args); |
|
223 | + } |
|
206 | 224 | } |
207 | 225 | } |