@@ -64,7 +64,7 @@ |
||
64 | 64 | $finalPos = $this->findMatchingPos($str, $marker); |
65 | 65 | $string = substr($str, 1, $finalPos-1); |
66 | 66 | //Now remove escape characters |
67 | - return str_replace('\\' . $marker, $marker, $string); |
|
67 | + return str_replace('\\'.$marker, $marker, $string); |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | private function parseFunction($function) { |
@@ -63,6 +63,9 @@ discard block |
||
63 | 63 | return $array; |
64 | 64 | } |
65 | 65 | |
66 | + /** |
|
67 | + * @param \DOMElement $element |
|
68 | + */ |
|
66 | 69 | private function callFunc($name, $params, $element) { |
67 | 70 | if ($name && is_callable([$this->dataFunction, $name])) { |
68 | 71 | return $this->dataFunction->$name($this->parse($params, $element), $element); |
@@ -70,6 +73,10 @@ discard block |
||
70 | 73 | return false; |
71 | 74 | } |
72 | 75 | |
76 | + /** |
|
77 | + * @param string $remaining |
|
78 | + * @param \DOMElement $element |
|
79 | + */ |
|
73 | 80 | private function parseNextValue($remaining, $result, $element) { |
74 | 81 | if (strlen($remaining) > 0 && $remaining[0] == ',') $result = array_merge($result, $this->parse(trim(substr($remaining, 1)), $element)); |
75 | 82 | return $result; |
@@ -28,8 +28,9 @@ discard block |
||
28 | 28 | $params = $bracketMatcher->match('(', ')'); |
29 | 29 | |
30 | 30 | return ['name' => $name, 'params' => $params, 'endPoint' => $bracketMatcher->getClosePos()]; |
31 | + } else { |
|
32 | + return ['name' => null, 'params' => $function, 'endPoint' => strlen($function)]; |
|
31 | 33 | } |
32 | - else return ['name' => null, 'params' => $function, 'endPoint' => strlen($function)]; |
|
33 | 34 | } |
34 | 35 | |
35 | 36 | public function parse($function, \DomElement $element) { |
@@ -37,23 +38,26 @@ discard block |
||
37 | 38 | if ($function && in_array($function[0], ['\'', '"'])) { |
38 | 39 | $finalPos = $this->findMatchingPos($function, $function[0]); |
39 | 40 | $result[] = $this->extractQuotedString($function[0], $function); |
40 | - } |
|
41 | - else { |
|
41 | + } else { |
|
42 | 42 | $func = $this->parseFunction($function); |
43 | 43 | $finalPos = $func['endPoint']; |
44 | 44 | |
45 | 45 | if (($data = $this->callFunc($func['name'], $func['params'], $element)) !== false) { |
46 | 46 | $result = $this->appendToArray($result, $data); |
47 | - } |
|
48 | - else $result[] = trim($function); |
|
47 | + } else { |
|
48 | + $result[] = trim($function); |
|
49 | + } |
|
49 | 50 | } |
50 | 51 | $remaining = trim(substr($function, $finalPos+1)); |
51 | 52 | return $this->parseNextValue($remaining, $result, $element); |
52 | 53 | } |
53 | 54 | |
54 | 55 | private function appendToArray($array, $value) { |
55 | - if (is_array($value)) $array += $value; |
|
56 | - else $array[] = $value; |
|
56 | + if (is_array($value)) { |
|
57 | + $array += $value; |
|
58 | + } else { |
|
59 | + $array[] = $value; |
|
60 | + } |
|
57 | 61 | return $array; |
58 | 62 | } |
59 | 63 | |
@@ -65,7 +69,9 @@ discard block |
||
65 | 69 | } |
66 | 70 | |
67 | 71 | private function parseNextValue($remaining, $result, $element) { |
68 | - if (strlen($remaining) > 0 && $remaining[0] == ',') $result = array_merge($result, $this->parse(trim(substr($remaining, 1)), $element)); |
|
72 | + if (strlen($remaining) > 0 && $remaining[0] == ',') { |
|
73 | + $result = array_merge($result, $this->parse(trim(substr($remaining, 1)), $element)); |
|
74 | + } |
|
69 | 75 | return $result; |
70 | 76 | } |
71 | 77 | |
@@ -73,8 +79,9 @@ discard block |
||
73 | 79 | $pos = $start+1; |
74 | 80 | $end = 0; |
75 | 81 | while ($end = strpos($string, $char, $pos)) { |
76 | - if ($string[$end-1] === $escape) $pos = $end+1; |
|
77 | - else { |
|
82 | + if ($string[$end-1] === $escape) { |
|
83 | + $pos = $end+1; |
|
84 | + } else { |
|
78 | 85 | break; |
79 | 86 | } |
80 | 87 | } |
@@ -50,6 +50,9 @@ |
||
50 | 50 | return $attr; |
51 | 51 | } |
52 | 52 | |
53 | + /** |
|
54 | + * @param string $comparator |
|
55 | + */ |
|
53 | 56 | private static function compare($comparator, $a, $b) { |
54 | 57 | if ($comparator == '=') return $a == $b; |
55 | 58 | else if ($comparator == '!=') return $a != $b; |
@@ -51,8 +51,11 @@ discard block |
||
51 | 51 | } |
52 | 52 | |
53 | 53 | private static function compare($comparator, $a, $b) { |
54 | - if ($comparator == '=') return $a == $b; |
|
55 | - else if ($comparator == '!=') return $a != $b; |
|
54 | + if ($comparator == '=') { |
|
55 | + return $a == $b; |
|
56 | + } else if ($comparator == '!=') { |
|
57 | + return $a != $b; |
|
58 | + } |
|
56 | 59 | } |
57 | 60 | |
58 | 61 | //split the css into indivudal functions |
@@ -66,8 +69,9 @@ discard block |
||
66 | 69 | $selector = $this->createSelector(); |
67 | 70 | $selector->type = $css[$i]; |
68 | 71 | $selectors[] = $selector; |
72 | + } else { |
|
73 | + $selector->string .= $css[$i]; |
|
69 | 74 | } |
70 | - else $selector->string .= $css[$i]; |
|
71 | 75 | } |
72 | 76 | return $selectors; |
73 | 77 | } |
@@ -78,7 +82,9 @@ discard block |
||
78 | 82 | $this->depth = count($selectors); |
79 | 83 | $xpath = '/'; |
80 | 84 | foreach ($selectors as $selector) { |
81 | - if (isset($this->translators[$selector->type])) $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
85 | + if (isset($this->translators[$selector->type])) { |
|
86 | + $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
87 | + } |
|
82 | 88 | } |
83 | 89 | |
84 | 90 | $xpath = str_replace('/[', '/*[', $xpath); |
@@ -15,14 +15,14 @@ |
||
15 | 15 | public function __construct($css, Value $valueParser, $prefix = '') { |
16 | 16 | $hash = $this->registerInstance(); |
17 | 17 | $this->valueParser = $valueParser; |
18 | - $this->css = str_replace([' >', '> '],['>', '>'], trim($css)); |
|
18 | + $this->css = str_replace([' >', '> '], ['>', '>'], trim($css)); |
|
19 | 19 | $this->translators = [ |
20 | - ' ' => function($string) use ($prefix) { return '//' . $prefix . $string; }, |
|
21 | - '' => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
|
22 | - '>' => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
|
23 | - '#' => function($string) { return '[@id=\'' . $string . '\']'; }, |
|
24 | - '.' => function($string) { return '[contains(concat(\' \', normalize-space(@class), \' \'), \' ' . $string . ' \')]'; }, |
|
25 | - '[' => function($string, $xpath) use($hash) { return '[' .'php:function(\'\Transphporm\Parser\CssToXpath::processAttr\', \'' . $string . '\', ., "' . $hash . '")' . ']'; }, |
|
20 | + ' ' => function($string) use ($prefix) { return '//'.$prefix.$string; }, |
|
21 | + '' => function($string) use ($prefix) { return '/'.$prefix.$string; }, |
|
22 | + '>' => function($string) use ($prefix) { return '/'.$prefix.$string; }, |
|
23 | + '#' => function($string) { return '[@id=\''.$string.'\']'; }, |
|
24 | + '.' => function($string) { return '[contains(concat(\' \', normalize-space(@class), \' \'), \' '.$string.' \')]'; }, |
|
25 | + '[' => function($string, $xpath) use($hash) { return '['.'php:function(\'\Transphporm\Parser\CssToXpath::processAttr\', \''.$string.'\', ., "'.$hash.'")'.']'; }, |
|
26 | 26 | ']' => function() { return ''; } |
27 | 27 | ]; |
28 | 28 | } |
@@ -9,9 +9,13 @@ |
||
9 | 9 | class PostProcess implements \Transphporm\Hook { |
10 | 10 | public function run(\DomElement $element) { |
11 | 11 | $transphporm = $element->getAttribute('transphporm'); |
12 | - if ($transphporm === 'remove') $element->parentNode->removeChild($element); |
|
13 | - else if ($transphporm === 'text') $element->parentNode->replaceChild($element->firstChild, $element); |
|
14 | - else $element->removeAttribute('transphporm'); |
|
12 | + if ($transphporm === 'remove') { |
|
13 | + $element->parentNode->removeChild($element); |
|
14 | + } else if ($transphporm === 'text') { |
|
15 | + $element->parentNode->replaceChild($element->firstChild, $element); |
|
16 | + } else { |
|
17 | + $element->removeAttribute('transphporm'); |
|
18 | + } |
|
15 | 19 | } |
16 | 20 | |
17 | 21 | } |
18 | 22 | \ No newline at end of file |
@@ -19,7 +19,9 @@ |
||
19 | 19 | $close = strpos($this->str, $closingChr, $open); |
20 | 20 | |
21 | 21 | $cPos = $open+1; |
22 | - while (($cPos = strpos($this->str, $openChr, $cPos+1)) !== false && $cPos < $close) $close = strpos($this->str, $closingChr, $close+1); |
|
22 | + while (($cPos = strpos($this->str, $openChr, $cPos+1)) !== false && $cPos < $close) { |
|
23 | + $close = strpos($this->str, $closingChr, $close+1); |
|
24 | + } |
|
23 | 25 | |
24 | 26 | $this->startPos = $open; |
25 | 27 | $this->endPos = $close; |
@@ -34,6 +34,10 @@ discard block |
||
34 | 34 | return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append'; |
35 | 35 | } |
36 | 36 | |
37 | + /** |
|
38 | + * @param \DOMElement $element |
|
39 | + * @param \Transphporm\Hook\PseudoMatcher $pseudoMatcher |
|
40 | + */ |
|
37 | 41 | private function processPseudo($value, $element, $pseudoMatcher) { |
38 | 42 | $pseudoContent = ['attr', 'header', 'before', 'after']; |
39 | 43 | foreach ($pseudoContent as $pseudo) { |
@@ -91,6 +95,9 @@ discard block |
||
91 | 95 | foreach ($remove as $r) $r->parentNode->removeChild($r); |
92 | 96 | } |
93 | 97 | |
98 | + /** |
|
99 | + * @param \DOMElement $element |
|
100 | + */ |
|
94 | 101 | private function replaceContent($element, $content) { |
95 | 102 | //If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. |
96 | 103 | $this->removeAdded($element); |
@@ -100,12 +107,18 @@ discard block |
||
100 | 107 | $element->setAttribute('transphporm', 'remove'); |
101 | 108 | } |
102 | 109 | |
110 | + /** |
|
111 | + * @param \DOMElement $element |
|
112 | + */ |
|
103 | 113 | private function appendContent($element, $content) { |
104 | 114 | foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
105 | 115 | $element->appendChild($node); |
106 | 116 | } |
107 | 117 | } |
108 | 118 | |
119 | + /** |
|
120 | + * @param \DOMElement $element |
|
121 | + */ |
|
109 | 122 | private function removeAllChildren($element) { |
110 | 123 | while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
111 | 124 | } |
@@ -18,15 +18,20 @@ discard block |
||
18 | 18 | } |
19 | 19 | |
20 | 20 | public function run($value, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
21 | - if ($element->getAttribute('transphporm') === 'remove') return; |
|
21 | + if ($element->getAttribute('transphporm') === 'remove') { |
|
22 | + return; |
|
23 | + } |
|
22 | 24 | |
23 | 25 | $value = $this->formatter->format($value, $rules); |
24 | 26 | if (!$this->processPseudo($value, $element, $pseudoMatcher)) { |
25 | 27 | //Remove the current contents |
26 | 28 | $this->removeAllChildren($element); |
27 | 29 | //Now make a text node |
28 | - if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $value); |
|
29 | - else $this->appendContent($element, $value); |
|
30 | + if ($this->getContentMode($rules) === 'replace') { |
|
31 | + $this->replaceContent($element, $value); |
|
32 | + } else { |
|
33 | + $this->appendContent($element, $value); |
|
34 | + } |
|
30 | 35 | } |
31 | 36 | } |
32 | 37 | |
@@ -50,9 +55,10 @@ discard block |
||
50 | 55 | if ($n instanceof \DomElement) { |
51 | 56 | $new = $document->importNode($n, true); |
52 | 57 | $new->setAttribute('transphporm', 'added'); |
53 | - } |
|
54 | - else { |
|
55 | - if ($n instanceof \DomText) $n = $n->nodeValue; |
|
58 | + } else { |
|
59 | + if ($n instanceof \DomText) { |
|
60 | + $n = $n->nodeValue; |
|
61 | + } |
|
56 | 62 | $new = $document->createElement('text'); |
57 | 63 | $new->appendChild($document->createTextNode($n)); |
58 | 64 | $new->setAttribute('transphporm', 'text'); |
@@ -88,7 +94,9 @@ discard block |
||
88 | 94 | while ($e = $e->previousSibling && !in_array($e->getAttribute('transphporm'), [null, 'remove'])) { |
89 | 95 | $remove[] = $e; |
90 | 96 | } |
91 | - foreach ($remove as $r) $r->parentNode->removeChild($r); |
|
97 | + foreach ($remove as $r) { |
|
98 | + $r->parentNode->removeChild($r); |
|
99 | + } |
|
92 | 100 | } |
93 | 101 | |
94 | 102 | private function replaceContent($element, $content) { |
@@ -107,6 +115,8 @@ discard block |
||
107 | 115 | } |
108 | 116 | |
109 | 117 | private function removeAllChildren($element) { |
110 | - while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
|
118 | + while ($element->hasChildNodes()) { |
|
119 | + $element->removeChild($element->firstChild); |
|
120 | + } |
|
111 | 121 | } |
112 | 122 | } |
113 | 123 | \ No newline at end of file |
@@ -34,6 +34,9 @@ |
||
34 | 34 | return false; |
35 | 35 | } |
36 | 36 | |
37 | + /** |
|
38 | + * @param \Transphporm\Hook\PseudoMatcher $pseudoMatcher |
|
39 | + */ |
|
37 | 40 | private function createHook($newRules, $pseudoMatcher, $properties) { |
38 | 41 | $hook = new \Transphporm\Hook\PropertyHook($newRules, $pseudoMatcher, new \Transphporm\Parser\Value($this->data)); |
39 | 42 | foreach ($properties as $name => $property) $hook->registerProperty($name, $property); |
@@ -13,13 +13,17 @@ discard block |
||
13 | 13 | } |
14 | 14 | |
15 | 15 | public function run($value, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
16 | - if ($element->getAttribute('transphporm') === 'added') return $element->parentNode->removeChild($element); |
|
16 | + if ($element->getAttribute('transphporm') === 'added') { |
|
17 | + return $element->parentNode->removeChild($element); |
|
18 | + } |
|
17 | 19 | |
18 | 20 | $count = 0; |
19 | 21 | foreach ($value as $key => $iteration) { |
20 | 22 | $clone = $element->cloneNode(true); |
21 | 23 | //Mark all but one of the nodes as having been added by transphporm, when the hook is run again, these are removed |
22 | - if ($count > 0) $clone->setAttribute('transphporm', 'added'); |
|
24 | + if ($count > 0) { |
|
25 | + $clone->setAttribute('transphporm', 'added'); |
|
26 | + } |
|
23 | 27 | $this->data->bind($clone, $iteration, 'iteration'); |
24 | 28 | $this->data->bind($clone, $key, 'key'); |
25 | 29 | $element->parentNode->insertBefore($clone, $element); |
@@ -38,7 +42,9 @@ discard block |
||
38 | 42 | |
39 | 43 | private function createHook($newRules, $pseudoMatcher, $properties) { |
40 | 44 | $hook = new \Transphporm\Hook\PropertyHook($newRules, $pseudoMatcher, new \Transphporm\Parser\Value($this->data)); |
41 | - foreach ($properties as $name => $property) $hook->registerProperty($name, $property); |
|
45 | + foreach ($properties as $name => $property) { |
|
46 | + $hook->registerProperty($name, $property); |
|
47 | + } |
|
42 | 48 | return $hook; |
43 | 49 | } |
44 | 50 | } |
45 | 51 | \ No newline at end of file |
@@ -20,11 +20,15 @@ discard block |
||
20 | 20 | |
21 | 21 | public function run(\DomElement $element) { |
22 | 22 | //Don't run if there's a pseudo element like nth-child() and this element doesn't match it |
23 | - if (!$this->pseudoMatcher->matches($element)) return; |
|
23 | + if (!$this->pseudoMatcher->matches($element)) { |
|
24 | + return; |
|
25 | + } |
|
24 | 26 | |
25 | 27 | foreach ($this->rules as $name => $value) { |
26 | 28 | $result = $this->callProperty($name, $element, $this->valueParser->parse(trim($value), $element)); |
27 | - if ($result === false) break; |
|
29 | + if ($result === false) { |
|
30 | + break; |
|
31 | + } |
|
28 | 32 | } |
29 | 33 | } |
30 | 34 | |
@@ -45,7 +49,9 @@ discard block |
||
45 | 49 | } |
46 | 50 | |
47 | 51 | private function callProperty($name, $element, $value) { |
48 | - if (isset($this->properties[$name])) return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); |
|
52 | + if (isset($this->properties[$name])) { |
|
53 | + return $this->properties[$name]->run($value, $element, $this->rules, $this->pseudoMatcher, $this->properties); |
|
54 | + } |
|
49 | 55 | return false; |
50 | 56 | } |
51 | 57 | } |
@@ -7,8 +7,12 @@ |
||
7 | 7 | namespace Transphporm\Property; |
8 | 8 | class Display implements \Transphporm\Property { |
9 | 9 | public function run($value, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
10 | - if ($pseudoMatcher->hasFunction('attr')) $element->removeAttribute($pseudoMatcher->getFuncArgs('attr')); |
|
11 | - else if (strtolower($value[0]) === 'none') $element->setAttribute('transphporm', 'remove'); |
|
12 | - else $element->setAttribute('transphporm', 'show'); |
|
10 | + if ($pseudoMatcher->hasFunction('attr')) { |
|
11 | + $element->removeAttribute($pseudoMatcher->getFuncArgs('attr')); |
|
12 | + } else if (strtolower($value[0]) === 'none') { |
|
13 | + $element->setAttribute('transphporm', 'remove'); |
|
14 | + } else { |
|
15 | + $element->setAttribute('transphporm', 'show'); |
|
16 | + } |
|
13 | 17 | } |
14 | 18 | } |
15 | 19 | \ No newline at end of file |
@@ -14,10 +14,14 @@ discard block |
||
14 | 14 | |
15 | 15 | public function match($pseudo, \DomElement $element) { |
16 | 16 | $pos = strpos($pseudo, '['); |
17 | - if ($pos === false) return true; |
|
17 | + if ($pos === false) { |
|
18 | + return true; |
|
19 | + } |
|
18 | 20 | |
19 | 21 | $name = substr($pseudo, 0, $pos); |
20 | - if (!is_callable([$this->dataFunction, $name])) return true; |
|
22 | + if (!is_callable([$this->dataFunction, $name])) { |
|
23 | + return true; |
|
24 | + } |
|
21 | 25 | |
22 | 26 | $bracketMatcher = new \Transphporm\Parser\BracketMatcher($pseudo); |
23 | 27 | $criteria = $bracketMatcher->match('[', ']'); |
@@ -42,16 +46,21 @@ discard block |
||
42 | 46 | } |
43 | 47 | |
44 | 48 | private function parseValue($value) { |
45 | - if ($value == 'true') return true; |
|
46 | - else if ($value == 'false') return false; |
|
47 | - else return $value; |
|
49 | + if ($value == 'true') { |
|
50 | + return true; |
|
51 | + } else if ($value == 'false') { |
|
52 | + return false; |
|
53 | + } else { |
|
54 | + return $value; |
|
55 | + } |
|
48 | 56 | } |
49 | 57 | |
50 | 58 | private function getOperator($field) { |
51 | 59 | if ($field[strlen($field)-1] == '!') { |
52 | 60 | return '!'; |
61 | + } else { |
|
62 | + return ''; |
|
53 | 63 | } |
54 | - else return ''; |
|
55 | 64 | } |
56 | 65 | |
57 | 66 | } |
58 | 67 | \ No newline at end of file |