@@ -2,16 +2,16 @@ |
||
2 | 2 | namespace Transphporm; |
3 | 3 | |
4 | 4 | class FilePath { |
5 | - private $baseDir; |
|
6 | - private $cwd; |
|
5 | + private $baseDir; |
|
6 | + private $cwd; |
|
7 | 7 | |
8 | - public function __construct(&$baseDir, $customBase = null) { |
|
9 | - $this->baseDir = &$baseDir; |
|
10 | - if ($customBase === null) $this->customBase = getcwd(); |
|
11 | - else $this->customBase = rtrim($customBase, '/'); |
|
12 | - } |
|
8 | + public function __construct(&$baseDir, $customBase = null) { |
|
9 | + $this->baseDir = &$baseDir; |
|
10 | + if ($customBase === null) $this->customBase = getcwd(); |
|
11 | + else $this->customBase = rtrim($customBase, '/'); |
|
12 | + } |
|
13 | 13 | |
14 | - public function getFilePath($filePath = "") { |
|
14 | + public function getFilePath($filePath = "") { |
|
15 | 15 | if (isset($filePath[0]) && $filePath[0] == "/") return $this->customBase . $filePath; |
16 | 16 | else return $this->baseDir . $filePath; |
17 | 17 | } |
@@ -12,7 +12,7 @@ |
||
12 | 12 | } |
13 | 13 | |
14 | 14 | public function getFilePath($filePath = "") { |
15 | - if (isset($filePath[0]) && $filePath[0] == "/") return $this->customBase . $filePath; |
|
16 | - else return $this->baseDir . $filePath; |
|
15 | + if (isset($filePath[0]) && $filePath[0] == "/") return $this->customBase.$filePath; |
|
16 | + else return $this->baseDir.$filePath; |
|
17 | 17 | } |
18 | 18 | } |
@@ -7,12 +7,18 @@ |
||
7 | 7 | |
8 | 8 | public function __construct(&$baseDir, $customBase = null) { |
9 | 9 | $this->baseDir = &$baseDir; |
10 | - if ($customBase === null) $this->customBase = getcwd(); |
|
11 | - else $this->customBase = rtrim($customBase, '/'); |
|
10 | + if ($customBase === null) { |
|
11 | + $this->customBase = getcwd(); |
|
12 | + } else { |
|
13 | + $this->customBase = rtrim($customBase, '/'); |
|
14 | + } |
|
12 | 15 | } |
13 | 16 | |
14 | 17 | public function getFilePath($filePath = "") { |
15 | - if (isset($filePath[0]) && $filePath[0] == "/") return $this->customBase . $filePath; |
|
16 | - else return $this->baseDir . $filePath; |
|
18 | + if (isset($filePath[0]) && $filePath[0] == "/") { |
|
19 | + return $this->customBase . $filePath; |
|
20 | + } else { |
|
21 | + return $this->baseDir . $filePath; |
|
22 | + } |
|
17 | 23 | } |
18 | 24 | } |
@@ -43,7 +43,7 @@ |
||
43 | 43 | |
44 | 44 | public function extract($last, $autoLookup) { |
45 | 45 | $value = $this->read($last); |
46 | - if ($value && ($autoLookup || is_array($this->data) || $this->data instanceof \ArrayAccess) ) { |
|
46 | + if ($value && ($autoLookup || is_array($this->data) || $this->data instanceof \ArrayAccess)) { |
|
47 | 47 | return $value; |
48 | 48 | } |
49 | 49 | throw new \UnexpectedValueException('Not found'); |
@@ -14,16 +14,23 @@ discard block |
||
14 | 14 | } |
15 | 15 | |
16 | 16 | public function traverse($key) { |
17 | - if (isset($this->data->{$key})) $this->data = $this->data->{$key}; |
|
18 | - else if ((is_array($this->data) || $this->data instanceof \ArrayAccess) && isset($this->data[$key])) $this->data = $this->data[$key]; |
|
17 | + if (isset($this->data->{$key})) { |
|
18 | + $this->data = $this->data->{$key}; |
|
19 | + } else if ((is_array($this->data) || $this->data instanceof \ArrayAccess) && isset($this->data[$key])) { |
|
20 | + $this->data = $this->data[$key]; |
|
21 | + } |
|
19 | 22 | } |
20 | 23 | |
21 | 24 | public function read($value) { |
22 | 25 | if ((is_array($this->data) || $this->data instanceof \ArrayAccess)) { |
23 | - if (isset($this->data[$value])) return $this->data[$value]; |
|
26 | + if (isset($this->data[$value])) { |
|
27 | + return $this->data[$value]; |
|
28 | + } |
|
29 | + } else if (isset($this->data->$value)) { |
|
30 | + return $this->data->$value; |
|
31 | + } else { |
|
32 | + return false; |
|
24 | 33 | } |
25 | - else if (isset($this->data->$value)) return $this->data->$value; |
|
26 | - else return false; |
|
27 | 34 | } |
28 | 35 | |
29 | 36 | public function call($func, $args) { |
@@ -32,14 +39,20 @@ discard block |
||
32 | 39 | |
33 | 40 | public function parseNested($parser, $token, $funcName) { |
34 | 41 | $args = $parser->parseTokens($token['value'], $this->data); |
35 | - if ($args[0] == $this->data) $args = []; |
|
42 | + if ($args[0] == $this->data) { |
|
43 | + $args = []; |
|
44 | + } |
|
36 | 45 | return $this->callFuncOnObject($this->data, $funcName, $args); |
37 | 46 | } |
38 | 47 | |
39 | 48 | private function callFuncOnObject($obj, $func, $args) { |
40 | - if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args); |
|
41 | - else if (is_callable([$obj, $func])) return call_user_func_array([$obj, $func], $args); |
|
42 | - else return false; |
|
49 | + if (isset($obj->$func) && is_callable($obj->$func)) { |
|
50 | + return call_user_func_array($obj->$func, $args); |
|
51 | + } else if (is_callable([$obj, $func])) { |
|
52 | + return call_user_func_array([$obj, $func], $args); |
|
53 | + } else { |
|
54 | + return false; |
|
55 | + } |
|
43 | 56 | } |
44 | 57 | |
45 | 58 | public function extract($last, $autoLookup) { |
@@ -27,9 +27,10 @@ discard block |
||
27 | 27 | try { |
28 | 28 | $parts = $this->getFuncParts($tokens); |
29 | 29 | $matches = $function->match($parts['name'], $parts['args'], $element); |
30 | - if ($matches === false) return false; |
|
31 | - } |
|
32 | - catch (\Exception $e) { |
|
30 | + if ($matches === false) { |
|
31 | + return false; |
|
32 | + } |
|
33 | + } catch (\Exception $e) { |
|
33 | 34 | throw new \Transphporm\RunException(\Transphporm\Exception::PSEUDO, $parts['name'], $e); |
34 | 35 | } |
35 | 36 | } |
@@ -42,31 +43,37 @@ discard block |
||
42 | 43 | $parts['name'] = $this->getFuncName($tokens); |
43 | 44 | if ($parts['name'] === null || in_array($parts['name'], ['data', 'iteration', 'root'])) { |
44 | 45 | $parts['args'] = $this->valueParser->parseTokens($tokens); |
45 | - } |
|
46 | - else if (count($tokens) > 1) { |
|
46 | + } else if (count($tokens) > 1) { |
|
47 | 47 | $tokens->rewind(); |
48 | 48 | $tokens->next(); |
49 | 49 | $parts['args'] = $this->valueParser->parseTokens($tokens->current()['value']); |
50 | + } else { |
|
51 | + $parts['args'] = [['']]; |
|
50 | 52 | } |
51 | - else $parts['args'] = [['']]; |
|
52 | 53 | return $parts; |
53 | 54 | } |
54 | 55 | |
55 | 56 | private function getFuncName($tokens) { |
56 | - if ($tokens->type() === Tokenizer::NAME) return $tokens->read(); |
|
57 | + if ($tokens->type() === Tokenizer::NAME) { |
|
58 | + return $tokens->read(); |
|
59 | + } |
|
57 | 60 | return null; |
58 | 61 | } |
59 | 62 | |
60 | 63 | public function hasFunction($name) { |
61 | 64 | foreach ($this->pseudo as $tokens) { |
62 | - if ($name === $this->getFuncName($tokens)) return true; |
|
65 | + if ($name === $this->getFuncName($tokens)) { |
|
66 | + return true; |
|
67 | + } |
|
63 | 68 | } |
64 | 69 | } |
65 | 70 | |
66 | 71 | public function getFuncArgs($name) { |
67 | 72 | foreach ($this->pseudo as $tokens) { |
68 | 73 | $parts = $this->getFuncParts($tokens); |
69 | - if ($name === $parts['name']) return $parts['args']; |
|
74 | + if ($name === $parts['name']) { |
|
75 | + return $parts['args']; |
|
76 | + } |
|
70 | 77 | } |
71 | 78 | } |
72 | 79 | } |
@@ -21,15 +21,13 @@ |
||
21 | 21 | $tokens = $args[0]; |
22 | 22 | $parser = new \Transphporm\Parser\Value($this); |
23 | 23 | $args[0] = $parser->parseTokens($tokens, $this->elementData->getData($this->element)); |
24 | - } |
|
25 | - else if (isset($args[0]) && $args[0] instanceof Parser\Tokens) { |
|
24 | + } else if (isset($args[0]) && $args[0] instanceof Parser\Tokens) { |
|
26 | 25 | $args[0] = iterator_to_array($args[0]); |
27 | 26 | } |
28 | 27 | if (isset($this->functions[$name])) { |
29 | 28 | return $this->functions[$name]->run($args[0], $this->element); |
30 | 29 | } |
31 | - } |
|
32 | - catch (\Exception $e) { |
|
30 | + } catch (\Exception $e) { |
|
33 | 31 | throw new RunException(Exception::TSS_FUNCTION, $name, $e); |
34 | 32 | } |
35 | 33 | return true; |
@@ -50,7 +50,9 @@ discard block |
||
50 | 50 | $this->data = new ValueData($data ? $data : $this->baseData); |
51 | 51 | $this->last = null; |
52 | 52 | |
53 | - if (count($tokens) <= 0) return [$data]; |
|
53 | + if (count($tokens) <= 0) { |
|
54 | + return [$data]; |
|
55 | + } |
|
54 | 56 | |
55 | 57 | foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE, Tokenizer::NEW_LINE]) as $token) { |
56 | 58 | $this->{$this->tokenFuncs[$token['type']]}($token); |
@@ -72,16 +74,16 @@ discard block |
||
72 | 74 | //Reads the last selected value from $data regardless if it's an array or object and overrides $this->data with the new value |
73 | 75 | //Dot moves $data to the next object in $data foo.bar moves the $data pointer from `foo` to `bar` |
74 | 76 | private function processDot($token) { |
75 | - if ($this->last !== null) $this->data->traverse($this->last); |
|
76 | - else { |
|
77 | + if ($this->last !== null) { |
|
78 | + $this->data->traverse($this->last); |
|
79 | + } else { |
|
77 | 80 | //When . is not preceeded by anything, treat it as part of the string instead of an operator |
78 | 81 | // foo.bar is treated as looking up `bar` in `foo` whereas .foo is treated as the string ".foo" |
79 | 82 | $lastResult = $this->result->pop(); |
80 | 83 | if ($lastResult) { |
81 | 84 | $this->data = new ValueData($lastResult); |
82 | 85 | $this->traversing = true; |
83 | - } |
|
84 | - else { |
|
86 | + } else { |
|
85 | 87 | $this->processString(['value' => '.']); |
86 | 88 | $this->result->setMode(Tokenizer::CONCAT); |
87 | 89 | } |
@@ -94,15 +96,19 @@ discard block |
||
94 | 96 | $parser = new Value($this->baseData, $this->autoLookup); |
95 | 97 | if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) { |
96 | 98 | $this->callTransphpormFunctions($token); |
97 | - } |
|
98 | - else { |
|
99 | - if ($this->last !== null) $this->data->traverse($this->last); |
|
100 | - else { |
|
99 | + } else { |
|
100 | + if ($this->last !== null) { |
|
101 | + $this->data->traverse($this->last); |
|
102 | + } else { |
|
101 | 103 | $lastResult = $this->result->pop(); |
102 | - if ($lastResult) $this->data = new ValueData($lastResult); |
|
104 | + if ($lastResult) { |
|
105 | + $this->data = new ValueData($lastResult); |
|
106 | + } |
|
103 | 107 | } |
104 | 108 | $this->last = $parser->parseTokens($token['value'], null)[0]; |
105 | - if (!is_bool($this->last)) $this->traversing = true; |
|
109 | + if (!is_bool($this->last)) { |
|
110 | + $this->traversing = true; |
|
111 | + } |
|
106 | 112 | } |
107 | 113 | } |
108 | 114 | |
@@ -121,8 +127,7 @@ discard block |
||
121 | 127 | private function processBrackets($token) { |
122 | 128 | if ($this->baseData instanceof \Transphporm\Functionset && $this->baseData->hasFunction($this->last)) { |
123 | 129 | $this->callTransphpormFunctions($token); |
124 | - } |
|
125 | - else { |
|
130 | + } else { |
|
126 | 131 | $this->processNested($token); |
127 | 132 | } |
128 | 133 | } |
@@ -139,7 +144,9 @@ discard block |
||
139 | 144 | foreach ($this->result->getResult() as $i => $value) { |
140 | 145 | if (is_scalar($value)) { |
141 | 146 | $val = $this->data->read($value); |
142 | - if ($val) $this->result[$i] = $val; |
|
147 | + if ($val) { |
|
148 | + $this->result[$i] = $val; |
|
149 | + } |
|
143 | 150 | } |
144 | 151 | } |
145 | 152 | $this->last = null; |
@@ -151,12 +158,10 @@ discard block |
||
151 | 158 | try { |
152 | 159 | $value = $this->data->extract($this->last, $this->autoLookup); |
153 | 160 | $this->result->processValue($value); |
154 | - } |
|
155 | - catch (\UnexpectedValueException $e) { |
|
161 | + } catch (\UnexpectedValueException $e) { |
|
156 | 162 | if (!($this->autoLookup || $this->traversing)) { |
157 | 163 | $this->result->processValue($this->last); |
158 | - } |
|
159 | - else { |
|
164 | + } else { |
|
160 | 165 | $this->result->clear(); |
161 | 166 | $this->result[0] = false; |
162 | 167 | } |
@@ -3,33 +3,33 @@ |
||
3 | 3 | namespace Transphporm\TSSFunction; |
4 | 4 | |
5 | 5 | class Math implements \Transphporm\TSSFunction { |
6 | - private $mode; |
|
6 | + private $mode; |
|
7 | 7 | |
8 | - const ADD = 1; |
|
9 | - const SUBTRACT = 2; |
|
10 | - const MULTIPLY = 3; |
|
11 | - const DIVIDE = 4; |
|
8 | + const ADD = 1; |
|
9 | + const SUBTRACT = 2; |
|
10 | + const MULTIPLY = 3; |
|
11 | + const DIVIDE = 4; |
|
12 | 12 | |
13 | - public function __construct($mode) { |
|
14 | - $this->mode = $mode; |
|
15 | - } |
|
13 | + public function __construct($mode) { |
|
14 | + $this->mode = $mode; |
|
15 | + } |
|
16 | 16 | |
17 | - public function run(array $args, \DomElement $element) { |
|
18 | - $result = $args[0]; |
|
19 | - for ($i = 1; $i < count($args); $i++) $result = $this->getModeResult($args[$i], $result); |
|
20 | - return $result; |
|
21 | - } |
|
17 | + public function run(array $args, \DomElement $element) { |
|
18 | + $result = $args[0]; |
|
19 | + for ($i = 1; $i < count($args); $i++) $result = $this->getModeResult($args[$i], $result); |
|
20 | + return $result; |
|
21 | + } |
|
22 | 22 | |
23 | - private function getModeResult($val, $prev) { |
|
24 | - switch ($this->mode) { |
|
25 | - case Math::ADD: |
|
26 | - return $prev+$val; |
|
27 | - case Math::SUBTRACT: |
|
28 | - return $prev-$val; |
|
29 | - case Math::MULTIPLY: |
|
30 | - return $prev*$val; |
|
31 | - case MATH::DIVIDE: |
|
32 | - return $prev/$val; |
|
33 | - } |
|
34 | - } |
|
23 | + private function getModeResult($val, $prev) { |
|
24 | + switch ($this->mode) { |
|
25 | + case Math::ADD: |
|
26 | + return $prev+$val; |
|
27 | + case Math::SUBTRACT: |
|
28 | + return $prev-$val; |
|
29 | + case Math::MULTIPLY: |
|
30 | + return $prev*$val; |
|
31 | + case MATH::DIVIDE: |
|
32 | + return $prev/$val; |
|
33 | + } |
|
34 | + } |
|
35 | 35 | } |
@@ -27,9 +27,9 @@ |
||
27 | 27 | case Math::SUBTRACT: |
28 | 28 | return $prev-$val; |
29 | 29 | case Math::MULTIPLY: |
30 | - return $prev*$val; |
|
30 | + return $prev * $val; |
|
31 | 31 | case MATH::DIVIDE: |
32 | - return $prev/$val; |
|
32 | + return $prev / $val; |
|
33 | 33 | } |
34 | 34 | } |
35 | 35 | } |
@@ -16,7 +16,9 @@ |
||
16 | 16 | |
17 | 17 | public function run(array $args, \DomElement $element) { |
18 | 18 | $result = $args[0]; |
19 | - for ($i = 1; $i < count($args); $i++) $result = $this->getModeResult($args[$i], $result); |
|
19 | + for ($i = 1; $i < count($args); $i++) { |
|
20 | + $result = $this->getModeResult($args[$i], $result); |
|
21 | + } |
|
20 | 22 | return $result; |
21 | 23 | } |
22 | 24 |