@@ -20,8 +20,7 @@ discard block |
||
| 20 | 20 | if (isset($this->functions[$name])) { |
| 21 | 21 | return $this->functions[$name]->run($this->getArgs0($name, $args), $this->element); |
| 22 | 22 | } |
| 23 | - } |
|
| 24 | - catch (\Exception $e) { |
|
| 23 | + } catch (\Exception $e) { |
|
| 25 | 24 | throw new RunException(Exception::TSS_FUNCTION, $name, $e); |
| 26 | 25 | } |
| 27 | 26 | return true; |
@@ -32,8 +31,7 @@ discard block |
||
| 32 | 31 | $tokens = $args[0]; |
| 33 | 32 | $parser = new \Transphporm\Parser\Value($this); |
| 34 | 33 | return $parser->parseTokens($tokens, $this->elementData->getData($this->element)); |
| 35 | - } |
|
| 36 | - else if ($args[0] instanceof Parser\Tokens) { |
|
| 34 | + } else if ($args[0] instanceof Parser\Tokens) { |
|
| 37 | 35 | return iterator_to_array($args[0]); |
| 38 | 36 | } |
| 39 | 37 | |
@@ -14,7 +14,9 @@ discard block |
||
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | public function format($value, $rules) { |
| 17 | - if (!isset($rules['format'])) return $value; |
|
| 17 | + if (!isset($rules['format'])) { |
|
| 18 | + return $value; |
|
| 19 | + } |
|
| 18 | 20 | $tokens = $rules['format']; |
| 19 | 21 | |
| 20 | 22 | $functionName = $tokens->from(\Transphporm\Parser\Tokenizer::NAME, true)->read(); |
@@ -27,15 +29,16 @@ discard block |
||
| 27 | 29 | |
| 28 | 30 | try { |
| 29 | 31 | return $this->processFormat($options, $functionName, $value); |
| 30 | - } |
|
| 31 | - catch (\Exception $e) { |
|
| 32 | + } catch (\Exception $e) { |
|
| 32 | 33 | throw new \Transphporm\RunException(\Transphporm\Exception::FORMATTER, $functionName, $e); |
| 33 | 34 | } |
| 34 | 35 | } |
| 35 | 36 | |
| 36 | 37 | //TODO: Abstract all error reporting externally with a method for turning it on/off |
| 37 | 38 | private function assert($condition, $error) { |
| 38 | - if (!$condition) throw new \Exception($error); |
|
| 39 | + if (!$condition) { |
|
| 40 | + throw new \Exception($error); |
|
| 41 | + } |
|
| 39 | 42 | } |
| 40 | 43 | |
| 41 | 44 | private function processFormat($format, $functionName, $value) { |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | $this->save = function($content = null) { |
| 38 | 38 | return $this->document->saveHtml($content); |
| 39 | 39 | }; |
| 40 | - $this->document->loadHtml('<' . '?xml encoding="UTF-8">' .$doc, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); |
|
| 40 | + $this->document->loadHtml('<'.'?xml encoding="UTF-8">'.$doc, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); |
|
| 41 | 41 | |
| 42 | 42 | if (strpos($doc, '<!') !== 0) { |
| 43 | 43 | $templateNode = $this->document->getElementsByTagName('template')[0]; |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | //Either return a whole DomDocument or return the output HTML |
| 89 | 89 | if ($document) return $this->document; |
| 90 | 90 | |
| 91 | - $output = ($this->document->doctype) ? call_user_func($this->save, $this->document->doctype) . "\n" : ''; |
|
| 91 | + $output = ($this->document->doctype) ? call_user_func($this->save, $this->document->doctype)."\n" : ''; |
|
| 92 | 92 | |
| 93 | 93 | if ($this->document->documentElement->tagName !== 'template') $output .= call_user_func($this->save, $this->document->documentElement); |
| 94 | 94 | else $output = $this->printDocument(); |
@@ -66,7 +66,9 @@ discard block |
||
| 66 | 66 | /** Loops through all assigned hooks, runs the Xpath query and calls the hook */ |
| 67 | 67 | private function processHooks() { |
| 68 | 68 | foreach ($this->hooks as list($query, $hook)) { |
| 69 | - foreach ($this->xpath->query($query) as $element) $hook->run($element); |
|
| 69 | + foreach ($this->xpath->query($query) as $element) { |
|
| 70 | + $hook->run($element); |
|
| 71 | + } |
|
| 70 | 72 | } |
| 71 | 73 | $this->hooks = []; |
| 72 | 74 | } |
@@ -74,7 +76,9 @@ discard block |
||
| 74 | 76 | /** Prints out the current DomDocument as HTML */ |
| 75 | 77 | private function printDocument() { |
| 76 | 78 | $output = ''; |
| 77 | - foreach ($this->document->documentElement->childNodes as $node) $output .= call_user_func($this->save, $node); |
|
| 79 | + foreach ($this->document->documentElement->childNodes as $node) { |
|
| 80 | + $output .= call_user_func($this->save, $node); |
|
| 81 | + } |
|
| 78 | 82 | return $output; |
| 79 | 83 | } |
| 80 | 84 | |
@@ -86,12 +90,17 @@ discard block |
||
| 86 | 90 | //Generate the document by taking only the childnodes of the template, ignoring the <template> and </template> tags |
| 87 | 91 | //TODO: Is there a faster way of doing this without string manipulation on the output or this loop through childnodes? |
| 88 | 92 | //Either return a whole DomDocument or return the output HTML |
| 89 | - if ($document) return $this->document; |
|
| 93 | + if ($document) { |
|
| 94 | + return $this->document; |
|
| 95 | + } |
|
| 90 | 96 | |
| 91 | 97 | $output = ($this->document->doctype) ? call_user_func($this->save, $this->document->doctype) . "\n" : ''; |
| 92 | 98 | |
| 93 | - if ($this->document->documentElement->tagName !== 'template') $output .= call_user_func($this->save, $this->document->documentElement); |
|
| 94 | - else $output = $this->printDocument(); |
|
| 99 | + if ($this->document->documentElement->tagName !== 'template') { |
|
| 100 | + $output .= call_user_func($this->save, $this->document->documentElement); |
|
| 101 | + } else { |
|
| 102 | + $output = $this->printDocument(); |
|
| 103 | + } |
|
| 95 | 104 | |
| 96 | 105 | //repair empty tags. Browsers break on <script /> and <div /> so can't avoid LIBXML_NOEMPTYTAG but they also break on <base></base> so repair them |
| 97 | 106 | $output = str_replace(['></img>', '></br>', '></meta>', '></base>', '></link>', '></hr>', '></input>'], ' />', $output); |
@@ -11,25 +11,33 @@ |
||
| 11 | 11 | private $lastParentNode; |
| 12 | 12 | |
| 13 | 13 | public function match($name, $args, \DomElement $element) { |
| 14 | - if ($element->parentNode !== $this->lastParentNode) $this->count = 0; |
|
| 14 | + if ($element->parentNode !== $this->lastParentNode) { |
|
| 15 | + $this->count = 0; |
|
| 16 | + } |
|
| 15 | 17 | |
| 16 | 18 | $this->lastParentNode = $element->parentNode; |
| 17 | 19 | |
| 18 | 20 | |
| 19 | 21 | |
| 20 | - if ($name !== 'nth-child') return true; |
|
| 22 | + if ($name !== 'nth-child') { |
|
| 23 | + return true; |
|
| 24 | + } |
|
| 21 | 25 | |
| 22 | 26 | $this->count++; |
| 23 | 27 | $criteria = $args[0]; |
| 24 | 28 | |
| 25 | - if (is_callable([$this, $criteria])) return $this->$criteria($this->count); |
|
| 29 | + if (is_callable([$this, $criteria])) { |
|
| 30 | + return $this->$criteria($this->count); |
|
| 31 | + } |
|
| 26 | 32 | $this->assert(is_numeric($criteria), "Argument passed to 'nth-child' must be 'odd', 'even', or of type int"); |
| 27 | 33 | return $this->count == $criteria; |
| 28 | 34 | } |
| 29 | 35 | |
| 30 | 36 | //TODO: Abstract assertions throughout |
| 31 | 37 | private function assert($condition, $error) { |
| 32 | - if (!$condition) throw new \Exception($error); |
|
| 38 | + if (!$condition) { |
|
| 39 | + throw new \Exception($error); |
|
| 40 | + } |
|
| 33 | 41 | } |
| 34 | 42 | |
| 35 | 43 | private function odd($num) { |
@@ -94,7 +94,7 @@ |
||
| 94 | 94 | if ($lastResult) { |
| 95 | 95 | $this->traversing = true; |
| 96 | 96 | } |
| 97 | - else if ($this->last === null) { |
|
| 97 | + else if ($this->last === null) { |
|
| 98 | 98 | $this->processString(['value' => '.']); |
| 99 | 99 | $this->result->setMode(Tokenizer::CONCAT); |
| 100 | 100 | } |
@@ -54,7 +54,9 @@ discard block |
||
| 54 | 54 | $this->last = null; |
| 55 | 55 | $this->traversing = false; |
| 56 | 56 | |
| 57 | - if (count($tokens) <= 0) return [$data]; |
|
| 57 | + if (count($tokens) <= 0) { |
|
| 58 | + return [$data]; |
|
| 59 | + } |
|
| 58 | 60 | |
| 59 | 61 | foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE, Tokenizer::NEW_LINE]) as $token) { |
| 60 | 62 | $this->{$this->tokenFuncs[$token['type']]}($token); |
@@ -74,8 +76,9 @@ discard block |
||
| 74 | 76 | } |
| 75 | 77 | |
| 76 | 78 | private function traverseLast() { |
| 77 | - if ($this->last !== null) $this->data->traverse($this->last); |
|
| 78 | - else { |
|
| 79 | + if ($this->last !== null) { |
|
| 80 | + $this->data->traverse($this->last); |
|
| 81 | + } else { |
|
| 79 | 82 | $lastResult = $this->result->pop(); |
| 80 | 83 | if ($lastResult) { |
| 81 | 84 | $this->data = new ValueData($lastResult); |
@@ -93,8 +96,7 @@ discard block |
||
| 93 | 96 | // foo.bar is treated as looking up `bar` in `foo` whereas .foo is treated as the string ".foo" |
| 94 | 97 | if ($lastResult) { |
| 95 | 98 | $this->traversing = true; |
| 96 | - } |
|
| 97 | - else if ($this->last === null) { |
|
| 99 | + } else if ($this->last === null) { |
|
| 98 | 100 | $this->processString(['value' => '.']); |
| 99 | 101 | $this->result->setMode(Tokenizer::CONCAT); |
| 100 | 102 | } |
@@ -110,11 +112,12 @@ discard block |
||
| 110 | 112 | $parser = new Value($this->baseData, $this->autoLookup); |
| 111 | 113 | if ($this->hasFunction($this->last)) { |
| 112 | 114 | $this->callTransphpormFunctions($token); |
| 113 | - } |
|
| 114 | - else { |
|
| 115 | + } else { |
|
| 115 | 116 | $this->traverseLast(); |
| 116 | 117 | $this->last = $parser->parseTokens($token['value'], null)[0]; |
| 117 | - if (!is_bool($this->last)) $this->traversing = true; |
|
| 118 | + if (!is_bool($this->last)) { |
|
| 119 | + $this->traversing = true; |
|
| 120 | + } |
|
| 118 | 121 | } |
| 119 | 122 | } |
| 120 | 123 | |
@@ -134,8 +137,7 @@ discard block |
||
| 134 | 137 | if ($this->hasFunction($this->last) |
| 135 | 138 | && !$this->data->methodExists($this->last)) { |
| 136 | 139 | $this->callTransphpormFunctions($token); |
| 137 | - } |
|
| 138 | - else { |
|
| 140 | + } else { |
|
| 139 | 141 | $this->processNested($token); |
| 140 | 142 | } |
| 141 | 143 | } |
@@ -152,7 +154,9 @@ discard block |
||
| 152 | 154 | foreach ($this->result->getResult() as $i => $value) { |
| 153 | 155 | if (is_scalar($value)) { |
| 154 | 156 | $val = $this->data->read($value); |
| 155 | - if ($val) $this->result[$i] = $val; |
|
| 157 | + if ($val) { |
|
| 158 | + $this->result[$i] = $val; |
|
| 159 | + } |
|
| 156 | 160 | } |
| 157 | 161 | } |
| 158 | 162 | $this->last = null; |
@@ -164,8 +168,7 @@ discard block |
||
| 164 | 168 | try { |
| 165 | 169 | $value = $this->data->extract($this->last, $this->autoLookup, $this->traversing); |
| 166 | 170 | $this->result->processValue($value); |
| 167 | - } |
|
| 168 | - catch (\UnexpectedValueException $e) { |
|
| 171 | + } catch (\UnexpectedValueException $e) { |
|
| 169 | 172 | $this->processLastUnexpected(); |
| 170 | 173 | } |
| 171 | 174 | } |
@@ -174,8 +177,7 @@ discard block |
||
| 174 | 177 | private function processLastUnexpected() { |
| 175 | 178 | if (!($this->autoLookup || $this->traversing)) { |
| 176 | 179 | $this->result->processValue($this->last); |
| 177 | - } |
|
| 178 | - else { |
|
| 180 | + } else { |
|
| 179 | 181 | $this->result->clear(); |
| 180 | 182 | $this->result[0] = false; |
| 181 | 183 | } |
@@ -25,7 +25,9 @@ discard block |
||
| 25 | 25 | foreach ($this->pseudo as $tokens) { |
| 26 | 26 | foreach ($this->functions as $function) { |
| 27 | 27 | $matches = $this->match($tokens, $function, $element); |
| 28 | - if ($matches === false) return false; |
|
| 28 | + if ($matches === false) { |
|
| 29 | + return false; |
|
| 30 | + } |
|
| 29 | 31 | } |
| 30 | 32 | } |
| 31 | 33 | return true; |
@@ -35,9 +37,10 @@ discard block |
||
| 35 | 37 | try { |
| 36 | 38 | $parts = $this->getFuncParts($tokens); |
| 37 | 39 | $matches = $function->match($parts['name'], $parts['args'], $element); |
| 38 | - if ($matches === false) return false; |
|
| 39 | - } |
|
| 40 | - catch (\Exception $e) { |
|
| 40 | + if ($matches === false) { |
|
| 41 | + return false; |
|
| 42 | + } |
|
| 43 | + } catch (\Exception $e) { |
|
| 41 | 44 | throw new \Transphporm\RunException(\Transphporm\Exception::PSEUDO, $parts['name'], $e); |
| 42 | 45 | } |
| 43 | 46 | } |
@@ -46,31 +49,37 @@ discard block |
||
| 46 | 49 | $parts['name'] = $this->getFuncName($tokens); |
| 47 | 50 | if ($parts['name'] === null || in_array($parts['name'], ['data', 'iteration', 'root'])) { |
| 48 | 51 | $parts['args'] = $this->valueParser->parseTokens($tokens); |
| 49 | - } |
|
| 50 | - else if (count($tokens) > 1) { |
|
| 52 | + } else if (count($tokens) > 1) { |
|
| 51 | 53 | $tokens->rewind(); |
| 52 | 54 | $tokens->next(); |
| 53 | 55 | $parts['args'] = $this->valueParser->parseTokens($tokens->current()['value']); |
| 56 | + } else { |
|
| 57 | + $parts['args'] = [['']]; |
|
| 54 | 58 | } |
| 55 | - else $parts['args'] = [['']]; |
|
| 56 | 59 | return $parts; |
| 57 | 60 | } |
| 58 | 61 | |
| 59 | 62 | private function getFuncName($tokens) { |
| 60 | - if ($tokens->type() === Tokenizer::NAME) return $tokens->read(); |
|
| 63 | + if ($tokens->type() === Tokenizer::NAME) { |
|
| 64 | + return $tokens->read(); |
|
| 65 | + } |
|
| 61 | 66 | return null; |
| 62 | 67 | } |
| 63 | 68 | |
| 64 | 69 | public function hasFunction($name) { |
| 65 | 70 | foreach ($this->pseudo as $tokens) { |
| 66 | - if ($name === $this->getFuncName($tokens)) return true; |
|
| 71 | + if ($name === $this->getFuncName($tokens)) { |
|
| 72 | + return true; |
|
| 73 | + } |
|
| 67 | 74 | } |
| 68 | 75 | } |
| 69 | 76 | |
| 70 | 77 | public function getFuncArgs($name) { |
| 71 | 78 | foreach ($this->pseudo as $tokens) { |
| 72 | 79 | $parts = $this->getFuncParts($tokens); |
| 73 | - if ($name === $parts['name']) return $parts['args']; |
|
| 80 | + if ($name === $parts['name']) { |
|
| 81 | + return $parts['args']; |
|
| 82 | + } |
|
| 74 | 83 | } |
| 75 | 84 | } |
| 76 | 85 | } |