@@ -11,6 +11,9 @@ discard block |
||
| 11 | 11 | private $data; |
| 12 | 12 | private $baseDir; |
| 13 | 13 | |
| 14 | + /** |
|
| 15 | + * @param string $baseDir |
|
| 16 | + */ |
|
| 14 | 17 | public function __construct(\SplObjectStorage $objectStorage, $data, $baseDir) { |
| 15 | 18 | $this->dataStorage = $objectStorage; |
| 16 | 19 | $this->data = $data; |
@@ -74,6 +77,9 @@ discard block |
||
| 74 | 77 | return $obj; |
| 75 | 78 | } |
| 76 | 79 | |
| 80 | + /** |
|
| 81 | + * @param \Transphporm\Parser\Value $valueParser |
|
| 82 | + */ |
|
| 77 | 83 | private function processNestedFunc($part, $obj, $valueParser, $element) { |
| 78 | 84 | if (strpos($part, '(') !== false) { |
| 79 | 85 | $subObjParser = new \Transphporm\Parser\Value($obj, $valueParser, false); |
@@ -120,7 +120,7 @@ |
||
| 120 | 120 | public function template($val, $element) { |
| 121 | 121 | $tssToApply = $this->getTss($val); |
| 122 | 122 | |
| 123 | - $newTemplate = new \Transphporm\Builder($this->baseDir . $val[0], $this->baseDir . $tssToApply); |
|
| 123 | + $newTemplate = new \Transphporm\Builder($this->baseDir.$val[0], $this->baseDir.$tssToApply); |
|
| 124 | 124 | $data = $this->getData($element); |
| 125 | 125 | $doc = $newTemplate->output($data, true)->body; |
| 126 | 126 | if (!empty($val[1])) return $this->templateSubsection($val[1], $doc, $element); |
@@ -24,7 +24,9 @@ discard block |
||
| 24 | 24 | /** Binds data to an element */ |
| 25 | 25 | public function bind(\DomNode $element, $data, $type = 'data') { |
| 26 | 26 | //This is a bit of a hack to workaround #24, might need a better way of doing this if it causes a problem |
| 27 | - if (is_array($data) && $this->isObjectArray($data)) $data = $data[0]; |
|
| 27 | + if (is_array($data) && $this->isObjectArray($data)) { |
|
| 28 | + $data = $data[0]; |
|
| 29 | + } |
|
| 28 | 30 | $content = isset($this->dataStorage[$element]) ? $this->dataStorage[$element] : []; |
| 29 | 31 | $content[$type] = $data; |
| 30 | 32 | $this->dataStorage[$element] = $content; |
@@ -48,7 +50,9 @@ discard block |
||
| 48 | 50 | /** Returns the data that has been bound to $element, or, if no data is bound to $element climb the DOM tree to find the data bound to a parent node*/ |
| 49 | 51 | public function getData(\DomElement $element = null, $type = 'data') { |
| 50 | 52 | while ($element) { |
| 51 | - if (isset($this->dataStorage[$element]) && isset($this->dataStorage[$element][$type])) return $this->dataStorage[$element][$type]; |
|
| 53 | + if (isset($this->dataStorage[$element]) && isset($this->dataStorage[$element][$type])) { |
|
| 54 | + return $this->dataStorage[$element][$type]; |
|
| 55 | + } |
|
| 52 | 56 | $element = $element->parentNode; |
| 53 | 57 | } |
| 54 | 58 | return $this->data; |
@@ -67,13 +71,17 @@ discard block |
||
| 67 | 71 | $valueParser = new \Transphporm\Parser\Value($this); |
| 68 | 72 | |
| 69 | 73 | foreach ($parts as $part) { |
| 70 | - if ($part === '') continue; |
|
| 74 | + if ($part === '') { |
|
| 75 | + continue; |
|
| 76 | + } |
|
| 71 | 77 | $part = $valueParser->parse($part, $element)[0]; |
| 72 | 78 | $funcResult = $this->traverseObj($part, $obj, $valueParser, $element); |
| 73 | 79 | |
| 74 | - if ($funcResult !== false) $obj = $funcResult; |
|
| 75 | - |
|
| 76 | - else $obj = $this->ifNull($obj, $part); |
|
| 80 | + if ($funcResult !== false) { |
|
| 81 | + $obj = $funcResult; |
|
| 82 | + } else { |
|
| 83 | + $obj = $this->ifNull($obj, $part); |
|
| 84 | + } |
|
| 77 | 85 | } |
| 78 | 86 | return $obj; |
| 79 | 87 | } |
@@ -82,14 +90,19 @@ discard block |
||
| 82 | 90 | if (strpos($part, '(') !== false) { |
| 83 | 91 | $subObjParser = new \Transphporm\Parser\Value($obj, $valueParser, false); |
| 84 | 92 | return $subObjParser->parse($part, $element)[0]; |
| 93 | + } else if (method_exists($obj, $part)) { |
|
| 94 | + return call_user_func([$obj, $part]); |
|
| 95 | + } else { |
|
| 96 | + return false; |
|
| 85 | 97 | } |
| 86 | - else if (method_exists($obj, $part)) return call_user_func([$obj, $part]); |
|
| 87 | - else return false; |
|
| 88 | 98 | } |
| 89 | 99 | |
| 90 | 100 | private function ifNull($obj, $key) { |
| 91 | - if (is_array($obj)) return isset($obj[$key]) ? $obj[$key] : null; |
|
| 92 | - else return isset($obj->$key) ? $obj->$key : null; |
|
| 101 | + if (is_array($obj)) { |
|
| 102 | + return isset($obj[$key]) ? $obj[$key] : null; |
|
| 103 | + } else { |
|
| 104 | + return isset($obj->$key) ? $obj->$key : null; |
|
| 105 | + } |
|
| 93 | 106 | } |
| 94 | 107 | |
| 95 | 108 | public function attr($val, $element) { |
@@ -113,7 +126,9 @@ discard block |
||
| 113 | 126 | |
| 114 | 127 | private function getClonedElement($node, $tss) { |
| 115 | 128 | $clone = $node->cloneNode(true); |
| 116 | - if ($tss !== null && $clone instanceof \DomElement) $clone->setAttribute('transphporm', 'includedtemplate'); |
|
| 129 | + if ($tss !== null && $clone instanceof \DomElement) { |
|
| 130 | + $clone->setAttribute('transphporm', 'includedtemplate'); |
|
| 131 | + } |
|
| 117 | 132 | return $clone; |
| 118 | 133 | } |
| 119 | 134 | |
@@ -123,7 +138,9 @@ discard block |
||
| 123 | 138 | $newTemplate = new \Transphporm\Builder($this->baseDir . $val[0], $this->baseDir . $tssToApply); |
| 124 | 139 | $data = $this->getData($element); |
| 125 | 140 | $doc = $newTemplate->output($data, true)->body; |
| 126 | - if (!empty($val[1])) return $this->templateSubsection($val[1], $doc, $element); |
|
| 141 | + if (!empty($val[1])) { |
|
| 142 | + return $this->templateSubsection($val[1], $doc, $element); |
|
| 143 | + } |
|
| 127 | 144 | |
| 128 | 145 | $newNode = $element->ownerDocument->importNode($doc->documentElement, true); |
| 129 | 146 | $result = []; |
@@ -23,8 +23,8 @@ |
||
| 23 | 23 | $end = strpos($str, '"', $pos+1); |
| 24 | 24 | if (!$end) break; |
| 25 | 25 | while ($str[$end-1] == '\\') $end = strpos($str, '"', $end+1); |
| 26 | - $strings['$___STR' . ++$num] = substr($str, $pos, $end-$pos+1); |
|
| 27 | - $str = substr_replace($str, '$___STR' . $num, $pos, $end-$pos+1); |
|
| 26 | + $strings['$___STR'.++$num] = substr($str, $pos, $end-$pos+1); |
|
| 27 | + $str = substr_replace($str, '$___STR'.$num, $pos, $end-$pos+1); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | return [$str, $strings]; |
@@ -21,7 +21,9 @@ discard block |
||
| 21 | 21 | $strings = []; |
| 22 | 22 | while (isset($str[$pos]) && ($pos = strpos($str, '"', $pos)) !== false) { |
| 23 | 23 | $end = strpos($str, '"', $pos+1); |
| 24 | - if (!$end) break; |
|
| 24 | + if (!$end) { |
|
| 25 | + break; |
|
| 26 | + } |
|
| 25 | 27 | $end = $this->getNextPosEscaped($str, '\\', '"', $end); |
| 26 | 28 | $strings['$___STR' . ++$num] = substr($str, $pos, $end-$pos+1); |
| 27 | 29 | $str = substr_replace($str, '$___STR' . $num, $pos, $end-$pos+1); |
@@ -31,7 +33,9 @@ discard block |
||
| 31 | 33 | } |
| 32 | 34 | |
| 33 | 35 | private function getNextPosEscaped($str, $escape, $chr, $start) { |
| 34 | - while ($str[$start-1] == $escape) $start = strpos($str, $chr, $start+1); |
|
| 36 | + while ($str[$start-1] == $escape) { |
|
| 37 | + $start = strpos($str, $chr, $start+1); |
|
| 38 | + } |
|
| 35 | 39 | return $start; |
| 36 | 40 | } |
| 37 | 41 | |
@@ -14,8 +14,8 @@ |
||
| 14 | 14 | |
| 15 | 15 | private function getLocale() { |
| 16 | 16 | if (is_array($this->locale)) return $this->locale; |
| 17 | - 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); |
|
| 18 | - else return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . 'enGB.json'), true); |
|
| 17 | + 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); |
|
| 18 | + else return json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'../Formatter'.DIRECTORY_SEPARATOR.'Locale'.DIRECTORY_SEPARATOR.'enGB.json'), true); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | public function load(\Transphporm\FeatureSet $featureSet) { |
@@ -13,9 +13,13 @@ |
||
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | private function getLocale() { |
| 16 | - if (is_array($this->locale)) return $this->locale; |
|
| 17 | - 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); |
|
| 18 | - else return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . 'enGB.json'), true); |
|
| 16 | + if (is_array($this->locale)) { |
|
| 17 | + return $this->locale; |
|
| 18 | + } else if (strlen($this->locale) > 0) { |
|
| 19 | + return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . $this->locale . '.json'), true); |
|
| 20 | + } else { |
|
| 21 | + return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . 'enGB.json'), true); |
|
| 22 | + } |
|
| 19 | 23 | } |
| 20 | 24 | |
| 21 | 25 | public function load(\Transphporm\FeatureSet $featureSet) { |
@@ -11,6 +11,9 @@ discard block |
||
| 11 | 11 | private $formatter; |
| 12 | 12 | |
| 13 | 13 | |
| 14 | + /** |
|
| 15 | + * @param \Transphporm\Hook\DataFunction $data |
|
| 16 | + */ |
|
| 14 | 17 | public function __construct($data, &$headers, \Transphporm\Hook\Formatter $formatter) { |
| 15 | 18 | $this->data = $data; |
| 16 | 19 | $this->headers = &$headers; |
@@ -30,6 +33,9 @@ discard block |
||
| 30 | 33 | } |
| 31 | 34 | } |
| 32 | 35 | |
| 36 | + /** |
|
| 37 | + * @param \DOMElement $element |
|
| 38 | + */ |
|
| 33 | 39 | private function shouldRun($element) { |
| 34 | 40 | if ($element->getAttribute('transphporm') === 'remove') return false; |
| 35 | 41 | |
@@ -44,6 +50,10 @@ discard block |
||
| 44 | 50 | return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append'; |
| 45 | 51 | } |
| 46 | 52 | |
| 53 | + /** |
|
| 54 | + * @param \DOMElement $element |
|
| 55 | + * @param \Transphporm\Hook\PseudoMatcher $pseudoMatcher |
|
| 56 | + */ |
|
| 47 | 57 | private function processPseudo($value, $element, $pseudoMatcher) { |
| 48 | 58 | $pseudoContent = ['attr', 'header', 'before', 'after']; |
| 49 | 59 | foreach ($pseudoContent as $pseudo) { |
@@ -101,6 +111,9 @@ discard block |
||
| 101 | 111 | foreach ($remove as $r) $r->parentNode->removeChild($r); |
| 102 | 112 | } |
| 103 | 113 | |
| 114 | + /** |
|
| 115 | + * @param \DOMElement $element |
|
| 116 | + */ |
|
| 104 | 117 | private function replaceContent($element, $content) { |
| 105 | 118 | //If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. |
| 106 | 119 | $this->removeAdded($element); |
@@ -110,12 +123,18 @@ discard block |
||
| 110 | 123 | $element->setAttribute('transphporm', 'remove'); |
| 111 | 124 | } |
| 112 | 125 | |
| 126 | + /** |
|
| 127 | + * @param \DOMElement $element |
|
| 128 | + */ |
|
| 113 | 129 | private function appendContent($element, $content) { |
| 114 | 130 | foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
| 115 | 131 | $element->appendChild($node); |
| 116 | 132 | } |
| 117 | 133 | } |
| 118 | 134 | |
| 135 | + /** |
|
| 136 | + * @param \DOMElement $element |
|
| 137 | + */ |
|
| 119 | 138 | private function removeAllChildren($element) { |
| 120 | 139 | while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
| 121 | 140 | } |
@@ -18,22 +18,31 @@ discard block |
||
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | public function run($value, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
| 21 | - if ($this->isIncludedTemplate($element)) return false; |
|
| 22 | - if ($element->getAttribute('transphporm') === 'remove') return false; |
|
| 21 | + if ($this->isIncludedTemplate($element)) { |
|
| 22 | + return false; |
|
| 23 | + } |
|
| 24 | + if ($element->getAttribute('transphporm') === 'remove') { |
|
| 25 | + return false; |
|
| 26 | + } |
|
| 23 | 27 | |
| 24 | 28 | $value = $this->formatter->format($value, $rules); |
| 25 | 29 | if (!$this->processPseudo($value, $element, $pseudoMatcher)) { |
| 26 | 30 | //Remove the current contents |
| 27 | 31 | $this->removeAllChildren($element); |
| 28 | 32 | //Now make a text node |
| 29 | - if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $value); |
|
| 30 | - else $this->appendContent($element, $value); |
|
| 33 | + if ($this->getContentMode($rules) === 'replace') { |
|
| 34 | + $this->replaceContent($element, $value); |
|
| 35 | + } else { |
|
| 36 | + $this->appendContent($element, $value); |
|
| 37 | + } |
|
| 31 | 38 | } |
| 32 | 39 | } |
| 33 | 40 | |
| 34 | 41 | private function isIncludedTemplate($element) { |
| 35 | 42 | do { |
| 36 | - if ($element->getAttribute('transphporm') == 'includedtemplate') return true; |
|
| 43 | + if ($element->getAttribute('transphporm') == 'includedtemplate') { |
|
| 44 | + return true; |
|
| 45 | + } |
|
| 37 | 46 | } |
| 38 | 47 | while (($element = $element->parentNode) instanceof \DomElement); |
| 39 | 48 | return false; |
@@ -60,9 +69,10 @@ discard block |
||
| 60 | 69 | $new = $document->importNode($n, true); |
| 61 | 70 | //Removing this might cause problems with caching... |
| 62 | 71 | //$new->setAttribute('transphporm', 'added'); |
| 63 | - } |
|
| 64 | - else { |
|
| 65 | - if ($n instanceof \DomText) $n = $n->nodeValue; |
|
| 72 | + } else { |
|
| 73 | + if ($n instanceof \DomText) { |
|
| 74 | + $n = $n->nodeValue; |
|
| 75 | + } |
|
| 66 | 76 | $new = $document->createElement('text'); |
| 67 | 77 | $new->appendChild($document->createTextNode($n)); |
| 68 | 78 | $new->setAttribute('transphporm', 'text'); |
@@ -98,7 +108,9 @@ discard block |
||
| 98 | 108 | while ($e = $e->previousSibling && !in_array($e->getAttribute('transphporm'), [null, 'remove'])) { |
| 99 | 109 | $remove[] = $e; |
| 100 | 110 | } |
| 101 | - foreach ($remove as $r) $r->parentNode->removeChild($r); |
|
| 111 | + foreach ($remove as $r) { |
|
| 112 | + $r->parentNode->removeChild($r); |
|
| 113 | + } |
|
| 102 | 114 | } |
| 103 | 115 | |
| 104 | 116 | private function replaceContent($element, $content) { |
@@ -117,6 +129,8 @@ discard block |
||
| 117 | 129 | } |
| 118 | 130 | |
| 119 | 131 | private function removeAllChildren($element) { |
| 120 | - while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
|
| 132 | + while ($element->hasChildNodes()) { |
|
| 133 | + $element->removeChild($element->firstChild); |
|
| 134 | + } |
|
| 121 | 135 | } |
| 122 | 136 | } |
| 123 | 137 | \ No newline at end of file |