@@ -14,12 +14,12 @@ |
||
| 14 | 14 | |
| 15 | 15 | public function decimal($num, $decimals) { |
| 16 | 16 | if (!is_numeric($num)) return $num; |
| 17 | - return number_format((float)$num, $decimals, $this->locale['decimal_separator'], $this->locale['thousands_separator']); |
|
| 17 | + return number_format((float) $num, $decimals, $this->locale['decimal_separator'], $this->locale['thousands_separator']); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | public function currency($num) { |
| 21 | 21 | $num = $this->decimal($num, $this->locale['currency_decimals']); |
| 22 | - if ($this->locale['currency_position'] === 'before') return $this->locale['currency'] . $num; |
|
| 23 | - else return $num . $this->locale['currency']; |
|
| 22 | + if ($this->locale['currency_position'] === 'before') return $this->locale['currency'].$num; |
|
| 23 | + else return $num.$this->locale['currency']; |
|
| 24 | 24 | } |
| 25 | 25 | } |
@@ -13,13 +13,18 @@ |
||
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | public function decimal($num, $decimals) { |
| 16 | - if (!is_numeric($num)) return $num; |
|
| 16 | + if (!is_numeric($num)) { |
|
| 17 | + return $num; |
|
| 18 | + } |
|
| 17 | 19 | return number_format((float)$num, $decimals, $this->locale['decimal_separator'], $this->locale['thousands_separator']); |
| 18 | 20 | } |
| 19 | 21 | |
| 20 | 22 | public function currency($num) { |
| 21 | 23 | $num = $this->decimal($num, $this->locale['currency_decimals']); |
| 22 | - if ($this->locale['currency_position'] === 'before') return $this->locale['currency'] . $num; |
|
| 23 | - else return $num . $this->locale['currency']; |
|
| 24 | + if ($this->locale['currency_position'] === 'before') { |
|
| 25 | + return $this->locale['currency'] . $num; |
|
| 26 | + } else { |
|
| 27 | + return $num . $this->locale['currency']; |
|
| 28 | + } |
|
| 24 | 29 | } |
| 25 | 30 | } |
@@ -9,9 +9,13 @@ |
||
| 9 | 9 | class PostProcess implements \Transphporm\Hook { |
| 10 | 10 | public function run(\DomElement $element) { |
| 11 | 11 | $transphporm = $element->getAttribute('transphporm'); |
| 12 | - if ($transphporm === 'remove') $element->parentNode->removeChild($element); |
|
| 13 | - else if ($transphporm === 'text') $element->parentNode->replaceChild($element->firstChild, $element); |
|
| 14 | - else $element->removeAttribute('transphporm'); |
|
| 12 | + if ($transphporm === 'remove') { |
|
| 13 | + $element->parentNode->removeChild($element); |
|
| 14 | + } else if ($transphporm === 'text') { |
|
| 15 | + $element->parentNode->replaceChild($element->firstChild, $element); |
|
| 16 | + } else { |
|
| 17 | + $element->removeAttribute('transphporm'); |
|
| 18 | + } |
|
| 15 | 19 | } |
| 16 | 20 | |
| 17 | 21 | public function runOnImmutableElements(): bool { |
@@ -9,13 +9,13 @@ |
||
| 9 | 9 | private $functionSet; |
| 10 | 10 | private $elementData; |
| 11 | 11 | private $line; |
| 12 | - private $filePath; |
|
| 12 | + private $filePath; |
|
| 13 | 13 | |
| 14 | 14 | public function __construct(\Transphporm\FunctionSet $functionSet, \Transphporm\Hook\ElementData $elementData, &$line, \Transphporm\FilePath $filePath) { |
| 15 | 15 | $this->functionSet = $functionSet; |
| 16 | 16 | $this->elementData = $elementData; |
| 17 | 17 | $this->line = &$line; |
| 18 | - $this->filePath = $filePath; |
|
| 18 | + $this->filePath = $filePath; |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | |
@@ -22,7 +22,9 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
| 24 | 24 | $values = $this->fixEmpty($values); |
| 25 | - if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element); |
|
| 25 | + if ($element->getAttribute('transphporm') === 'added') { |
|
| 26 | + return $element->parentNode->removeChild($element); |
|
| 27 | + } |
|
| 26 | 28 | $this->handleContentModeAppend($element, $rules); |
| 27 | 29 | $max = $this->getMax($values); |
| 28 | 30 | $count = 0; |
@@ -36,10 +38,14 @@ discard block |
||
| 36 | 38 | |
| 37 | 39 | |
| 38 | 40 | foreach ($repeat as $key => $iteration) { |
| 39 | - if ($count+1 > $max) break; |
|
| 41 | + if ($count+1 > $max) { |
|
| 42 | + break; |
|
| 43 | + } |
|
| 40 | 44 | $clone = $this->cloneElement($element, $iteration, $key, $count++); |
| 41 | 45 | //Re-run the hook on the new element, but use the iterated data |
| 42 | - if ($hook) $hook->run($clone); |
|
| 46 | + if ($hook) { |
|
| 47 | + $hook->run($clone); |
|
| 48 | + } |
|
| 43 | 49 | } |
| 44 | 50 | |
| 45 | 51 | //Remove the original element |
@@ -73,7 +79,9 @@ discard block |
||
| 73 | 79 | } |
| 74 | 80 | |
| 75 | 81 | private function fixEmpty($value) { |
| 76 | - if (empty($value[0])) $value[0] = []; |
|
| 82 | + if (empty($value[0])) { |
|
| 83 | + $value[0] = []; |
|
| 84 | + } |
|
| 77 | 85 | return $value; |
| 78 | 86 | } |
| 79 | 87 | |
@@ -89,7 +97,9 @@ discard block |
||
| 89 | 97 | |
| 90 | 98 | private function tagElement($element, $count) { |
| 91 | 99 | //Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed |
| 92 | - if ($count > 0) $element->setAttribute('transphporm', 'added'); |
|
| 100 | + if ($count > 0) { |
|
| 101 | + $element->setAttribute('transphporm', 'added'); |
|
| 102 | + } |
|
| 93 | 103 | } |
| 94 | 104 | |
| 95 | 105 | private function getMax($values) { |
@@ -101,10 +111,14 @@ discard block |
||
| 101 | 111 | // for this rule e.g. repeat: data(); content: "foo" |
| 102 | 112 | // The content property still needs to be used |
| 103 | 113 | // But for rules that are just { repeat: data(); } this can be skipped. |
| 104 | - if (empty($newRules)) return false; |
|
| 114 | + if (empty($newRules)) { |
|
| 115 | + return false; |
|
| 116 | + } |
|
| 105 | 117 | |
| 106 | 118 | $hook = new \Transphporm\Hook\PropertyHook($newRules, $this->line, null, $this->line, $pseudoMatcher, new \Transphporm\Parser\Value($this->functionSet), $this->functionSet, $this->filePath); |
| 107 | - foreach ($properties as $name => $property) $hook->registerProperty($name, $property); |
|
| 119 | + foreach ($properties as $name => $property) { |
|
| 120 | + $hook->registerProperty($name, $property); |
|
| 121 | + } |
|
| 108 | 122 | return $hook; |
| 109 | 123 | } |
| 110 | 124 | } |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | public function &getLine() { |
| 43 | - $line = &$this->line; |
|
| 43 | + $line = &$this->line; |
|
| 44 | 44 | return $line; |
| 45 | 45 | } |
| 46 | 46 | |
@@ -68,9 +68,9 @@ discard block |
||
| 68 | 68 | $this->properties[$name] = $property; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - public function registerContentPseudo($name, Property\ContentPseudo $pseudo) { |
|
| 72 | - if (isset($this->properties['content'])) $this->properties['content']->addContentPseudo($name, $pseudo); |
|
| 73 | - } |
|
| 71 | + public function registerContentPseudo($name, Property\ContentPseudo $pseudo) { |
|
| 72 | + if (isset($this->properties['content'])) $this->properties['content']->addContentPseudo($name, $pseudo); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | 75 | public function registerPseudo($name, Pseudo $pseudo) { |
| 76 | 76 | $this->pseudo[$name] = $pseudo; |
@@ -69,7 +69,9 @@ discard block |
||
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | public function registerContentPseudo($name, Property\ContentPseudo $pseudo) { |
| 72 | - if (isset($this->properties['content'])) $this->properties['content']->addContentPseudo($name, $pseudo); |
|
| 72 | + if (isset($this->properties['content'])) { |
|
| 73 | + $this->properties['content']->addContentPseudo($name, $pseudo); |
|
| 74 | + } |
|
| 73 | 75 | } |
| 74 | 76 | |
| 75 | 77 | public function registerPseudo($name, Pseudo $pseudo) { |
@@ -77,12 +79,16 @@ discard block |
||
| 77 | 79 | } |
| 78 | 80 | |
| 79 | 81 | public function loadProperties(Hook\PropertyHook $hook) { |
| 80 | - foreach ($this->properties as $name => $property) $hook->registerProperty($name, $property); |
|
| 82 | + foreach ($this->properties as $name => $property) { |
|
| 83 | + $hook->registerProperty($name, $property); |
|
| 84 | + } |
|
| 81 | 85 | } |
| 82 | 86 | |
| 83 | 87 | public function createPseudoMatcher($pseudo) { |
| 84 | 88 | $pseudoMatcher = new Hook\PseudoMatcher($pseudo, $this->valueParser); |
| 85 | - foreach ($this->pseudo as $name => $pseudoFunction) $pseudoMatcher->registerFunction($name, clone $pseudoFunction); |
|
| 89 | + foreach ($this->pseudo as $name => $pseudoFunction) { |
|
| 90 | + $pseudoMatcher->registerFunction($name, clone $pseudoFunction); |
|
| 91 | + } |
|
| 86 | 92 | return $pseudoMatcher; |
| 87 | 93 | } |
| 88 | 94 | |
@@ -7,7 +7,9 @@ |
||
| 7 | 7 | namespace Transphporm\Pseudo; |
| 8 | 8 | class Attribute implements \Transphporm\Pseudo { |
| 9 | 9 | public function match($name, $args, \DomElement $element) { |
| 10 | - if ($name === null) return true; |
|
| 10 | + if ($name === null) { |
|
| 11 | + return true; |
|
| 12 | + } |
|
| 11 | 13 | return $args[0]; |
| 12 | 14 | } |
| 13 | 15 | } |
@@ -25,8 +25,12 @@ discard block |
||
| 25 | 25 | public function matches($element) { |
| 26 | 26 | foreach ($this->pseudo as $i => $tokens) { |
| 27 | 27 | $parts = $this->getFuncParts($i, $tokens); |
| 28 | - if ($parts['name'] === null) $parts['name'] = 'data'; |
|
| 29 | - if (!isset($this->functions[$parts['name']])) return true; |
|
| 28 | + if ($parts['name'] === null) { |
|
| 29 | + $parts['name'] = 'data'; |
|
| 30 | + } |
|
| 31 | + if (!isset($this->functions[$parts['name']])) { |
|
| 32 | + return true; |
|
| 33 | + } |
|
| 30 | 34 | if ($this->match($parts, $this->functions[$parts['name']], $element) === false) { |
| 31 | 35 | return false; |
| 32 | 36 | } |
@@ -38,14 +42,17 @@ discard block |
||
| 38 | 42 | private function match($parts, $function, $element) { |
| 39 | 43 | try { |
| 40 | 44 | $matches = $function->match($parts['name'], $parts['args'], $element); |
| 41 | - if ($matches === false) return false; |
|
| 42 | - } |
|
| 43 | - catch (\Exception $e) { |
|
| 45 | + if ($matches === false) { |
|
| 46 | + return false; |
|
| 47 | + } |
|
| 48 | + } catch (\Exception $e) { |
|
| 44 | 49 | throw new \Transphporm\RunException(\Transphporm\Exception::PSEUDO, $parts['name'], $e); |
| 45 | 50 | } |
| 46 | 51 | } |
| 47 | 52 | private function getFuncParts($i, $tokens) { |
| 48 | - if (isset($this->funcParts[$i])) return $this->funcParts[$i]; |
|
| 53 | + if (isset($this->funcParts[$i])) { |
|
| 54 | + return $this->funcParts[$i]; |
|
| 55 | + } |
|
| 49 | 56 | $parts = []; |
| 50 | 57 | $canCache = true; |
| 51 | 58 | $parts['name'] = $this->getFuncName($tokens); |
@@ -53,37 +60,47 @@ discard block |
||
| 53 | 60 | //If the args are dynamic, it can't be cached as it may change between calls |
| 54 | 61 | $canCache = false; |
| 55 | 62 | $parts['args'] = $this->valueParser->parseTokens($tokens); |
| 56 | - } |
|
| 57 | - else if (count($tokens) > 1) { |
|
| 63 | + } else if (count($tokens) > 1) { |
|
| 58 | 64 | $tokens->rewind(); |
| 59 | 65 | $tokens->next(); |
| 60 | 66 | $this->skipWhitespace($tokens); |
| 61 | 67 | $parts['args'] = $this->valueParser->parseTokens($tokens->current()['value']); |
| 68 | + } else { |
|
| 69 | + $parts['args'] = [['']]; |
|
| 70 | + } |
|
| 71 | + if ($canCache) { |
|
| 72 | + $this->funcParts[$i] = $parts; |
|
| 62 | 73 | } |
| 63 | - else $parts['args'] = [['']]; |
|
| 64 | - if ($canCache) $this->funcParts[$i] = $parts; |
|
| 65 | 74 | return $parts; |
| 66 | 75 | } |
| 67 | 76 | |
| 68 | 77 | private function skipWhitespace($tokens) { |
| 69 | - while ($tokens->current()['type'] === 'WHITESPACE' || $tokens->current()['type'] == 'NEWLINE') $tokens->next(); |
|
| 78 | + while ($tokens->current()['type'] === 'WHITESPACE' || $tokens->current()['type'] == 'NEWLINE') { |
|
| 79 | + $tokens->next(); |
|
| 80 | + } |
|
| 70 | 81 | } |
| 71 | 82 | |
| 72 | 83 | private function getFuncName($tokens) { |
| 73 | - if ($tokens->type() === Tokenizer::NAME) return $tokens->read(); |
|
| 84 | + if ($tokens->type() === Tokenizer::NAME) { |
|
| 85 | + return $tokens->read(); |
|
| 86 | + } |
|
| 74 | 87 | return null; |
| 75 | 88 | } |
| 76 | 89 | |
| 77 | 90 | public function hasFunction($name) { |
| 78 | 91 | foreach ($this->pseudo as $tokens) { |
| 79 | - if ($name === $this->getFuncName($tokens)) return true; |
|
| 92 | + if ($name === $this->getFuncName($tokens)) { |
|
| 93 | + return true; |
|
| 94 | + } |
|
| 80 | 95 | } |
| 81 | 96 | } |
| 82 | 97 | |
| 83 | 98 | public function getFuncArgs($name) { |
| 84 | 99 | foreach ($this->pseudo as $i => $tokens) { |
| 85 | 100 | $parts = $this->getFuncParts($i, $tokens); |
| 86 | - if ($name === $parts['name']) return $parts['args']; |
|
| 101 | + if ($name === $parts['name']) { |
|
| 102 | + return $parts['args']; |
|
| 103 | + } |
|
| 87 | 104 | } |
| 88 | 105 | } |
| 89 | 106 | } |
@@ -36,8 +36,12 @@ discard block |
||
| 36 | 36 | $n--; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - if ($n == 0) return false; |
|
| 40 | - if (in_array($str->read($n), ['(', "\n", ' ', '['])) return true; |
|
| 39 | + if ($n == 0) { |
|
| 40 | + return false; |
|
| 41 | + } |
|
| 42 | + if (in_array($str->read($n), ['(', "\n", ' ', '['])) { |
|
| 43 | + return true; |
|
| 44 | + } |
|
| 41 | 45 | |
| 42 | 46 | return false; |
| 43 | 47 | } |
@@ -51,9 +55,13 @@ discard block |
||
| 51 | 55 | } |
| 52 | 56 | |
| 53 | 57 | private function processLiterals($tokens, $name, $str) { |
| 54 | - if (is_numeric($name)) $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]); |
|
| 55 | - else if (method_exists($this, $name)) $this->$name($tokens); |
|
| 56 | - else $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]); |
|
| 58 | + if (is_numeric($name)) { |
|
| 59 | + $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]); |
|
| 60 | + } else if (method_exists($this, $name)) { |
|
| 61 | + $this->$name($tokens); |
|
| 62 | + } else { |
|
| 63 | + $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]); |
|
| 64 | + } |
|
| 57 | 65 | } |
| 58 | 66 | |
| 59 | 67 | private function true($tokens) { |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | $templateFunction = new \Transphporm\TSSFunction\Template($config->getElementData(), $config->getCssToXpath(), $baseDir); |
| 22 | 22 | $functionSet->addFunction('template', $templateFunction); |
| 23 | 23 | $functionSet->addFunction('json', new \Transphporm\TSSFunction\Json($baseDir)); |
| 24 | - $functionSet->addFunction('constant', new \Transphporm\TSSFunction\Constant()); |
|
| 24 | + $functionSet->addFunction('constant', new \Transphporm\TSSFunction\Constant()); |
|
| 25 | 25 | $functionSet->addFunction('file', new \Transphporm\TSSFunction\File($baseDir)); |
| 26 | 26 | |
| 27 | 27 | // Register HTML formatter here because it uses the template function |
@@ -7,30 +7,30 @@ |
||
| 7 | 7 | |
| 8 | 8 | class File implements \Transphporm\TSSFunction |
| 9 | 9 | { |
| 10 | - private $filePath; |
|
| 10 | + private $filePath; |
|
| 11 | 11 | |
| 12 | - public function __construct(\Transphporm\FilePath $filePath) |
|
| 13 | - { |
|
| 14 | - $this->filePath = $filePath; |
|
| 15 | - } |
|
| 12 | + public function __construct(\Transphporm\FilePath $filePath) |
|
| 13 | + { |
|
| 14 | + $this->filePath = $filePath; |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @param array $args |
|
| 19 | - * @param \DomElement|null $element |
|
| 20 | - * |
|
| 21 | - * @return array |
|
| 22 | - * @throws \Exception |
|
| 23 | - */ |
|
| 24 | - public function run(array $args, \DomElement $element = null) |
|
| 25 | - { |
|
| 26 | - $fileContents = $args[0]; |
|
| 17 | + /** |
|
| 18 | + * @param array $args |
|
| 19 | + * @param \DomElement|null $element |
|
| 20 | + * |
|
| 21 | + * @return array |
|
| 22 | + * @throws \Exception |
|
| 23 | + */ |
|
| 24 | + public function run(array $args, \DomElement $element = null) |
|
| 25 | + { |
|
| 26 | + $fileContents = $args[0]; |
|
| 27 | 27 | |
| 28 | - $path = $this->filePath->getFilePath($fileContents); |
|
| 29 | - if (!file_exists($path)) { |
|
| 30 | - throw new \Exception('File does not exist at: ' . $path); |
|
| 31 | - } |
|
| 32 | - $fileContents = file_get_contents($path); |
|
| 28 | + $path = $this->filePath->getFilePath($fileContents); |
|
| 29 | + if (!file_exists($path)) { |
|
| 30 | + throw new \Exception('File does not exist at: ' . $path); |
|
| 31 | + } |
|
| 32 | + $fileContents = file_get_contents($path); |
|
| 33 | 33 | |
| 34 | - return $fileContents; |
|
| 35 | - } |
|
| 34 | + return $fileContents; |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -27,7 +27,7 @@ |
||
| 27 | 27 | |
| 28 | 28 | $path = $this->filePath->getFilePath($fileContents); |
| 29 | 29 | if (!file_exists($path)) { |
| 30 | - throw new \Exception('File does not exist at: ' . $path); |
|
| 30 | + throw new \Exception('File does not exist at: '.$path); |
|
| 31 | 31 | } |
| 32 | 32 | $fileContents = file_get_contents($path); |
| 33 | 33 | |