@@ -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 = []; |
@@ -33,8 +33,9 @@ discard block |
||
| 33 | 33 | $params = $bracketMatcher->match('(', ')'); |
| 34 | 34 | |
| 35 | 35 | return ['name' => $name, 'params' => $params, 'endPoint' => $bracketMatcher->getClosePos()]; |
| 36 | + } else { |
|
| 37 | + return ['name' => null, 'params' => $function, 'endPoint' => strlen($function)]; |
|
| 36 | 38 | } |
| 37 | - else return ['name' => null, 'params' => $function, 'endPoint' => strlen($function)]; |
|
| 38 | 39 | } |
| 39 | 40 | |
| 40 | 41 | public function parse($function, \DomElement $element) { |
@@ -55,13 +56,15 @@ discard block |
||
| 55 | 56 | if ($function && in_array($function[0], ['\'', '"'])) { |
| 56 | 57 | $finalPos = $this->findMatchingPos($function, $function[0]); |
| 57 | 58 | $result[] = $this->extractQuotedString($function[0], $function); |
| 58 | - } |
|
| 59 | - else { |
|
| 59 | + } else { |
|
| 60 | 60 | $func = $this->parseFunction($function); |
| 61 | 61 | $finalPos = $func['endPoint']; |
| 62 | 62 | |
| 63 | - if (($data = $this->getFunctionValue($func['name'], $func['params'], $element)) !== self::IS_NOT_FUNCTION) $result = $this->appendToArray($result, $data); |
|
| 64 | - else $result[] = trim($function); |
|
| 63 | + if (($data = $this->getFunctionValue($func['name'], $func['params'], $element)) !== self::IS_NOT_FUNCTION) { |
|
| 64 | + $result = $this->appendToArray($result, $data); |
|
| 65 | + } else { |
|
| 66 | + $result[] = trim($function); |
|
| 67 | + } |
|
| 65 | 68 | } |
| 66 | 69 | $remaining = trim(substr($function, $finalPos+1)); |
| 67 | 70 | return $this->parseNextValue($remaining, $result, $element); |
@@ -70,23 +73,27 @@ discard block |
||
| 70 | 73 | private function getFunctionValue($name, $params, $element) { |
| 71 | 74 | if (($data = $this->callFunc($name, $params, $element)) !== self::IS_NOT_FUNCTION) { |
| 72 | 75 | return $data; |
| 73 | - } |
|
| 74 | - else if ($this->parent != null && ($data = $this->parent->callFunc($name, $params, $element)) !== self::IS_NOT_FUNCTION) { |
|
| 76 | + } else if ($this->parent != null && ($data = $this->parent->callFunc($name, $params, $element)) !== self::IS_NOT_FUNCTION) { |
|
| 75 | 77 | return $data; |
| 78 | + } else { |
|
| 79 | + return self::IS_NOT_FUNCTION; |
|
| 76 | 80 | } |
| 77 | - else return self::IS_NOT_FUNCTION; |
|
| 78 | 81 | } |
| 79 | 82 | |
| 80 | 83 | private function appendToArray($array, $value) { |
| 81 | - if (is_array($value)) $array += $value; |
|
| 82 | - else $array[] = $value; |
|
| 84 | + if (is_array($value)) { |
|
| 85 | + $array += $value; |
|
| 86 | + } else { |
|
| 87 | + $array[] = $value; |
|
| 88 | + } |
|
| 83 | 89 | return $array; |
| 84 | 90 | } |
| 85 | 91 | |
| 86 | 92 | private function callFunc($name, $params, $element) { |
| 87 | 93 | if ($name && $this->isCallable($this->dataFunction, $name)) { |
| 88 | - if ($this->callParamsAsArray) return $this->dataFunction->$name($this->parse($params, $element), $element); |
|
| 89 | - else { |
|
| 94 | + if ($this->callParamsAsArray) { |
|
| 95 | + return $this->dataFunction->$name($this->parse($params, $element), $element); |
|
| 96 | + } else { |
|
| 90 | 97 | return $this->callFuncOnObject($this->dataFunction, $name, $this->parse($params, $element)); |
| 91 | 98 | } |
| 92 | 99 | } |
@@ -103,18 +110,25 @@ discard block |
||
| 103 | 110 | foreach ($params as $param) { |
| 104 | 111 | $stringExtractor = new StringExtractor($param); |
| 105 | 112 | $parts = explode(',', $stringExtractor); |
| 106 | - foreach ($parts as $part) $args[] = $stringExtractor->rebuild($part); |
|
| 113 | + foreach ($parts as $part) { |
|
| 114 | + $args[] = $stringExtractor->rebuild($part); |
|
| 115 | + } |
|
| 107 | 116 | } |
| 108 | 117 | return $this->callFuncOrClosure($obj, $func, $args); |
| 109 | 118 | } |
| 110 | 119 | |
| 111 | 120 | private function callFuncOrClosure($obj, $func, $args) { |
| 112 | - if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args); |
|
| 113 | - else return call_user_func_array([$obj, $func], $args); |
|
| 121 | + if (isset($obj->$func) && is_callable($obj->$func)) { |
|
| 122 | + return call_user_func_array($obj->$func, $args); |
|
| 123 | + } else { |
|
| 124 | + return call_user_func_array([$obj, $func], $args); |
|
| 125 | + } |
|
| 114 | 126 | } |
| 115 | 127 | |
| 116 | 128 | private function parseNextValue($remaining, $result, $element) { |
| 117 | - if (strlen($remaining) > 0 && $remaining[0] == ',') $result = array_merge($result, $this->parse(trim(substr($remaining, 1)), $element)); |
|
| 129 | + if (strlen($remaining) > 0 && $remaining[0] == ',') { |
|
| 130 | + $result = array_merge($result, $this->parse(trim(substr($remaining, 1)), $element)); |
|
| 131 | + } |
|
| 118 | 132 | return $result; |
| 119 | 133 | } |
| 120 | 134 | |
@@ -122,8 +136,9 @@ discard block |
||
| 122 | 136 | $pos = $start+1; |
| 123 | 137 | $end = 0; |
| 124 | 138 | while ($end = strpos($string, $char, $pos)) { |
| 125 | - if ($string[$end-1] === $escape) $pos = $end+1; |
|
| 126 | - else { |
|
| 139 | + if ($string[$end-1] === $escape) { |
|
| 140 | + $pos = $end+1; |
|
| 141 | + } else { |
|
| 127 | 142 | break; |
| 128 | 143 | } |
| 129 | 144 | } |