@@ -7,7 +7,9 @@ |
||
7 | 7 | namespace Transphporm\Pseudo; |
8 | 8 | class Attribute implements \Transphporm\Pseudo { |
9 | 9 | public function match($name, $args, \DomElement $element) { |
10 | - if ($name === null) return true; |
|
10 | + if ($name === null) { |
|
11 | + return true; |
|
12 | + } |
|
11 | 13 | return $args[0]; |
12 | 14 | } |
13 | 15 | } |
@@ -104,15 +104,15 @@ |
||
104 | 104 | } |
105 | 105 | |
106 | 106 | private function getProperties($tokens) { |
107 | - $rules = $tokens->splitOnToken(Tokenizer::SEMI_COLON); |
|
107 | + $rules = $tokens->splitOnToken(Tokenizer::SEMI_COLON); |
|
108 | 108 | |
109 | - $return = []; |
|
110 | - foreach ($rules as $rule) { |
|
111 | - $name = $rule->from(Tokenizer::NAME, true)->to(Tokenizer::COLON)->read(); |
|
112 | - $tokens = $rule->from(Tokenizer::COLON)->trim(); |
|
113 | - if (count($tokens) > 0) $return[$name] = $rule->from(Tokenizer::COLON)->trim(); |
|
114 | - } |
|
109 | + $return = []; |
|
110 | + foreach ($rules as $rule) { |
|
111 | + $name = $rule->from(Tokenizer::NAME, true)->to(Tokenizer::COLON)->read(); |
|
112 | + $tokens = $rule->from(Tokenizer::COLON)->trim(); |
|
113 | + if (count($tokens) > 0) $return[$name] = $rule->from(Tokenizer::COLON)->trim(); |
|
114 | + } |
|
115 | 115 | |
116 | - return $return; |
|
117 | - } |
|
116 | + return $return; |
|
117 | + } |
|
118 | 118 | } |
@@ -25,7 +25,9 @@ discard block |
||
25 | 25 | } |
26 | 26 | |
27 | 27 | public function parse($indexStart = 0) { |
28 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
28 | + if (!empty($this->rules)) { |
|
29 | + return $this->rules['rules']; |
|
30 | + } |
|
29 | 31 | $rules = $this->parseTokens($indexStart); |
30 | 32 | $this->checkError($rules); |
31 | 33 | return $rules; |
@@ -36,8 +38,9 @@ discard block |
||
36 | 38 | foreach (new TokenFilterIterator($this->tss, [Tokenizer::WHITESPACE]) as $token) { |
37 | 39 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
38 | 40 | $this->rules = array_merge($this->rules, $processing); |
41 | + } else if (!in_array($token['type'], [Tokenizer::NEW_LINE, Tokenizer::AT_SIGN])) { |
|
42 | + $this->addRules($token, $indexStart++); |
|
39 | 43 | } |
40 | - else if (!in_array($token['type'], [Tokenizer::NEW_LINE, Tokenizer::AT_SIGN])) $this->addRules($token, $indexStart++); |
|
41 | 44 | } |
42 | 45 | |
43 | 46 | return $this->rules; |
@@ -47,13 +50,17 @@ discard block |
||
47 | 50 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
48 | 51 | |
49 | 52 | $this->tss->skip(count($selector)); |
50 | - if (count($selector) === 0) return; |
|
53 | + if (count($selector) === 0) { |
|
54 | + return; |
|
55 | + } |
|
51 | 56 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $token['line']); |
52 | 57 | $this->rules = $this->writeRule($this->rules, $newRules); |
53 | 58 | } |
54 | 59 | |
55 | 60 | private function checkError($rules) { |
56 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
61 | + if (empty($rules) && count($this->tss) > 0) { |
|
62 | + throw new \Exception('No TSS rules parsed'); |
|
63 | + } |
|
57 | 64 | } |
58 | 65 | |
59 | 66 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -80,7 +87,9 @@ discard block |
||
80 | 87 | } |
81 | 88 | |
82 | 89 | private function processingInstructions($token, $indexStart) { |
83 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
90 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
91 | + return false; |
|
92 | + } |
|
84 | 93 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
85 | 94 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
86 | 95 | $funcToks = $tokens->from(Tokenizer::NAME); |
@@ -110,7 +119,9 @@ discard block |
||
110 | 119 | foreach ($rules as $rule) { |
111 | 120 | $name = $rule->from(Tokenizer::NAME, true)->to(Tokenizer::COLON)->read(); |
112 | 121 | $tokens = $rule->from(Tokenizer::COLON)->trim(); |
113 | - if (count($tokens) > 0) $return[$name] = $rule->from(Tokenizer::COLON)->trim(); |
|
122 | + if (count($tokens) > 0) { |
|
123 | + $return[$name] = $rule->from(Tokenizer::COLON)->trim(); |
|
124 | + } |
|
114 | 125 | } |
115 | 126 | |
116 | 127 | return $return; |
@@ -36,8 +36,12 @@ discard block |
||
36 | 36 | $n--; |
37 | 37 | } |
38 | 38 | |
39 | - if ($n == 0) return false; |
|
40 | - if (in_array($str->read($n), ['(', "\n", ' ', '['])) return true; |
|
39 | + if ($n == 0) { |
|
40 | + return false; |
|
41 | + } |
|
42 | + if (in_array($str->read($n), ['(', "\n", ' ', '['])) { |
|
43 | + return true; |
|
44 | + } |
|
41 | 45 | |
42 | 46 | return false; |
43 | 47 | } |
@@ -51,9 +55,13 @@ discard block |
||
51 | 55 | } |
52 | 56 | |
53 | 57 | private function processLiterals($tokens, $name, $str) { |
54 | - if (is_numeric($name)) $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]); |
|
55 | - else if (method_exists($this, $name)) $this->$name($tokens); |
|
56 | - else $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]); |
|
58 | + if (is_numeric($name)) { |
|
59 | + $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]); |
|
60 | + } else if (method_exists($this, $name)) { |
|
61 | + $this->$name($tokens); |
|
62 | + } else { |
|
63 | + $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]); |
|
64 | + } |
|
57 | 65 | } |
58 | 66 | |
59 | 67 | private function true($tokens) { |
@@ -25,8 +25,12 @@ discard block |
||
25 | 25 | public function matches($element) { |
26 | 26 | foreach ($this->pseudo as $i => $tokens) { |
27 | 27 | $parts = $this->getFuncParts($i, $tokens); |
28 | - if ($parts['name'] === null) $parts['name'] = 'data'; |
|
29 | - if (!isset($this->functions[$parts['name']])) continue; |
|
28 | + if ($parts['name'] === null) { |
|
29 | + $parts['name'] = 'data'; |
|
30 | + } |
|
31 | + if (!isset($this->functions[$parts['name']])) { |
|
32 | + continue; |
|
33 | + } |
|
30 | 34 | if ($this->match($parts, $this->functions[$parts['name']], $element) === false) { |
31 | 35 | return false; |
32 | 36 | } |
@@ -38,14 +42,17 @@ discard block |
||
38 | 42 | private function match($parts, $function, $element) { |
39 | 43 | try { |
40 | 44 | $matches = $function->match($parts['name'], $parts['args'], $element); |
41 | - if ($matches === false) return false; |
|
42 | - } |
|
43 | - catch (\Exception $e) { |
|
45 | + if ($matches === false) { |
|
46 | + return false; |
|
47 | + } |
|
48 | + } catch (\Exception $e) { |
|
44 | 49 | throw new \Transphporm\RunException(\Transphporm\Exception::PSEUDO, $parts['name'], $e); |
45 | 50 | } |
46 | 51 | } |
47 | 52 | private function getFuncParts($i, $tokens) { |
48 | - if (isset($this->funcParts[$i])) return $this->funcParts[$i]; |
|
53 | + if (isset($this->funcParts[$i])) { |
|
54 | + return $this->funcParts[$i]; |
|
55 | + } |
|
49 | 56 | $parts = []; |
50 | 57 | $canCache = true; |
51 | 58 | $parts['name'] = $this->getFuncName($tokens); |
@@ -53,37 +60,47 @@ discard block |
||
53 | 60 | //If the args are dynamic, it can't be cached as it may change between calls |
54 | 61 | $canCache = false; |
55 | 62 | $parts['args'] = $this->valueParser->parseTokens($tokens); |
56 | - } |
|
57 | - else if (count($tokens) > 1) { |
|
63 | + } else if (count($tokens) > 1) { |
|
58 | 64 | $tokens->rewind(); |
59 | 65 | $tokens->next(); |
60 | 66 | $this->skipWhitespace($tokens); |
61 | 67 | $parts['args'] = $this->valueParser->parseTokens($tokens->current()['value']); |
68 | + } else { |
|
69 | + $parts['args'] = [['']]; |
|
70 | + } |
|
71 | + if ($canCache) { |
|
72 | + $this->funcParts[$i] = $parts; |
|
62 | 73 | } |
63 | - else $parts['args'] = [['']]; |
|
64 | - if ($canCache) $this->funcParts[$i] = $parts; |
|
65 | 74 | return $parts; |
66 | 75 | } |
67 | 76 | |
68 | 77 | private function skipWhitespace($tokens) { |
69 | - while ($tokens->current()['type'] === 'WHITESPACE' || $tokens->current()['type'] == 'NEWLINE') $tokens->next(); |
|
78 | + while ($tokens->current()['type'] === 'WHITESPACE' || $tokens->current()['type'] == 'NEWLINE') { |
|
79 | + $tokens->next(); |
|
80 | + } |
|
70 | 81 | } |
71 | 82 | |
72 | 83 | private function getFuncName($tokens) { |
73 | - if ($tokens->type() === Tokenizer::NAME) return $tokens->read(); |
|
84 | + if ($tokens->type() === Tokenizer::NAME) { |
|
85 | + return $tokens->read(); |
|
86 | + } |
|
74 | 87 | return null; |
75 | 88 | } |
76 | 89 | |
77 | 90 | public function hasFunction($name) { |
78 | 91 | foreach ($this->pseudo as $tokens) { |
79 | - if ($name === $this->getFuncName($tokens)) return true; |
|
92 | + if ($name === $this->getFuncName($tokens)) { |
|
93 | + return true; |
|
94 | + } |
|
80 | 95 | } |
81 | 96 | } |
82 | 97 | |
83 | 98 | public function getFuncArgs($name) { |
84 | 99 | foreach ($this->pseudo as $i => $tokens) { |
85 | 100 | $parts = $this->getFuncParts($i, $tokens); |
86 | - if ($name === $parts['name']) return $parts['args']; |
|
101 | + if ($name === $parts['name']) { |
|
102 | + return $parts['args']; |
|
103 | + } |
|
87 | 104 | } |
88 | 105 | } |
89 | 106 | } |
@@ -6,39 +6,39 @@ |
||
6 | 6 | * @version 1.2 */ |
7 | 7 | namespace Transphporm\Parser; |
8 | 8 | class TokenFilterIterator implements \Iterator { |
9 | - private $ignore; |
|
10 | - private $tokens; |
|
9 | + private $ignore; |
|
10 | + private $tokens; |
|
11 | 11 | |
12 | - public function __construct(Tokens $tokens, array $ignore) { |
|
13 | - $this->ignore = $ignore; |
|
14 | - $this->tokens = $tokens; |
|
15 | - } |
|
12 | + public function __construct(Tokens $tokens, array $ignore) { |
|
13 | + $this->ignore = $ignore; |
|
14 | + $this->tokens = $tokens; |
|
15 | + } |
|
16 | 16 | |
17 | - public function current(): array { |
|
18 | - return $this->tokens->current(); |
|
19 | - } |
|
17 | + public function current(): array { |
|
18 | + return $this->tokens->current(); |
|
19 | + } |
|
20 | 20 | |
21 | - public function key(): int { |
|
22 | - return $this->tokens->key(); |
|
23 | - } |
|
21 | + public function key(): int { |
|
22 | + return $this->tokens->key(); |
|
23 | + } |
|
24 | 24 | |
25 | - public function valid(): bool { |
|
26 | - return $this->tokens->valid(); |
|
27 | - } |
|
25 | + public function valid(): bool { |
|
26 | + return $this->tokens->valid(); |
|
27 | + } |
|
28 | 28 | |
29 | - public function next(): void { |
|
30 | - do { |
|
31 | - $this->tokens->next(); |
|
32 | - } |
|
33 | - while ($this->shouldContinue()); |
|
34 | - } |
|
29 | + public function next(): void { |
|
30 | + do { |
|
31 | + $this->tokens->next(); |
|
32 | + } |
|
33 | + while ($this->shouldContinue()); |
|
34 | + } |
|
35 | 35 | |
36 | - public function rewind(): void { |
|
37 | - $this->tokens->rewind(); |
|
38 | - while ($this->shouldContinue()) $this->tokens->next(); |
|
39 | - } |
|
36 | + public function rewind(): void { |
|
37 | + $this->tokens->rewind(); |
|
38 | + while ($this->shouldContinue()) $this->tokens->next(); |
|
39 | + } |
|
40 | 40 | |
41 | - private function shouldContinue() { |
|
42 | - return $this->tokens->valid() && in_array($this->tokens->current()['type'], $this->ignore); |
|
43 | - } |
|
41 | + private function shouldContinue() { |
|
42 | + return $this->tokens->valid() && in_array($this->tokens->current()['type'], $this->ignore); |
|
43 | + } |
|
44 | 44 | } |
@@ -35,7 +35,9 @@ |
||
35 | 35 | |
36 | 36 | public function rewind(): void { |
37 | 37 | $this->tokens->rewind(); |
38 | - while ($this->shouldContinue()) $this->tokens->next(); |
|
38 | + while ($this->shouldContinue()) { |
|
39 | + $this->tokens->next(); |
|
40 | + } |
|
39 | 41 | } |
40 | 42 | |
41 | 43 | private function shouldContinue() { |
@@ -15,8 +15,8 @@ |
||
15 | 15 | |
16 | 16 | private function getLocale() { |
17 | 17 | if (is_array($this->locale)) return $this->locale; |
18 | - else if (strlen((string)$this->locale) > 0) return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . $this->locale . '.json'), true); |
|
19 | - else return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . 'enGB.json'), true); |
|
18 | + else if (strlen((string) $this->locale) > 0) return json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'../Formatter'.DIRECTORY_SEPARATOR.'Locale'.DIRECTORY_SEPARATOR.$this->locale.'.json'), true); |
|
19 | + else return json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'../Formatter'.DIRECTORY_SEPARATOR.'Locale'.DIRECTORY_SEPARATOR.'enGB.json'), true); |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | public function load(\Transphporm\Config $config) { |
@@ -14,9 +14,13 @@ |
||
14 | 14 | } |
15 | 15 | |
16 | 16 | private function getLocale() { |
17 | - if (is_array($this->locale)) return $this->locale; |
|
18 | - else if (strlen((string)$this->locale) > 0) return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . $this->locale . '.json'), true); |
|
19 | - else return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . 'enGB.json'), true); |
|
17 | + if (is_array($this->locale)) { |
|
18 | + return $this->locale; |
|
19 | + } else if (strlen((string)$this->locale) > 0) { |
|
20 | + return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . $this->locale . '.json'), true); |
|
21 | + } else { |
|
22 | + return json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . '../Formatter' . DIRECTORY_SEPARATOR . 'Locale' . DIRECTORY_SEPARATOR . 'enGB.json'), true); |
|
23 | + } |
|
20 | 24 | } |
21 | 25 | |
22 | 26 | public function load(\Transphporm\Config $config) { |
@@ -14,7 +14,9 @@ discard block |
||
14 | 14 | } |
15 | 15 | |
16 | 16 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
17 | - if (!$this->shouldRun($element)) return false; |
|
17 | + if (!$this->shouldRun($element)) { |
|
18 | + return false; |
|
19 | + } |
|
18 | 20 | |
19 | 21 | $values = $this->formatter->format($values, $rules); |
20 | 22 | |
@@ -25,14 +27,17 @@ discard block |
||
25 | 27 | if ($this->getContentMode($rules) === 'replace') { |
26 | 28 | $contentReplace = new ContentReplace($this); |
27 | 29 | $contentReplace->replaceContent($element, $values); |
30 | + } else { |
|
31 | + $this->appendContent($element, $values); |
|
28 | 32 | } |
29 | - else $this->appendContent($element, $values); |
|
30 | 33 | } |
31 | 34 | } |
32 | 35 | |
33 | 36 | private function shouldRun($element) { |
34 | 37 | do { |
35 | - if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
|
38 | + if ($element->getAttribute('transphporm') == 'includedtemplate') { |
|
39 | + return false; |
|
40 | + } |
|
36 | 41 | } |
37 | 42 | while (($element = $element->parentNode) instanceof \DomElement); |
38 | 43 | return true; |
@@ -59,9 +64,10 @@ discard block |
||
59 | 64 | public function getNode($node, $document) { |
60 | 65 | foreach ($node as $n) { |
61 | 66 | if (is_array($n)) { |
62 | - foreach ($this->getNode($n, $document) as $new) yield $new; |
|
63 | - } |
|
64 | - else { |
|
67 | + foreach ($this->getNode($n, $document) as $new) { |
|
68 | + yield $new; |
|
69 | + } |
|
70 | + } else { |
|
65 | 71 | yield $this->convertNode($n, $document); |
66 | 72 | } |
67 | 73 | } |
@@ -70,9 +76,10 @@ discard block |
||
70 | 76 | private function convertNode($node, $document) { |
71 | 77 | if ($node instanceof \DomElement || $node instanceof \DOMComment) { |
72 | 78 | $new = $document->importNode($node, true); |
73 | - } |
|
74 | - else { |
|
75 | - if ($node instanceof \DomText) $node = $node->nodeValue; |
|
79 | + } else { |
|
80 | + if ($node instanceof \DomText) { |
|
81 | + $node = $node->nodeValue; |
|
82 | + } |
|
76 | 83 | $new = $document->createElement('text'); |
77 | 84 | |
78 | 85 | $new->appendChild($document->createTextNode($node !== null ? $node : '')); |
@@ -89,6 +96,8 @@ discard block |
||
89 | 96 | } |
90 | 97 | |
91 | 98 | private function removeAllChildren($element) { |
92 | - while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
|
99 | + while ($element->hasChildNodes()) { |
|
100 | + $element->removeChild($element->firstChild); |
|
101 | + } |
|
93 | 102 | } |
94 | 103 | } |