@@ -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,23 +18,34 @@ 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; |
|
22 | - if ($element->getAttribute('transphporm') === 'remove') return; |
|
21 | + if ($this->isIncludedTemplate($element)) { |
|
22 | + return; |
|
23 | + } |
|
24 | + if ($element->getAttribute('transphporm') === 'remove') { |
|
25 | + return; |
|
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 instanceof \DomDocument) return false; |
|
37 | - if ($element->getAttribute('transphporm') == 'includedtemplate') return true; |
|
43 | + if ($element instanceof \DomDocument) { |
|
44 | + return false; |
|
45 | + } |
|
46 | + if ($element->getAttribute('transphporm') == 'includedtemplate') { |
|
47 | + return true; |
|
48 | + } |
|
38 | 49 | } |
39 | 50 | while ($element = $element->parentNode); |
40 | 51 | return false; |
@@ -60,9 +71,10 @@ discard block |
||
60 | 71 | $new = $document->importNode($n, true); |
61 | 72 | //Removing this might cause problems with caching... |
62 | 73 | //$new->setAttribute('transphporm', 'added'); |
63 | - } |
|
64 | - else { |
|
65 | - if ($n instanceof \DomText) $n = $n->nodeValue; |
|
74 | + } else { |
|
75 | + if ($n instanceof \DomText) { |
|
76 | + $n = $n->nodeValue; |
|
77 | + } |
|
66 | 78 | $new = $document->createElement('text'); |
67 | 79 | $new->appendChild($document->createTextNode($n)); |
68 | 80 | $new->setAttribute('transphporm', 'text'); |
@@ -98,7 +110,9 @@ discard block |
||
98 | 110 | while ($e = $e->previousSibling && !in_array($e->getAttribute('transphporm'), [null, 'remove'])) { |
99 | 111 | $remove[] = $e; |
100 | 112 | } |
101 | - foreach ($remove as $r) $r->parentNode->removeChild($r); |
|
113 | + foreach ($remove as $r) { |
|
114 | + $r->parentNode->removeChild($r); |
|
115 | + } |
|
102 | 116 | } |
103 | 117 | |
104 | 118 | private function replaceContent($element, $content) { |
@@ -117,6 +131,8 @@ discard block |
||
117 | 131 | } |
118 | 132 | |
119 | 133 | private function removeAllChildren($element) { |
120 | - while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
|
134 | + while ($element->hasChildNodes()) { |
|
135 | + $element->removeChild($element->firstChild); |
|
136 | + } |
|
121 | 137 | } |
122 | 138 | } |
123 | 139 | \ No newline at end of file |
@@ -11,6 +11,10 @@ discard block |
||
11 | 11 | private $data; |
12 | 12 | private $baseDir; |
13 | 13 | |
14 | + /** |
|
15 | + * @param string $baseDir |
|
16 | + * @param string $tss |
|
17 | + */ |
|
14 | 18 | public function __construct(\SplObjectStorage $objectStorage, $data, $baseDir, $tss) { |
15 | 19 | $this->dataStorage = $objectStorage; |
16 | 20 | $this->data = $data; |
@@ -79,6 +83,9 @@ discard block |
||
79 | 83 | return $obj; |
80 | 84 | } |
81 | 85 | |
86 | + /** |
|
87 | + * @param \Transphporm\Parser\Value $valueParser |
|
88 | + */ |
|
82 | 89 | private function traverseObj($part, $obj, $valueParser, $element) { |
83 | 90 | if (strpos($part, '(') !== false) { |
84 | 91 | $subObjParser = new \Transphporm\Parser\Value($obj, $valueParser, false); |
@@ -143,6 +150,9 @@ discard block |
||
143 | 150 | return $result; |
144 | 151 | } |
145 | 152 | |
153 | + /** |
|
154 | + * @param string $templateFile |
|
155 | + */ |
|
146 | 156 | private function createDummyTemplateDoc(\DomElement $element, $templateFile) { |
147 | 157 | $newDocument = new \DomDocument; |
148 | 158 | $root = $newDocument->createElement('template'); |
@@ -164,6 +174,9 @@ discard block |
||
164 | 174 | return $newDocument; |
165 | 175 | } |
166 | 176 | |
177 | + /** |
|
178 | + * @param \DOMNode $baseElement |
|
179 | + */ |
|
167 | 180 | private function loadTemplate($baseElement, $templateFile) { |
168 | 181 | $baseElement->setAttribute('transphpormbaselement', 'true'); |
169 | 182 | //Load the template XML |
@@ -117,7 +117,7 @@ |
||
117 | 117 | else $tss = ''; |
118 | 118 | //Create a document to mimic the structure of the parent template |
119 | 119 | |
120 | - $newDocument = $this->createDummyTemplateDoc($element, $this->baseDir . $val[0]); |
|
120 | + $newDocument = $this->createDummyTemplateDoc($element, $this->baseDir.$val[0]); |
|
121 | 121 | |
122 | 122 | //Build a new template using the $newDocument |
123 | 123 | $newTemplate = new \Transphporm\Builder($newDocument->saveXml(), $tss); |
@@ -25,7 +25,9 @@ discard block |
||
25 | 25 | /** Binds data to an element */ |
26 | 26 | public function bind(\DomNode $element, $data, $type = 'data') { |
27 | 27 | //This is a bit of a hack to workaround #24, might need a better way of doing this if it causes a problem |
28 | - if (is_array($data) && $this->isObjectArray($data)) $data = $data[0]; |
|
28 | + if (is_array($data) && $this->isObjectArray($data)) { |
|
29 | + $data = $data[0]; |
|
30 | + } |
|
29 | 31 | $content = isset($this->dataStorage[$element]) ? $this->dataStorage[$element] : []; |
30 | 32 | $content[$type] = $data; |
31 | 33 | $this->dataStorage[$element] = $content; |
@@ -49,7 +51,9 @@ discard block |
||
49 | 51 | /** 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*/ |
50 | 52 | public function getData(\DomElement $element = null, $type = 'data') { |
51 | 53 | while ($element) { |
52 | - if (isset($this->dataStorage[$element]) && isset($this->dataStorage[$element][$type])) return $this->dataStorage[$element][$type]; |
|
54 | + if (isset($this->dataStorage[$element]) && isset($this->dataStorage[$element][$type])) { |
|
55 | + return $this->dataStorage[$element][$type]; |
|
56 | + } |
|
53 | 57 | $element = $element->parentNode; |
54 | 58 | } |
55 | 59 | return $this->data; |
@@ -68,13 +72,17 @@ discard block |
||
68 | 72 | $valueParser = new \Transphporm\Parser\Value($this); |
69 | 73 | |
70 | 74 | foreach ($parts as $part) { |
71 | - if ($part === '') continue; |
|
75 | + if ($part === '') { |
|
76 | + continue; |
|
77 | + } |
|
72 | 78 | $part = $valueParser->parse($part, $element)[0]; |
73 | 79 | $funcResult = $this->traverseObj($part, $obj, $valueParser, $element); |
74 | 80 | |
75 | - if ($funcResult !== false) $obj = $funcResult; |
|
76 | - |
|
77 | - else $obj = $this->ifNull($obj, $part); |
|
81 | + if ($funcResult !== false) { |
|
82 | + $obj = $funcResult; |
|
83 | + } else { |
|
84 | + $obj = $this->ifNull($obj, $part); |
|
85 | + } |
|
78 | 86 | } |
79 | 87 | return $obj; |
80 | 88 | } |
@@ -83,14 +91,19 @@ discard block |
||
83 | 91 | if (strpos($part, '(') !== false) { |
84 | 92 | $subObjParser = new \Transphporm\Parser\Value($obj, $valueParser, false); |
85 | 93 | return $subObjParser->parse($part, $element)[0]; |
94 | + } else if (method_exists($obj, $part)) { |
|
95 | + return call_user_func([$obj, $part]); |
|
96 | + } else { |
|
97 | + return false; |
|
86 | 98 | } |
87 | - else if (method_exists($obj, $part)) return call_user_func([$obj, $part]); |
|
88 | - else return false; |
|
89 | 99 | } |
90 | 100 | |
91 | 101 | private function ifNull($obj, $key) { |
92 | - if (is_array($obj)) return isset($obj[$key]) ? $obj[$key] : null; |
|
93 | - else return isset($obj->$key) ? $obj->$key : null; |
|
102 | + if (is_array($obj)) { |
|
103 | + return isset($obj[$key]) ? $obj[$key] : null; |
|
104 | + } else { |
|
105 | + return isset($obj->$key) ? $obj->$key : null; |
|
106 | + } |
|
94 | 107 | } |
95 | 108 | |
96 | 109 | public function attr($val, $element) { |
@@ -113,8 +126,11 @@ discard block |
||
113 | 126 | |
114 | 127 | public function template($val, \DomElement $element, $rules) { |
115 | 128 | //Check the nesting level... without this it will keep applying TSS to included templates forever in some cases |
116 | - if (isset($rules['template-recursion']) && $rules['template-recursion'] == 'on') $tss = $this->tss; |
|
117 | - else $tss = ''; |
|
129 | + if (isset($rules['template-recursion']) && $rules['template-recursion'] == 'on') { |
|
130 | + $tss = $this->tss; |
|
131 | + } else { |
|
132 | + $tss = ''; |
|
133 | + } |
|
118 | 134 | //Create a document to mimic the structure of the parent template |
119 | 135 | |
120 | 136 | $newDocument = $this->createDummyTemplateDoc($element, $this->baseDir . $val[0]); |
@@ -133,10 +149,14 @@ discard block |
||
133 | 149 | $result = []; |
134 | 150 | $xpath = new \DomXpath($doc); |
135 | 151 | $correspondingElement = $xpath->query('//*[@transphpormbaselement]')[0]; |
136 | - if (!$correspondingElement) $correspondingElement = $doc->documentElement; |
|
152 | + if (!$correspondingElement) { |
|
153 | + $correspondingElement = $doc->documentElement; |
|
154 | + } |
|
137 | 155 | foreach ($correspondingElement->childNodes as $child) { |
138 | 156 | $child = $child->cloneNode(true); |
139 | - if ($child instanceof \DomElement) $child->setAttribute('transphporm', 'includedtemplate'); |
|
157 | + if ($child instanceof \DomElement) { |
|
158 | + $child->setAttribute('transphporm', 'includedtemplate'); |
|
159 | + } |
|
140 | 160 | |
141 | 161 | $result[] = $child; |
142 | 162 | } |
@@ -155,8 +175,12 @@ discard block |
||
155 | 175 | $firstChild = $root->firstChild; |
156 | 176 | $el = $el->cloneNode(); |
157 | 177 | $newNode = $newDocument->importNode($el); |
158 | - if ($baseElement === null) $baseElement = $newNode; |
|
159 | - if ($firstChild) $newNode->appendChild($firstChild); |
|
178 | + if ($baseElement === null) { |
|
179 | + $baseElement = $newNode; |
|
180 | + } |
|
181 | + if ($firstChild) { |
|
182 | + $newNode->appendChild($firstChild); |
|
183 | + } |
|
160 | 184 | $root->appendChild($newNode); |
161 | 185 | } |
162 | 186 | while (($el = $el->parentNode) instanceof \DomElement); |