@@ -7,6 +7,9 @@ |
||
| 7 | 7 | $this->templateFunction = $templateFunction; |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | + /** |
|
| 11 | + * @param string $val |
|
| 12 | + */ |
|
| 10 | 13 | public function html($val) { |
| 11 | 14 | return $this->templateFunction->run(['<template>' . $val . '</template>']); |
| 12 | 15 | } |
@@ -1,13 +1,13 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace Transphporm\Formatter; |
| 3 | 3 | class HTMLFormatter { |
| 4 | - private $templateFunction; |
|
| 4 | + private $templateFunction; |
|
| 5 | 5 | |
| 6 | - public function __construct(\Transphporm\TSSFunction\Template $templateFunction) { |
|
| 7 | - $this->templateFunction = $templateFunction; |
|
| 8 | - } |
|
| 6 | + public function __construct(\Transphporm\TSSFunction\Template $templateFunction) { |
|
| 7 | + $this->templateFunction = $templateFunction; |
|
| 8 | + } |
|
| 9 | 9 | |
| 10 | - public function html($val) { |
|
| 10 | + public function html($val) { |
|
| 11 | 11 | return $this->templateFunction->run(['<template>' . $val . '</template>']); |
| 12 | 12 | } |
| 13 | 13 | |
@@ -8,12 +8,12 @@ |
||
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | public function html($val) { |
| 11 | - return $this->templateFunction->run(['<template>' . $val . '</template>']); |
|
| 11 | + return $this->templateFunction->run(['<template>'.$val.'</template>']); |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | public function debug($val) { |
| 15 | 15 | ob_start(); |
| 16 | 16 | var_dump($val); |
| 17 | - return $this->html('<pre>' . ob_get_clean() . '</pre>'); |
|
| 17 | + return $this->html('<pre>'.ob_get_clean().'</pre>'); |
|
| 18 | 18 | } |
| 19 | 19 | } |
@@ -28,6 +28,9 @@ discard block |
||
| 28 | 28 | } |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | + /** |
|
| 32 | + * @param \DOMElement $element |
|
| 33 | + */ |
|
| 31 | 34 | private function shouldRun($element) { |
| 32 | 35 | do { |
| 33 | 36 | if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
@@ -40,6 +43,10 @@ discard block |
||
| 40 | 43 | return (isset($rules['content-mode'])) ? $rules['content-mode']->read() : 'append'; |
| 41 | 44 | } |
| 42 | 45 | |
| 46 | + /** |
|
| 47 | + * @param \DOMElement $element |
|
| 48 | + * @param \Transphporm\Hook\PseudoMatcher $pseudoMatcher |
|
| 49 | + */ |
|
| 43 | 50 | private function processPseudo($value, $element, $pseudoMatcher) { |
| 44 | 51 | $pseudoContent = ['attr', 'header', 'before', 'after']; |
| 45 | 52 | foreach ($pseudoContent as $pseudo) { |
@@ -101,6 +108,9 @@ discard block |
||
| 101 | 108 | } |
| 102 | 109 | } |
| 103 | 110 | |
| 111 | + /** |
|
| 112 | + * @param \DOMElement $element |
|
| 113 | + */ |
|
| 104 | 114 | private function replaceContent($element, $content) { |
| 105 | 115 | //If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. |
| 106 | 116 | foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
@@ -109,12 +119,18 @@ discard block |
||
| 109 | 119 | $element->setAttribute('transphporm', 'remove'); |
| 110 | 120 | } |
| 111 | 121 | |
| 122 | + /** |
|
| 123 | + * @param \DOMElement $element |
|
| 124 | + */ |
|
| 112 | 125 | private function appendContent($element, $content) { |
| 113 | 126 | foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
| 114 | 127 | $element->appendChild($node); |
| 115 | 128 | } |
| 116 | 129 | } |
| 117 | 130 | |
| 131 | + /** |
|
| 132 | + * @param \DOMElement $element |
|
| 133 | + */ |
|
| 118 | 134 | private function removeAllChildren($element) { |
| 119 | 135 | while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
| 120 | 136 | } |
@@ -15,7 +15,9 @@ discard block |
||
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
| 18 | - if (!$this->shouldRun($element)) return false; |
|
| 18 | + if (!$this->shouldRun($element)) { |
|
| 19 | + return false; |
|
| 20 | + } |
|
| 19 | 21 | |
| 20 | 22 | $values = $this->formatter->format($values, $rules); |
| 21 | 23 | |
@@ -23,14 +25,19 @@ discard block |
||
| 23 | 25 | //Remove the current contents |
| 24 | 26 | $this->removeAllChildren($element); |
| 25 | 27 | //Now make a text node |
| 26 | - if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $values); |
|
| 27 | - else $this->appendContent($element, $values); |
|
| 28 | + if ($this->getContentMode($rules) === 'replace') { |
|
| 29 | + $this->replaceContent($element, $values); |
|
| 30 | + } else { |
|
| 31 | + $this->appendContent($element, $values); |
|
| 32 | + } |
|
| 28 | 33 | } |
| 29 | 34 | } |
| 30 | 35 | |
| 31 | 36 | private function shouldRun($element) { |
| 32 | 37 | do { |
| 33 | - if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
|
| 38 | + if ($element->getAttribute('transphporm') == 'includedtemplate') { |
|
| 39 | + return false; |
|
| 40 | + } |
|
| 34 | 41 | } |
| 35 | 42 | while (($element = $element->parentNode) instanceof \DomElement); |
| 36 | 43 | return true; |
@@ -54,9 +61,10 @@ discard block |
||
| 54 | 61 | private function getNode($node, $document) { |
| 55 | 62 | foreach ($node as $n) { |
| 56 | 63 | if (is_array($n)) { |
| 57 | - foreach ($this->getNode($n, $document) as $new) yield $new; |
|
| 58 | - } |
|
| 59 | - else { |
|
| 64 | + foreach ($this->getNode($n, $document) as $new) { |
|
| 65 | + yield $new; |
|
| 66 | + } |
|
| 67 | + } else { |
|
| 60 | 68 | yield $this->convertNode($n, $document); |
| 61 | 69 | } |
| 62 | 70 | } |
@@ -67,9 +75,10 @@ discard block |
||
| 67 | 75 | $new = $document->importNode($node, true); |
| 68 | 76 | //Removing this might cause problems with caching... |
| 69 | 77 | //$new->setAttribute('transphporm', 'added'); |
| 70 | - } |
|
| 71 | - else { |
|
| 72 | - if ($node instanceof \DomText) $node = $node->nodeValue; |
|
| 78 | + } else { |
|
| 79 | + if ($node instanceof \DomText) { |
|
| 80 | + $node = $node->nodeValue; |
|
| 81 | + } |
|
| 73 | 82 | $new = $document->createElement('text'); |
| 74 | 83 | |
| 75 | 84 | $new->appendChild($document->createTextNode($node)); |
@@ -116,6 +125,8 @@ discard block |
||
| 116 | 125 | } |
| 117 | 126 | |
| 118 | 127 | private function removeAllChildren($element) { |
| 119 | - while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
|
| 128 | + while ($element->hasChildNodes()) { |
|
| 129 | + $element->removeChild($element->firstChild); |
|
| 130 | + } |
|
| 120 | 131 | } |
| 121 | 132 | } |
@@ -15,8 +15,8 @@ |
||
| 15 | 15 | |
| 16 | 16 | private function getLocale() { |
| 17 | 17 | if (is_array($this->locale)) return $this->locale; |
| 18 | - else if (strlen($this->locale) > 0) return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . $this->locale . '.json'), true); |
|
| 19 | - else return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . 'enGB.json'), true); |
|
| 18 | + else if (strlen($this->locale) > 0) return json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'../Formatter'.DIRECTORY_SEPARATOR.'Locale'.DIRECTORY_SEPARATOR.$this->locale.'.json'), true); |
|
| 19 | + else return json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'../Formatter'.DIRECTORY_SEPARATOR.'Locale'.DIRECTORY_SEPARATOR.'enGB.json'), true); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | public function load(\Transphporm\Config $config) { |
@@ -14,9 +14,13 @@ |
||
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | private function getLocale() { |
| 17 | - if (is_array($this->locale)) return $this->locale; |
|
| 18 | - else if (strlen($this->locale) > 0) return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . $this->locale . '.json'), true); |
|
| 19 | - else return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . 'enGB.json'), true); |
|
| 17 | + if (is_array($this->locale)) { |
|
| 18 | + return $this->locale; |
|
| 19 | + } else if (strlen($this->locale) > 0) { |
|
| 20 | + return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . $this->locale . '.json'), true); |
|
| 21 | + } else { |
|
| 22 | + return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . 'enGB.json'), true); |
|
| 23 | + } |
|
| 20 | 24 | } |
| 21 | 25 | |
| 22 | 26 | public function load(\Transphporm\Config $config) { |
@@ -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 || in_array($name, ['data', 'iteration', 'root']))) return true; |
|
| 10 | + if (!($name === null || in_array($name, ['data', 'iteration', 'root']))) { |
|
| 11 | + return true; |
|
| 12 | + } |
|
| 11 | 13 | return $args[0]; |
| 12 | 14 | } |
| 13 | 15 | } |
@@ -21,7 +21,8 @@ |
||
| 21 | 21 | $key = md5($key); |
| 22 | 22 | if (isset($this->cache[$key]) && $this->cache[$key]['timestamp'] >= $modified) { |
| 23 | 23 | return $this->cache[$key]['content']; |
| 24 | + } else { |
|
| 25 | + return false; |
|
| 24 | 26 | } |
| 25 | - else return false; |
|
| 26 | 27 | } |
| 27 | 28 | } |
| 28 | 29 | \ No newline at end of file |
@@ -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 | } |
@@ -56,7 +56,9 @@ discard block |
||
| 56 | 56 | $this->last = null; |
| 57 | 57 | $this->traversing = false; |
| 58 | 58 | |
| 59 | - if (count($tokens) <= 0) return [$data]; |
|
| 59 | + if (count($tokens) <= 0) { |
|
| 60 | + return [$data]; |
|
| 61 | + } |
|
| 60 | 62 | |
| 61 | 63 | foreach (new TokenFilterIterator($tokens, [Tokenizer::WHITESPACE, Tokenizer::NEW_LINE]) as $token) { |
| 62 | 64 | $this->{$this->tokenFuncs[$token['type']]}($token); |
@@ -84,8 +86,7 @@ discard block |
||
| 84 | 86 | // foo.bar is treated as looking up `bar` in `foo` whereas .foo is treated as the string ".foo" |
| 85 | 87 | if ($lastResult) { |
| 86 | 88 | $this->traversing = true; |
| 87 | - } |
|
| 88 | - else if ($this->last === null) { |
|
| 89 | + } else if ($this->last === null) { |
|
| 89 | 90 | $this->processString(['value' => '.']); |
| 90 | 91 | $this->result->setMode(Tokenizer::CONCAT); |
| 91 | 92 | } |
@@ -101,11 +102,12 @@ discard block |
||
| 101 | 102 | $parser = new Value($this->baseData, $this->autoLookup); |
| 102 | 103 | if ($this->hasFunction($this->last)) { |
| 103 | 104 | $this->callTransphpormFunctions($token); |
| 104 | - } |
|
| 105 | - else { |
|
| 105 | + } else { |
|
| 106 | 106 | $this->data->traverse($this->last, $this->result); |
| 107 | 107 | $this->last = $parser->parseTokens($token['value'], null)[0]; |
| 108 | - if (!is_bool($this->last)) $this->traversing = true; |
|
| 108 | + if (!is_bool($this->last)) { |
|
| 109 | + $this->traversing = true; |
|
| 110 | + } |
|
| 109 | 111 | } |
| 110 | 112 | } |
| 111 | 113 | |
@@ -128,8 +130,7 @@ discard block |
||
| 128 | 130 | if ($this->hasFunction($this->last) |
| 129 | 131 | && !$this->data->methodExists($this->last)) { |
| 130 | 132 | $this->callTransphpormFunctions($token); |
| 131 | - } |
|
| 132 | - else { |
|
| 133 | + } else { |
|
| 133 | 134 | $this->processNested($token); |
| 134 | 135 | } |
| 135 | 136 | } |
@@ -149,8 +150,7 @@ discard block |
||
| 149 | 150 | $parser = new Value($this->data->getData()); |
| 150 | 151 | $parsedVal = $parser->parse($val)[0] ?? null; |
| 151 | 152 | $this->result->postProcess($this->data, $val, $parsedVal, $this->allowNullResult); |
| 152 | - } |
|
| 153 | - else { |
|
| 153 | + } else { |
|
| 154 | 154 | $this->result->postProcess($this->data, $val, null, $this->allowNullResult); |
| 155 | 155 | } |
| 156 | 156 | |
@@ -163,8 +163,7 @@ discard block |
||
| 163 | 163 | try { |
| 164 | 164 | $value = $this->data->extract($this->last, $this->autoLookup, $this->traversing); |
| 165 | 165 | $this->result->processValue($value); |
| 166 | - } |
|
| 167 | - catch (\UnexpectedValueException $e) { |
|
| 166 | + } catch (\UnexpectedValueException $e) { |
|
| 168 | 167 | $this->processLastUnexpected(); |
| 169 | 168 | } |
| 170 | 169 | } |
@@ -173,8 +172,7 @@ discard block |
||
| 173 | 172 | private function processLastUnexpected() { |
| 174 | 173 | if (!($this->autoLookup || $this->traversing)) { |
| 175 | 174 | $this->result->processValue($this->last); |
| 176 | - } |
|
| 177 | - else { |
|
| 175 | + } else { |
|
| 178 | 176 | $this->result->clear(); |
| 179 | 177 | $this->result->processValue(false); |
| 180 | 178 | } |