@@ -6,7 +6,7 @@ |
||
6 | 6 | * @version 1.0 */ |
7 | 7 | namespace Transphporm\TSSFunction; |
8 | 8 | /* Handles data() and iteration() function calls from the stylesheet */ |
9 | -class Data implements \Transphporm\TSSFunction{ |
|
9 | +class Data implements \Transphporm\TSSFunction { |
|
10 | 10 | private $data; |
11 | 11 | private $dataKey; |
12 | 12 | private $functionSet; |
@@ -24,7 +24,9 @@ discard block |
||
24 | 24 | public function parse($str, $element = null, $returnTokens = false) { |
25 | 25 | $tokenizer = new Tokenizer($str); |
26 | 26 | $tokens = $tokenizer->getTokens(); |
27 | - if ($returnTokens) return $tokens; |
|
27 | + if ($returnTokens) { |
|
28 | + return $tokens; |
|
29 | + } |
|
28 | 30 | $this->result = $this->parseTokens($tokens, $element, $this->baseData); |
29 | 31 | return $this->result; |
30 | 32 | } |
@@ -36,7 +38,9 @@ discard block |
||
36 | 38 | $this->last = null; |
37 | 39 | $this->element = $element; |
38 | 40 | |
39 | - if (empty($tokens)) return [$this->data]; |
|
41 | + if (empty($tokens)) { |
|
42 | + return [$this->data]; |
|
43 | + } |
|
40 | 44 | |
41 | 45 | $tokenFuncs = [ |
42 | 46 | Tokenizer::NOT => 'processComparator', |
@@ -64,27 +68,36 @@ discard block |
||
64 | 68 | |
65 | 69 | if ($this->mode == Tokenizer::NOT && $token['type'] == Tokenizer::EQUALS) { |
66 | 70 | $this->mode = Tokenizer::NOT; |
71 | + } else { |
|
72 | + $this->mode = $token['type']; |
|
67 | 73 | } |
68 | - else $this->mode = $token['type']; |
|
69 | 74 | } |
70 | 75 | |
71 | 76 | |
72 | 77 | //Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value |
73 | 78 | private function moveLastToData() { |
74 | - if (isset($this->data->{$this->last})) $this->data = $this->data->{$this->last}; |
|
75 | - else if (is_array($this->data) && isset($this->data[$this->last])) $this->data = $this->data[$this->last]; |
|
79 | + if (isset($this->data->{$this->last})) { |
|
80 | + $this->data = $this->data->{$this->last}; |
|
81 | + } else if (is_array($this->data) && isset($this->data[$this->last])) { |
|
82 | + $this->data = $this->data[$this->last]; |
|
83 | + } |
|
76 | 84 | } |
77 | 85 | |
78 | 86 | //Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar` |
79 | 87 | private function processDot($token) { |
80 | - if ($this->last !== null) $this->moveLastToData(); |
|
81 | - else $this->data = array_pop($this->result); |
|
88 | + if ($this->last !== null) { |
|
89 | + $this->moveLastToData(); |
|
90 | + } else { |
|
91 | + $this->data = array_pop($this->result); |
|
92 | + } |
|
82 | 93 | |
83 | 94 | $this->last = null; |
84 | 95 | } |
85 | 96 | |
86 | 97 | private function processSquareBracket($token) { |
87 | - if ($this->last !== null) $this->moveLastToData(); |
|
98 | + if ($this->last !== null) { |
|
99 | + $this->moveLastToData(); |
|
100 | + } |
|
88 | 101 | |
89 | 102 | $parser = new Value($this->baseData, $this->autoLookup); |
90 | 103 | $this->last = $parser->parseTokens($token['value'], $this->element, null)[0]; |
@@ -107,15 +120,15 @@ discard block |
||
107 | 120 | private function processBrackets($token) { |
108 | 121 | if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) { |
109 | 122 | $this->callTransphpormFunctions($token); |
110 | - } |
|
111 | - else if ($this->data instanceof \Transphporm\Functionset) { |
|
123 | + } else if ($this->data instanceof \Transphporm\Functionset) { |
|
112 | 124 | $this->result = $this->processValue($this->data->{$this->last}($token['value'], $this->element)); |
113 | 125 | $this->last = null; |
114 | - } |
|
115 | - else { |
|
126 | + } else { |
|
116 | 127 | $parser = new Value($this->baseData, $this->autoLookup); |
117 | 128 | $args = $parser->parseTokens($token['value'], $this->element, $this->data); |
118 | - if ($args[0] == $this->data) $args = []; |
|
129 | + if ($args[0] == $this->data) { |
|
130 | + $args = []; |
|
131 | + } |
|
119 | 132 | $funcResult = $this->callFunc($this->last, $args, $this->element, $this->data); |
120 | 133 | $this->result = $this->processValue($funcResult); |
121 | 134 | $this->last = null; |
@@ -126,9 +139,12 @@ discard block |
||
126 | 139 | $this->result = $this->processValue($this->baseData->{$this->last}($token['value'], $this->element)); |
127 | 140 | foreach ($this->result as $i => $value) { |
128 | 141 | if (is_array($this->data)) { |
129 | - if (isset($this->data[$value])) $this->result[$i] = $this->data[$value]; |
|
142 | + if (isset($this->data[$value])) { |
|
143 | + $this->result[$i] = $this->data[$value]; |
|
144 | + } |
|
145 | + } else if (is_scalar($value) && isset($this->data->$value)) { |
|
146 | + $this->result[$i] = $this->data->$value; |
|
130 | 147 | } |
131 | - else if (is_scalar($value) && isset($this->data->$value)) $this->result[$i] = $this->data->$value; |
|
132 | 148 | } |
133 | 149 | $this->last = null; |
134 | 150 | } |
@@ -138,12 +154,12 @@ discard block |
||
138 | 154 | if ($this->last !== null) { |
139 | 155 | try { |
140 | 156 | $this->result = $this->extractLast($this->result); |
141 | - } |
|
142 | - catch (\UnexpectedValueException $e) { |
|
157 | + } catch (\UnexpectedValueException $e) { |
|
143 | 158 | if (!$this->autoLookup) { |
144 | 159 | $this->result = $this->processValue($this->last); |
160 | + } else { |
|
161 | + $this->result = [false]; |
|
145 | 162 | } |
146 | - else $this->result = [false]; |
|
147 | 163 | } |
148 | 164 | } |
149 | 165 | return $this->result; |
@@ -152,8 +168,7 @@ discard block |
||
152 | 168 | private function extractLast($result) { |
153 | 169 | if ($this->autoLookup && isset($this->data->{$this->last})) { |
154 | 170 | return $this->processValue($this->data->{$this->last}); |
155 | - } |
|
156 | - else if (is_array($this->data) && isset($this->data[$this->last])) { |
|
171 | + } else if (is_array($this->data) && isset($this->data[$this->last])) { |
|
157 | 172 | return $this->processValue($this->data[$this->last]); |
158 | 173 | } |
159 | 174 | throw new \UnexpectedValueException('Not found'); |
@@ -162,14 +177,11 @@ discard block |
||
162 | 177 | private function processValue($newValue) { |
163 | 178 | if ($this->mode == Tokenizer::ARG) { |
164 | 179 | $this->result[] = $newValue; |
165 | - } |
|
166 | - else if ($this->mode == Tokenizer::CONCAT) { |
|
180 | + } else if ($this->mode == Tokenizer::CONCAT) { |
|
167 | 181 | $this->result[count($this->result)-1] .= $newValue; |
168 | - } |
|
169 | - else if ($this->mode == Tokenizer::NOT) { |
|
182 | + } else if ($this->mode == Tokenizer::NOT) { |
|
170 | 183 | $this->result[count($this->result)-1] = $this->result[count($this->result)-1] != $newValue; |
171 | - } |
|
172 | - else if ($this->mode == Tokenizer::EQUALS) { |
|
184 | + } else if ($this->mode == Tokenizer::EQUALS) { |
|
173 | 185 | $this->result[count($this->result)-1] = $this->result[count($this->result)-1] == $newValue; |
174 | 186 | } |
175 | 187 | |
@@ -177,15 +189,20 @@ discard block |
||
177 | 189 | } |
178 | 190 | |
179 | 191 | private function callFunc($name, $args, $element, $data) { |
180 | - if ($this->data instanceof \Transphporm\FunctionSet) return $this->data->$name($args, $element); |
|
181 | - else return $this->callFuncOnObject($this->data, $name, $args, $element); |
|
192 | + if ($this->data instanceof \Transphporm\FunctionSet) { |
|
193 | + return $this->data->$name($args, $element); |
|
194 | + } else { |
|
195 | + return $this->callFuncOnObject($this->data, $name, $args, $element); |
|
196 | + } |
|
182 | 197 | } |
183 | 198 | |
184 | 199 | private function callFuncOnObject($obj, $func, $args, $element) { |
185 | - if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args); |
|
186 | - else if (isset($obj->$func) && is_array($obj->$func)) { |
|
200 | + if (isset($obj->$func) && is_callable($obj->$func)) { |
|
201 | + return call_user_func_array($obj->$func, $args); |
|
202 | + } else if (isset($obj->$func) && is_array($obj->$func)) { |
|
187 | 203 | |
204 | + } else { |
|
205 | + return call_user_func_array([$obj, $func], $args); |
|
188 | 206 | } |
189 | - else return call_user_func_array([$obj, $func], $args); |
|
190 | 207 | } |
191 | 208 | } |