@@ -20,7 +20,9 @@ discard block |
||
| 20 | 20 | /** Binds data to an element */ |
| 21 | 21 | public function bind(\DomElement $element, $data, $type = 'data') { |
| 22 | 22 | //This is a bit of a hack to workaround #24, might need a better way of doing this if it causes a problem |
| 23 | - if (is_array($data) && $this->isObjectArray($data)) $data = $data[0]; |
|
| 23 | + if (is_array($data) && $this->isObjectArray($data)) { |
|
| 24 | + $data = $data[0]; |
|
| 25 | + } |
|
| 24 | 26 | $content = isset($this->dataStorage[$element]) ? $this->dataStorage[$element] : []; |
| 25 | 27 | $content[$type] = $data; |
| 26 | 28 | $this->dataStorage[$element] = $content; |
@@ -44,7 +46,9 @@ discard block |
||
| 44 | 46 | /** 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*/ |
| 45 | 47 | private function getData(\DomElement $element = null, $type = 'data') { |
| 46 | 48 | while ($element) { |
| 47 | - if (isset($this->dataStorage[$element]) && isset($this->dataStorage[$element][$type])) return $this->dataStorage[$element][$type]; |
|
| 49 | + if (isset($this->dataStorage[$element]) && isset($this->dataStorage[$element][$type])) { |
|
| 50 | + return $this->dataStorage[$element][$type]; |
|
| 51 | + } |
|
| 48 | 52 | $element = $element->parentNode; |
| 49 | 53 | } |
| 50 | 54 | return $this->data; |
@@ -63,13 +67,19 @@ discard block |
||
| 63 | 67 | $valueParser = new \Transphporm\Parser\Value($this); |
| 64 | 68 | |
| 65 | 69 | foreach ($parts as $part) { |
| 66 | - if ($part === '') continue; |
|
| 70 | + if ($part === '') { |
|
| 71 | + continue; |
|
| 72 | + } |
|
| 67 | 73 | $part = $valueParser->parse($part, $element)[0]; |
| 68 | 74 | $funcResult = $this->processNestedFunc($part, $obj, $valueParser, $element); |
| 69 | 75 | |
| 70 | - if ($funcResult !== false) $obj = $funcResult; |
|
| 71 | - else if (method_exists($obj, $part)) $obj = call_user_func([$obj, $part]); |
|
| 72 | - else $obj = $this->ifNull($obj, $part); |
|
| 76 | + if ($funcResult !== false) { |
|
| 77 | + $obj = $funcResult; |
|
| 78 | + } else if (method_exists($obj, $part)) { |
|
| 79 | + $obj = call_user_func([$obj, $part]); |
|
| 80 | + } else { |
|
| 81 | + $obj = $this->ifNull($obj, $part); |
|
| 82 | + } |
|
| 73 | 83 | } |
| 74 | 84 | return $obj; |
| 75 | 85 | } |
@@ -78,13 +88,17 @@ discard block |
||
| 78 | 88 | if (strpos($part, '(') !== false) { |
| 79 | 89 | $subObjParser = new \Transphporm\Parser\Value($obj, $valueParser, false); |
| 80 | 90 | return $subObjParser->parse($part, $element); |
| 91 | + } else { |
|
| 92 | + return false; |
|
| 81 | 93 | } |
| 82 | - else return false; |
|
| 83 | 94 | } |
| 84 | 95 | |
| 85 | 96 | private function ifNull($obj, $key) { |
| 86 | - if (is_array($obj)) return isset($obj[$key]) ? $obj[$key] : null; |
|
| 87 | - else return isset($obj->$key) ? $obj->$key : null; |
|
| 97 | + if (is_array($obj)) { |
|
| 98 | + return isset($obj[$key]) ? $obj[$key] : null; |
|
| 99 | + } else { |
|
| 100 | + return isset($obj->$key) ? $obj->$key : null; |
|
| 101 | + } |
|
| 88 | 102 | } |
| 89 | 103 | |
| 90 | 104 | public function attr($val, $element) { |
@@ -109,14 +123,18 @@ discard block |
||
| 109 | 123 | |
| 110 | 124 | $doc = $newTemplate->output([], true)->body; |
| 111 | 125 | |
| 112 | - if (isset($val[1])) return $this->templateSubsection($val[1], $doc, $element); |
|
| 126 | + if (isset($val[1])) { |
|
| 127 | + return $this->templateSubsection($val[1], $doc, $element); |
|
| 128 | + } |
|
| 113 | 129 | |
| 114 | 130 | $newNode = $element->ownerDocument->importNode($doc->documentElement, true); |
| 115 | 131 | |
| 116 | 132 | $result = []; |
| 117 | 133 | |
| 118 | 134 | if ($newNode->tagName === 'template') { |
| 119 | - foreach ($newNode->childNodes as $node) $result[] = $node->cloneNode(true); |
|
| 135 | + foreach ($newNode->childNodes as $node) { |
|
| 136 | + $result[] = $node->cloneNode(true); |
|
| 137 | + } |
|
| 120 | 138 | } |
| 121 | 139 | //else $result[] = $newNode; |
| 122 | 140 | |
@@ -32,8 +32,9 @@ discard block |
||
| 32 | 32 | $params = $bracketMatcher->match('(', ')'); |
| 33 | 33 | |
| 34 | 34 | return ['name' => $name, 'params' => $params, 'endPoint' => $bracketMatcher->getClosePos()]; |
| 35 | + } else { |
|
| 36 | + return ['name' => null, 'params' => $function, 'endPoint' => strlen($function)]; |
|
| 35 | 37 | } |
| 36 | - else return ['name' => null, 'params' => $function, 'endPoint' => strlen($function)]; |
|
| 37 | 38 | } |
| 38 | 39 | |
| 39 | 40 | public function parse($function, \DomElement $element) { |
@@ -41,13 +42,15 @@ discard block |
||
| 41 | 42 | if ($function && in_array($function[0], ['\'', '"'])) { |
| 42 | 43 | $finalPos = $this->findMatchingPos($function, $function[0]); |
| 43 | 44 | $result[] = $this->extractQuotedString($function[0], $function); |
| 44 | - } |
|
| 45 | - else { |
|
| 45 | + } else { |
|
| 46 | 46 | $func = $this->parseFunction($function); |
| 47 | 47 | $finalPos = $func['endPoint']; |
| 48 | 48 | |
| 49 | - if (($data = $this->getFunctionValue($func['name'], $func['params'], $element)) !== false) $result = $this->appendToArray($result, $data); |
|
| 50 | - else $result[] = trim($function); |
|
| 49 | + if (($data = $this->getFunctionValue($func['name'], $func['params'], $element)) !== false) { |
|
| 50 | + $result = $this->appendToArray($result, $data); |
|
| 51 | + } else { |
|
| 52 | + $result[] = trim($function); |
|
| 53 | + } |
|
| 51 | 54 | } |
| 52 | 55 | $remaining = trim(substr($function, $finalPos+1)); |
| 53 | 56 | return $this->parseNextValue($remaining, $result, $element); |
@@ -56,23 +59,27 @@ discard block |
||
| 56 | 59 | private function getFunctionValue($name, $params, $element) { |
| 57 | 60 | if (($data = $this->callFunc($name, $params, $element)) !== false) { |
| 58 | 61 | return $data; |
| 59 | - } |
|
| 60 | - else if ($this->parent != null && ($data = $this->parent->callFunc($name, $params, $element)) !== false) { |
|
| 62 | + } else if ($this->parent != null && ($data = $this->parent->callFunc($name, $params, $element)) !== false) { |
|
| 61 | 63 | return $data; |
| 64 | + } else { |
|
| 65 | + return false; |
|
| 62 | 66 | } |
| 63 | - else return false; |
|
| 64 | 67 | } |
| 65 | 68 | |
| 66 | 69 | private function appendToArray($array, $value) { |
| 67 | - if (is_array($value)) $array += $value; |
|
| 68 | - else $array[] = $value; |
|
| 70 | + if (is_array($value)) { |
|
| 71 | + $array += $value; |
|
| 72 | + } else { |
|
| 73 | + $array[] = $value; |
|
| 74 | + } |
|
| 69 | 75 | return $array; |
| 70 | 76 | } |
| 71 | 77 | |
| 72 | 78 | private function callFunc($name, $params, $element) { |
| 73 | 79 | if ($name && $this->isCallable($this->dataFunction, $name)) { |
| 74 | - if ($this->callParamsAsArray) return $this->dataFunction->$name($this->parse($params, $element), $element); |
|
| 75 | - else { |
|
| 80 | + if ($this->callParamsAsArray) { |
|
| 81 | + return $this->dataFunction->$name($this->parse($params, $element), $element); |
|
| 82 | + } else { |
|
| 76 | 83 | return $this->callFuncOnObject($this->dataFunction, $name, $this->parse($params, $element)); |
| 77 | 84 | } |
| 78 | 85 | } |
@@ -85,12 +92,17 @@ discard block |
||
| 85 | 92 | } |
| 86 | 93 | |
| 87 | 94 | private function callFuncOnObject($obj, $func, $params) { |
| 88 | - if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $params); |
|
| 89 | - else return call_user_func_array($obj, $params); |
|
| 95 | + if (isset($obj->$func) && is_callable($obj->$func)) { |
|
| 96 | + return call_user_func_array($obj->$func, $params); |
|
| 97 | + } else { |
|
| 98 | + return call_user_func_array($obj, $params); |
|
| 99 | + } |
|
| 90 | 100 | } |
| 91 | 101 | |
| 92 | 102 | private function parseNextValue($remaining, $result, $element) { |
| 93 | - if (strlen($remaining) > 0 && $remaining[0] == ',') $result = array_merge($result, $this->parse(trim(substr($remaining, 1)), $element)); |
|
| 103 | + if (strlen($remaining) > 0 && $remaining[0] == ',') { |
|
| 104 | + $result = array_merge($result, $this->parse(trim(substr($remaining, 1)), $element)); |
|
| 105 | + } |
|
| 94 | 106 | return $result; |
| 95 | 107 | } |
| 96 | 108 | |
@@ -98,8 +110,9 @@ discard block |
||
| 98 | 110 | $pos = $start+1; |
| 99 | 111 | $end = 0; |
| 100 | 112 | while ($end = strpos($string, $char, $pos)) { |
| 101 | - if ($string[$end-1] === $escape) $pos = $end+1; |
|
| 102 | - else { |
|
| 113 | + if ($string[$end-1] === $escape) { |
|
| 114 | + $pos = $end+1; |
|
| 115 | + } else { |
|
| 103 | 116 | break; |
| 104 | 117 | } |
| 105 | 118 | } |