@@ -12,13 +12,17 @@ |
||
12 | 12 | |
13 | 13 | if (trim($json)[0] != '{') { |
14 | 14 | $path = $this->baseDir . $json; |
15 | - if (!file_exists($path)) throw new \Exception('File does not exist at: ' . $path); |
|
15 | + if (!file_exists($path)) { |
|
16 | + throw new \Exception('File does not exist at: ' . $path); |
|
17 | + } |
|
16 | 18 | $json = file_get_contents($json); |
17 | 19 | } |
18 | 20 | |
19 | 21 | $map = json_decode($json, true); |
20 | 22 | |
21 | - if (!is_array($map)) throw new \Exception('Could not decode json: ' . json_last_error_msg()); |
|
23 | + if (!is_array($map)) { |
|
24 | + throw new \Exception('Could not decode json: ' . json_last_error_msg()); |
|
25 | + } |
|
22 | 26 | |
23 | 27 | return $map; |
24 | 28 | } |
@@ -12,13 +12,13 @@ |
||
12 | 12 | |
13 | 13 | if ($this->isJsonFile($json)) { |
14 | 14 | $path = $this->filePath->getFilePath($json); |
15 | - if (!file_exists($path)) throw new \Exception('File does not exist at: ' . $path); |
|
15 | + if (!file_exists($path)) throw new \Exception('File does not exist at: '.$path); |
|
16 | 16 | $json = file_get_contents($path); |
17 | 17 | } |
18 | 18 | |
19 | 19 | $map = json_decode($json, true); |
20 | 20 | |
21 | - if (!is_array($map)) throw new \Exception('Could not decode json: ' . json_last_error_msg()); |
|
21 | + if (!is_array($map)) throw new \Exception('Could not decode json: '.json_last_error_msg()); |
|
22 | 22 | |
23 | 23 | return $map; |
24 | 24 | } |
@@ -1,29 +1,29 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Transphporm\TSSFunction; |
3 | 3 | class Json implements \Transphporm\TSSFunction { |
4 | - private $filePath; |
|
4 | + private $filePath; |
|
5 | 5 | |
6 | - public function __construct(\Transphporm\FilePath $filePath) { |
|
7 | - $this->filePath = $filePath; |
|
8 | - } |
|
6 | + public function __construct(\Transphporm\FilePath $filePath) { |
|
7 | + $this->filePath = $filePath; |
|
8 | + } |
|
9 | 9 | |
10 | - public function run(array $args, \DomElement $element = null) { |
|
11 | - $json = $args[0]; |
|
10 | + public function run(array $args, \DomElement $element = null) { |
|
11 | + $json = $args[0]; |
|
12 | 12 | |
13 | - if ($this->isJsonFile($json)) { |
|
14 | - $path = $this->filePath->getFilePath($json); |
|
15 | - if (!file_exists($path)) throw new \Exception('File does not exist at: ' . $path); |
|
16 | - $json = file_get_contents($path); |
|
17 | - } |
|
13 | + if ($this->isJsonFile($json)) { |
|
14 | + $path = $this->filePath->getFilePath($json); |
|
15 | + if (!file_exists($path)) throw new \Exception('File does not exist at: ' . $path); |
|
16 | + $json = file_get_contents($path); |
|
17 | + } |
|
18 | 18 | |
19 | - $map = json_decode($json, true); |
|
19 | + $map = json_decode($json, true); |
|
20 | 20 | |
21 | - if (!is_array($map)) throw new \Exception('Could not decode json: ' . json_last_error_msg()); |
|
21 | + if (!is_array($map)) throw new \Exception('Could not decode json: ' . json_last_error_msg()); |
|
22 | 22 | |
23 | - return $map; |
|
24 | - } |
|
23 | + return $map; |
|
24 | + } |
|
25 | 25 | |
26 | - private function isJsonFile($json) { |
|
27 | - return trim($json)[0] != '{' && trim($json)[0] != '['; |
|
28 | - } |
|
26 | + private function isJsonFile($json) { |
|
27 | + return trim($json)[0] != '{' && trim($json)[0] != '['; |
|
28 | + } |
|
29 | 29 | } |
@@ -16,7 +16,9 @@ discard block |
||
16 | 16 | } |
17 | 17 | |
18 | 18 | public function match($name, $args, \DomElement $element) { |
19 | - if ($name !== 'not') return true; |
|
19 | + if ($name !== 'not') { |
|
20 | + return true; |
|
21 | + } |
|
20 | 22 | |
21 | 23 | $xpath = new \DomXpath($element->ownerDocument); |
22 | 24 | return $this->notElement($args, $xpath, $element); |
@@ -29,7 +31,9 @@ discard block |
||
29 | 31 | //Find all nodes matched by the expressions in the brackets :not(EXPR) |
30 | 32 | foreach ($xpath->query($xpathString) as $matchedElement) { |
31 | 33 | //Check to see whether this node was matched by the not query |
32 | - if ($element->isSameNode($matchedElement)) return false; |
|
34 | + if ($element->isSameNode($matchedElement)) { |
|
35 | + return false; |
|
36 | + } |
|
33 | 37 | } |
34 | 38 | } |
35 | 39 | return true; |
@@ -14,7 +14,9 @@ |
||
14 | 14 | } |
15 | 15 | |
16 | 16 | public function match($name, $args, \DomElement $element) { |
17 | - if (!($name === null || in_array($name, ['data', 'iteration', 'root']))) return true; |
|
17 | + if (!($name === null || in_array($name, ['data', 'iteration', 'root']))) { |
|
18 | + return true; |
|
19 | + } |
|
18 | 20 | return $args[0]; |
19 | 21 | } |
20 | 22 | } |
@@ -28,6 +28,9 @@ discard block |
||
28 | 28 | } |
29 | 29 | } |
30 | 30 | |
31 | + /** |
|
32 | + * @param \DOMElement $element |
|
33 | + */ |
|
31 | 34 | private function shouldRun($element) { |
32 | 35 | do { |
33 | 36 | if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
@@ -40,6 +43,10 @@ discard block |
||
40 | 43 | return (isset($rules['content-mode'])) ? $rules['content-mode'] : 'append'; |
41 | 44 | } |
42 | 45 | |
46 | + /** |
|
47 | + * @param \DOMElement $element |
|
48 | + * @param \Transphporm\Hook\PseudoMatcher $pseudoMatcher |
|
49 | + */ |
|
43 | 50 | private function processPseudo($value, $element, $pseudoMatcher) { |
44 | 51 | $pseudoContent = ['attr', 'header', 'before', 'after']; |
45 | 52 | foreach ($pseudoContent as $pseudo) { |
@@ -96,6 +103,9 @@ discard block |
||
96 | 103 | } |
97 | 104 | } |
98 | 105 | |
106 | + /** |
|
107 | + * @param \DOMElement $element |
|
108 | + */ |
|
99 | 109 | private function replaceContent($element, $content) { |
100 | 110 | //If this rule was cached, the elements that were added last time need to be removed prior to running the rule again. |
101 | 111 | foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
@@ -104,12 +114,18 @@ discard block |
||
104 | 114 | $element->setAttribute('transphporm', 'remove'); |
105 | 115 | } |
106 | 116 | |
117 | + /** |
|
118 | + * @param \DOMElement $element |
|
119 | + */ |
|
107 | 120 | private function appendContent($element, $content) { |
108 | 121 | foreach ($this->getNode($content, $element->ownerDocument) as $node) { |
109 | 122 | $element->appendChild($node); |
110 | 123 | } |
111 | 124 | } |
112 | 125 | |
126 | + /** |
|
127 | + * @param \DOMElement $element |
|
128 | + */ |
|
113 | 129 | private function removeAllChildren($element) { |
114 | 130 | while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
115 | 131 | } |
@@ -15,21 +15,28 @@ discard block |
||
15 | 15 | } |
16 | 16 | |
17 | 17 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
18 | - if (!$this->shouldRun($element)) return false; |
|
18 | + if (!$this->shouldRun($element)) { |
|
19 | + return false; |
|
20 | + } |
|
19 | 21 | $values = $this->formatter->format($values, $rules); |
20 | 22 | |
21 | 23 | if (!$this->processPseudo($values, $element, $pseudoMatcher)) { |
22 | 24 | //Remove the current contents |
23 | 25 | $this->removeAllChildren($element); |
24 | 26 | //Now make a text node |
25 | - if ($this->getContentMode($rules) === 'replace') $this->replaceContent($element, $values); |
|
26 | - else $this->appendContent($element, $values); |
|
27 | + if ($this->getContentMode($rules) === 'replace') { |
|
28 | + $this->replaceContent($element, $values); |
|
29 | + } else { |
|
30 | + $this->appendContent($element, $values); |
|
31 | + } |
|
27 | 32 | } |
28 | 33 | } |
29 | 34 | |
30 | 35 | private function shouldRun($element) { |
31 | 36 | do { |
32 | - if ($element->getAttribute('transphporm') == 'includedtemplate') return false; |
|
37 | + if ($element->getAttribute('transphporm') == 'includedtemplate') { |
|
38 | + return false; |
|
39 | + } |
|
33 | 40 | } |
34 | 41 | while (($element = $element->parentNode) instanceof \DomElement); |
35 | 42 | return true; |
@@ -53,9 +60,10 @@ discard block |
||
53 | 60 | private function getNode($node, $document) { |
54 | 61 | foreach ($node as $n) { |
55 | 62 | if (is_array($n)) { |
56 | - foreach ($this->getNode($n, $document) as $new) yield $new; |
|
57 | - } |
|
58 | - else { |
|
63 | + foreach ($this->getNode($n, $document) as $new) { |
|
64 | + yield $new; |
|
65 | + } |
|
66 | + } else { |
|
59 | 67 | yield $this->convertNode($n, $document); |
60 | 68 | } |
61 | 69 | } |
@@ -66,9 +74,10 @@ discard block |
||
66 | 74 | $new = $document->importNode($node, true); |
67 | 75 | //Removing this might cause problems with caching... |
68 | 76 | //$new->setAttribute('transphporm', 'added'); |
69 | - } |
|
70 | - else { |
|
71 | - if ($node instanceof \DomText) $node = $node->nodeValue; |
|
77 | + } else { |
|
78 | + if ($node instanceof \DomText) { |
|
79 | + $node = $node->nodeValue; |
|
80 | + } |
|
72 | 81 | $new = $document->createElement('text'); |
73 | 82 | |
74 | 83 | $new->appendChild($document->createTextNode($node)); |
@@ -114,6 +123,8 @@ discard block |
||
114 | 123 | } |
115 | 124 | |
116 | 125 | private function removeAllChildren($element) { |
117 | - while ($element->hasChildNodes()) $element->removeChild($element->firstChild); |
|
126 | + while ($element->hasChildNodes()) { |
|
127 | + $element->removeChild($element->firstChild); |
|
128 | + } |
|
118 | 129 | } |
119 | 130 | } |
@@ -49,11 +49,11 @@ |
||
49 | 49 | $functionSet->setElement($element[0]); |
50 | 50 | |
51 | 51 | $attributes = array(); |
52 | - foreach($element[0]->attributes as $attribute_name => $attribute_node) { |
|
53 | - $attributes[$attribute_name] = $attribute_node->nodeValue; |
|
54 | - } |
|
52 | + foreach($element[0]->attributes as $attribute_name => $attribute_node) { |
|
53 | + $attributes[$attribute_name] = $attribute_node->nodeValue; |
|
54 | + } |
|
55 | 55 | |
56 | - $parser = new \Transphporm\Parser\Value($functionSet, true); |
|
56 | + $parser = new \Transphporm\Parser\Value($functionSet, true); |
|
57 | 57 | $return = $parser->parseTokens($attr, $attributes); |
58 | 58 | |
59 | 59 | return $return[0] === '' ? false : $return[0]; |
@@ -18,12 +18,12 @@ discard block |
||
18 | 18 | $this->functionSet = $functionSet; |
19 | 19 | |
20 | 20 | $this->translators = [ |
21 | - Tokenizer::WHITESPACE => function($string) use ($prefix) { return '//' . $prefix . $string; }, |
|
22 | - '' => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
|
23 | - Tokenizer::GREATER_THAN => function($string) use ($prefix) { return '/' . $prefix . $string; }, |
|
24 | - Tokenizer::NUM_SIGN => function($string) { return '[@id=\'' . $string . '\']'; }, |
|
25 | - Tokenizer::DOT => function($string) { return '[contains(concat(\' \', normalize-space(@class), \' \'), \' ' . $string . ' \')]'; }, |
|
26 | - Tokenizer::OPEN_SQUARE_BRACKET => function($string) use ($hash) { return '[' .'php:function(\'\Transphporm\Parser\CssToXpath::processAttr\', \'' . base64_encode(serialize($string)) . '\', ., "' . $hash . '")' . ']'; } |
|
21 | + Tokenizer::WHITESPACE => function($string) use ($prefix) { return '//'.$prefix.$string; }, |
|
22 | + '' => function($string) use ($prefix) { return '/'.$prefix.$string; }, |
|
23 | + Tokenizer::GREATER_THAN => function($string) use ($prefix) { return '/'.$prefix.$string; }, |
|
24 | + Tokenizer::NUM_SIGN => function($string) { return '[@id=\''.$string.'\']'; }, |
|
25 | + Tokenizer::DOT => function($string) { return '[contains(concat(\' \', normalize-space(@class), \' \'), \' '.$string.' \')]'; }, |
|
26 | + Tokenizer::OPEN_SQUARE_BRACKET => function($string) use ($hash) { return '['.'php:function(\'\Transphporm\Parser\CssToXpath::processAttr\', \''.base64_encode(serialize($string)).'\', ., "'.$hash.'")'.']'; } |
|
27 | 27 | ]; |
28 | 28 | } |
29 | 29 | |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | $functionSet->setElement($element[0]); |
48 | 48 | |
49 | 49 | $attributes = array(); |
50 | - foreach($element[0]->attributes as $attribute_name => $attribute_node) { |
|
50 | + foreach ($element[0]->attributes as $attribute_name => $attribute_node) { |
|
51 | 51 | $attributes[$attribute_name] = $attribute_node->nodeValue; |
52 | 52 | } |
53 | 53 | |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | $tokens = []; |
79 | 79 | |
80 | 80 | foreach ($css->splitOnToken(Tokenizer::GREATER_THAN) as $token) { |
81 | - foreach ($token->trim() as $t) $tokens[] = $t; |
|
81 | + foreach ($token->trim() as $t) $tokens[] = $t; |
|
82 | 82 | $tokens[] = ['type' => Tokenizer::GREATER_THAN]; |
83 | 83 | } |
84 | 84 | $tokens = new Tokens(array_slice($tokens, 0, -1)); |
@@ -69,7 +69,9 @@ discard block |
||
69 | 69 | $selector->type = $token['type']; |
70 | 70 | $selectors[] = $selector; |
71 | 71 | } |
72 | - if (isset($token['value'])) $selectors[count($selectors)-1]->string = $token['value']; |
|
72 | + if (isset($token['value'])) { |
|
73 | + $selectors[count($selectors)-1]->string = $token['value']; |
|
74 | + } |
|
73 | 75 | } |
74 | 76 | return $selectors; |
75 | 77 | } |
@@ -79,7 +81,9 @@ discard block |
||
79 | 81 | $selectors = $this->split($css); |
80 | 82 | $xpath = '/'; |
81 | 83 | foreach ($selectors as $selector) { |
82 | - if (isset($this->translators[$selector->type])) $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
84 | + if (isset($this->translators[$selector->type])) { |
|
85 | + $xpath .= $this->translators[$selector->type]($selector->string, $xpath); |
|
86 | + } |
|
83 | 87 | } |
84 | 88 | |
85 | 89 | $xpath = str_replace('/[', '/*[', $xpath); |
@@ -90,7 +94,9 @@ discard block |
||
90 | 94 | private function removeSpacesFromDirectDecend($css) { |
91 | 95 | $tokens = []; |
92 | 96 | foreach ($css->splitOnToken(Tokenizer::GREATER_THAN) as $token) { |
93 | - foreach ($token->trim() as $t) $tokens[] = $t; |
|
97 | + foreach ($token->trim() as $t) { |
|
98 | + $tokens[] = $t; |
|
99 | + } |
|
94 | 100 | $tokens[] = ['type' => Tokenizer::GREATER_THAN]; |
95 | 101 | } |
96 | 102 | return new Tokens(array_slice($tokens, 0, -1)); |
@@ -7,8 +7,12 @@ |
||
7 | 7 | namespace Transphporm\Property; |
8 | 8 | class Display implements \Transphporm\Property { |
9 | 9 | public function run(array $values, \DomElement $element, array $rules, \Transphporm\Hook\PseudoMatcher $pseudoMatcher, array $properties = []) { |
10 | - if ($pseudoMatcher->hasFunction('attr')) $element->removeAttribute($pseudoMatcher->getFuncArgs('attr')[0]); |
|
11 | - else if (strtolower($values[0]) === 'none') $element->setAttribute('transphporm', 'remove'); |
|
12 | - else $element->setAttribute('transphporm', 'show'); |
|
10 | + if ($pseudoMatcher->hasFunction('attr')) { |
|
11 | + $element->removeAttribute($pseudoMatcher->getFuncArgs('attr')[0]); |
|
12 | + } else if (strtolower($values[0]) === 'none') { |
|
13 | + $element->setAttribute('transphporm', 'remove'); |
|
14 | + } else { |
|
15 | + $element->setAttribute('transphporm', 'show'); |
|
16 | + } |
|
13 | 17 | } |
14 | 18 | } |
@@ -78,7 +78,7 @@ |
||
78 | 78 | if ($token['type'] === $tokenType) $i++; |
79 | 79 | else $splitTokens[$i][] = $token; |
80 | 80 | } |
81 | - return array_map(function ($tokens) { |
|
81 | + return array_map(function($tokens) { |
|
82 | 82 | return new Tokens($tokens); |
83 | 83 | }, $splitTokens); |
84 | 84 | //return $splitTokens; |
@@ -44,23 +44,35 @@ discard block |
||
44 | 44 | |
45 | 45 | private function getKeyToSlice($tokenType) { |
46 | 46 | $keys = $this->getKeysOfTokenType($tokenType); |
47 | - if (empty($keys)) return false; |
|
47 | + if (empty($keys)) { |
|
48 | + return false; |
|
49 | + } |
|
48 | 50 | $key = $keys[0]; |
49 | - for ($i = 0; $key < $this->iterator; $i++) $key = $keys[$i]; |
|
51 | + for ($i = 0; $key < $this->iterator; $i++) { |
|
52 | + $key = $keys[$i]; |
|
53 | + } |
|
50 | 54 | return $key; |
51 | 55 | } |
52 | 56 | |
53 | 57 | public function from($tokenType, $inclusive = false) { |
54 | 58 | $key = $this->getKeyToSlice($tokenType); |
55 | - if ($key === false) return new Tokens([]); |
|
56 | - if (!$inclusive) $key++; |
|
59 | + if ($key === false) { |
|
60 | + return new Tokens([]); |
|
61 | + } |
|
62 | + if (!$inclusive) { |
|
63 | + $key++; |
|
64 | + } |
|
57 | 65 | return new Tokens(array_slice($this->tokens, $key)); |
58 | 66 | } |
59 | 67 | |
60 | 68 | public function to($tokenType, $inclusive = false) { |
61 | 69 | $key = $this->getKeyToSlice($tokenType); |
62 | - if ($key === false) return new Tokens([]); |
|
63 | - if ($inclusive) $key++; |
|
70 | + if ($key === false) { |
|
71 | + return new Tokens([]); |
|
72 | + } |
|
73 | + if ($inclusive) { |
|
74 | + $key++; |
|
75 | + } |
|
64 | 76 | return new Tokens(array_slice($this->tokens, 0, $key)); |
65 | 77 | } |
66 | 78 | |
@@ -72,8 +84,11 @@ discard block |
||
72 | 84 | $splitTokens = []; |
73 | 85 | $i = 0; |
74 | 86 | foreach ($this->tokens as $token) { |
75 | - if ($token['type'] === $tokenType) $i++; |
|
76 | - else $splitTokens[$i][] = $token; |
|
87 | + if ($token['type'] === $tokenType) { |
|
88 | + $i++; |
|
89 | + } else { |
|
90 | + $splitTokens[$i][] = $token; |
|
91 | + } |
|
77 | 92 | } |
78 | 93 | return array_map(function ($tokens) { |
79 | 94 | return new Tokens($tokens); |
@@ -6,28 +6,28 @@ discard block |
||
6 | 6 | * @version 1.0 */ |
7 | 7 | namespace Transphporm\Parser; |
8 | 8 | class Tokens implements \Iterator, \Countable { |
9 | - private $tokens; |
|
10 | - private $iterator = 0; |
|
9 | + private $tokens; |
|
10 | + private $iterator = 0; |
|
11 | 11 | |
12 | - public function __construct(array $tokens) { |
|
13 | - $this->tokens = $tokens; |
|
14 | - } |
|
12 | + public function __construct(array $tokens) { |
|
13 | + $this->tokens = $tokens; |
|
14 | + } |
|
15 | 15 | |
16 | - public function count() { |
|
17 | - return count($this->tokens); |
|
18 | - } |
|
16 | + public function count() { |
|
17 | + return count($this->tokens); |
|
18 | + } |
|
19 | 19 | |
20 | - // Iterator Functions |
|
21 | - public function current() { |
|
22 | - return $this->tokens[$this->iterator]; |
|
23 | - } |
|
20 | + // Iterator Functions |
|
21 | + public function current() { |
|
22 | + return $this->tokens[$this->iterator]; |
|
23 | + } |
|
24 | 24 | |
25 | - public function key() { |
|
26 | - return $this->iterator; |
|
27 | - } |
|
25 | + public function key() { |
|
26 | + return $this->iterator; |
|
27 | + } |
|
28 | 28 | |
29 | - public function next() { |
|
30 | - ++$this->iterator; |
|
29 | + public function next() { |
|
30 | + ++$this->iterator; |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | public function valid() { |
@@ -38,67 +38,67 @@ discard block |
||
38 | 38 | $this->iterator = 0; |
39 | 39 | } |
40 | 40 | |
41 | - private function getKeysOfTokenType($tokenType) { |
|
42 | - return array_keys(array_column($this->tokens, 'type'), $tokenType); |
|
43 | - } |
|
44 | - |
|
45 | - private function getKeyToSlice($tokenType) { |
|
46 | - $keys = $this->getKeysOfTokenType($tokenType); |
|
47 | - if (empty($keys)) return false; |
|
48 | - $key = $keys[0]; |
|
49 | - for ($i = 0; $key < $this->iterator; $i++) $key = $keys[$i]; |
|
50 | - return $key; |
|
51 | - } |
|
52 | - |
|
53 | - public function from($tokenType, $inclusive = false) { |
|
54 | - $key = $this->getKeyToSlice($tokenType); |
|
55 | - if ($key === false) return new Tokens([]); |
|
56 | - if (!$inclusive) $key++; |
|
57 | - return new Tokens(array_slice($this->tokens, $key)); |
|
58 | - } |
|
59 | - |
|
60 | - public function to($tokenType, $inclusive = false) { |
|
61 | - $key = $this->getKeyToSlice($tokenType); |
|
62 | - if ($key === false) return new Tokens([]); |
|
63 | - if ($inclusive) $key++; |
|
64 | - return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
65 | - } |
|
66 | - |
|
67 | - public function skip($count) { |
|
68 | - $this->iterator += $count; |
|
69 | - } |
|
70 | - |
|
71 | - public function splitOnToken($tokenType) { |
|
72 | - $splitTokens = []; |
|
41 | + private function getKeysOfTokenType($tokenType) { |
|
42 | + return array_keys(array_column($this->tokens, 'type'), $tokenType); |
|
43 | + } |
|
44 | + |
|
45 | + private function getKeyToSlice($tokenType) { |
|
46 | + $keys = $this->getKeysOfTokenType($tokenType); |
|
47 | + if (empty($keys)) return false; |
|
48 | + $key = $keys[0]; |
|
49 | + for ($i = 0; $key < $this->iterator; $i++) $key = $keys[$i]; |
|
50 | + return $key; |
|
51 | + } |
|
52 | + |
|
53 | + public function from($tokenType, $inclusive = false) { |
|
54 | + $key = $this->getKeyToSlice($tokenType); |
|
55 | + if ($key === false) return new Tokens([]); |
|
56 | + if (!$inclusive) $key++; |
|
57 | + return new Tokens(array_slice($this->tokens, $key)); |
|
58 | + } |
|
59 | + |
|
60 | + public function to($tokenType, $inclusive = false) { |
|
61 | + $key = $this->getKeyToSlice($tokenType); |
|
62 | + if ($key === false) return new Tokens([]); |
|
63 | + if ($inclusive) $key++; |
|
64 | + return new Tokens(array_slice($this->tokens, $this->iterator, $key)); |
|
65 | + } |
|
66 | + |
|
67 | + public function skip($count) { |
|
68 | + $this->iterator += $count; |
|
69 | + } |
|
70 | + |
|
71 | + public function splitOnToken($tokenType) { |
|
72 | + $splitTokens = []; |
|
73 | 73 | $i = 0; |
74 | 74 | foreach ($this->tokens as $token) { |
75 | 75 | if ($token['type'] === $tokenType) $i++; |
76 | 76 | else $splitTokens[$i][] = $token; |
77 | 77 | } |
78 | - return array_map(function ($tokens) { |
|
79 | - return new Tokens($tokens); |
|
80 | - }, $splitTokens); |
|
78 | + return array_map(function ($tokens) { |
|
79 | + return new Tokens($tokens); |
|
80 | + }, $splitTokens); |
|
81 | 81 | //return $splitTokens; |
82 | - } |
|
83 | - |
|
84 | - public function trim() { |
|
85 | - $tokens = $this->tokens; |
|
86 | - // Remove end whitespace |
|
87 | - while (end($tokens)['type'] === Tokenizer::WHITESPACE) { |
|
88 | - array_pop($tokens); |
|
89 | - } |
|
90 | - // Remove begining whitespace |
|
91 | - while (isset($tokens[0]) && $tokens[0]['type'] === Tokenizer::WHITESPACE) { |
|
92 | - array_shift($tokens); |
|
93 | - } |
|
94 | - return new Tokens($tokens); |
|
95 | - } |
|
96 | - |
|
97 | - public function read($offset = 0) { |
|
98 | - return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false; |
|
99 | - } |
|
100 | - |
|
101 | - public function type($offset = 0) { |
|
102 | - return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false; |
|
103 | - } |
|
82 | + } |
|
83 | + |
|
84 | + public function trim() { |
|
85 | + $tokens = $this->tokens; |
|
86 | + // Remove end whitespace |
|
87 | + while (end($tokens)['type'] === Tokenizer::WHITESPACE) { |
|
88 | + array_pop($tokens); |
|
89 | + } |
|
90 | + // Remove begining whitespace |
|
91 | + while (isset($tokens[0]) && $tokens[0]['type'] === Tokenizer::WHITESPACE) { |
|
92 | + array_shift($tokens); |
|
93 | + } |
|
94 | + return new Tokens($tokens); |
|
95 | + } |
|
96 | + |
|
97 | + public function read($offset = 0) { |
|
98 | + return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false; |
|
99 | + } |
|
100 | + |
|
101 | + public function type($offset = 0) { |
|
102 | + return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false; |
|
103 | + } |
|
104 | 104 | } |
@@ -102,14 +102,14 @@ |
||
102 | 102 | } |
103 | 103 | |
104 | 104 | private function getProperties($tokens) { |
105 | - $rules = $tokens->splitOnToken(Tokenizer::SEMI_COLON); |
|
105 | + $rules = $tokens->splitOnToken(Tokenizer::SEMI_COLON); |
|
106 | 106 | |
107 | - $return = []; |
|
108 | - foreach ($rules as $rule) { |
|
109 | - $name = $rule->from(Tokenizer::NAME, true)->to(Tokenizer::COLON)->read(); |
|
110 | - $return[$name] = $rule->from(Tokenizer::COLON)->trim(); |
|
111 | - } |
|
107 | + $return = []; |
|
108 | + foreach ($rules as $rule) { |
|
109 | + $name = $rule->from(Tokenizer::NAME, true)->to(Tokenizer::COLON)->read(); |
|
110 | + $return[$name] = $rule->from(Tokenizer::COLON)->trim(); |
|
111 | + } |
|
112 | 112 | |
113 | - return $return; |
|
114 | - } |
|
113 | + return $return; |
|
114 | + } |
|
115 | 115 | } |
@@ -43,6 +43,9 @@ discard block |
||
43 | 43 | return $this->cache->write($this->file, $rules, $this->import); |
44 | 44 | } |
45 | 45 | |
46 | + /** |
|
47 | + * @param integer $indexStart |
|
48 | + */ |
|
46 | 49 | private function parseTokens($indexStart) { |
47 | 50 | $this->rules = []; |
48 | 51 | $line = 1; |
@@ -60,6 +63,9 @@ discard block |
||
60 | 63 | return $this->rules; |
61 | 64 | } |
62 | 65 | |
66 | + /** |
|
67 | + * @param integer $line |
|
68 | + */ |
|
63 | 69 | private function addRules($token, $indexStart, $line) { |
64 | 70 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
65 | 71 | $this->tss->skip(count($selector)); |
@@ -73,6 +79,9 @@ discard block |
||
73 | 79 | if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
74 | 80 | } |
75 | 81 | |
82 | + /** |
|
83 | + * @param integer $index |
|
84 | + */ |
|
76 | 85 | private function CssToRules($selector, $index, $properties, $line) { |
77 | 86 | $parts = $selector->trim()->splitOnToken(Tokenizer::ARG); |
78 | 87 | $rules = []; |
@@ -96,6 +105,9 @@ discard block |
||
96 | 105 | return $rules; |
97 | 106 | } |
98 | 107 | |
108 | + /** |
|
109 | + * @param integer $indexStart |
|
110 | + */ |
|
99 | 111 | private function processingInstructions($token, $indexStart) { |
100 | 112 | if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
101 | 113 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
@@ -123,6 +135,10 @@ discard block |
||
123 | 135 | return ($a->depth < $b->depth) ? -1 : 1; |
124 | 136 | } |
125 | 137 | |
138 | + /** |
|
139 | + * @param string $open |
|
140 | + * @param string $close |
|
141 | + */ |
|
126 | 142 | private function stripComments($str, $open, $close) { |
127 | 143 | $pos = 0; |
128 | 144 | while (($pos = strpos($str, $open, $pos)) !== false) { |
@@ -24,7 +24,7 @@ |
||
24 | 24 | if (is_file($tss)) { |
25 | 25 | $this->file = $tss; |
26 | 26 | $this->rules = $this->cache->load($tss); |
27 | - $baseDir = dirname(realpath($tss)) . DIRECTORY_SEPARATOR; |
|
27 | + $baseDir = dirname(realpath($tss)).DIRECTORY_SEPARATOR; |
|
28 | 28 | if (empty($this->rules)) $tss = file_get_contents($tss); |
29 | 29 | else return; |
30 | 30 | } |
@@ -25,8 +25,11 @@ discard block |
||
25 | 25 | $this->file = $tss; |
26 | 26 | $this->rules = $this->cache->load($tss); |
27 | 27 | $baseDir = dirname(realpath($tss)) . DIRECTORY_SEPARATOR; |
28 | - if (empty($this->rules)) $tss = file_get_contents($tss); |
|
29 | - else return; |
|
28 | + if (empty($this->rules)) { |
|
29 | + $tss = file_get_contents($tss); |
|
30 | + } else { |
|
31 | + return; |
|
32 | + } |
|
30 | 33 | } |
31 | 34 | $this->tss = $this->stripComments($tss, '//', "\n"); |
32 | 35 | $this->tss = $this->stripComments($this->tss, '/*', '*/'); |
@@ -38,7 +41,9 @@ discard block |
||
38 | 41 | } |
39 | 42 | |
40 | 43 | public function parse($indexStart = 0) { |
41 | - if (!empty($this->rules)) return $this->rules['rules']; |
|
44 | + if (!empty($this->rules)) { |
|
45 | + return $this->rules['rules']; |
|
46 | + } |
|
42 | 47 | $rules = $this->parseTokens($indexStart); |
43 | 48 | usort($rules, [$this, 'sortRules']); |
44 | 49 | $this->checkError($rules); |
@@ -52,12 +57,12 @@ discard block |
||
52 | 57 | if ($processing = $this->processingInstructions($token, count($this->rules)+$indexStart)) { |
53 | 58 | $this->rules = array_merge($this->rules, $processing); |
54 | 59 | continue; |
55 | - } |
|
56 | - else if ($token['type'] === Tokenizer::NEW_LINE) { |
|
60 | + } else if ($token['type'] === Tokenizer::NEW_LINE) { |
|
57 | 61 | $line++; |
58 | 62 | continue; |
63 | + } else { |
|
64 | + $this->addRules($token, $indexStart, $line); |
|
59 | 65 | } |
60 | - else $this->addRules($token, $indexStart, $line); |
|
61 | 66 | } |
62 | 67 | return $this->rules; |
63 | 68 | } |
@@ -65,14 +70,18 @@ discard block |
||
65 | 70 | private function addRules($token, $indexStart, $line) { |
66 | 71 | $selector = $this->tss->from($token['type'], true)->to(Tokenizer::OPEN_BRACE); |
67 | 72 | $this->tss->skip(count($selector)); |
68 | - if (count($selector) === 0) return; |
|
73 | + if (count($selector) === 0) { |
|
74 | + return; |
|
75 | + } |
|
69 | 76 | |
70 | 77 | $newRules = $this->cssToRules($selector, count($this->rules)+$indexStart, $this->getProperties($this->tss->current()['value']), $line); |
71 | 78 | $this->rules = $this->writeRule($this->rules, $newRules); |
72 | 79 | } |
73 | 80 | |
74 | 81 | private function checkError($rules) { |
75 | - if (empty($rules) && count($this->tss) > 0) throw new \Exception('No TSS rules parsed'); |
|
82 | + if (empty($rules) && count($this->tss) > 0) { |
|
83 | + throw new \Exception('No TSS rules parsed'); |
|
84 | + } |
|
76 | 85 | } |
77 | 86 | |
78 | 87 | private function CssToRules($selector, $index, $properties, $line) { |
@@ -99,7 +108,9 @@ discard block |
||
99 | 108 | } |
100 | 109 | |
101 | 110 | private function processingInstructions($token, $indexStart) { |
102 | - if ($token['type'] !== Tokenizer::AT_SIGN) return false; |
|
111 | + if ($token['type'] !== Tokenizer::AT_SIGN) { |
|
112 | + return false; |
|
113 | + } |
|
103 | 114 | $tokens = $this->tss->from(Tokenizer::AT_SIGN, false)->to(Tokenizer::SEMI_COLON, false); |
104 | 115 | $funcName = $tokens->from(Tokenizer::NAME, true)->read(); |
105 | 116 | $args = $this->valueParser->parseTokens($tokens->from(Tokenizer::NAME)); |
@@ -111,8 +122,11 @@ discard block |
||
111 | 122 | } |
112 | 123 | |
113 | 124 | private function import($args, $indexStart) { |
114 | - if ($this->file !== null) $fileName = $fileName = $this->filePath->getFilePath($args[0]); |
|
115 | - else $fileName = $args[0]; |
|
125 | + if ($this->file !== null) { |
|
126 | + $fileName = $fileName = $this->filePath->getFilePath($args[0]); |
|
127 | + } else { |
|
128 | + $fileName = $args[0]; |
|
129 | + } |
|
116 | 130 | $this->import[] = $fileName; |
117 | 131 | $sheet = new Sheet($fileName, $this->baseDir, $this->xPath, $this->valueParser, $this->cache, $this->filePath); |
118 | 132 | return $sheet->parse($indexStart); |
@@ -120,7 +134,9 @@ discard block |
||
120 | 134 | |
121 | 135 | private function sortRules($a, $b) { |
122 | 136 | //If they have the same depth, compare on index |
123 | - if ($a->depth === $b->depth) return $a->index < $b->index ? -1 : 1; |
|
137 | + if ($a->depth === $b->depth) { |
|
138 | + return $a->index < $b->index ? -1 : 1; |
|
139 | + } |
|
124 | 140 | |
125 | 141 | return ($a->depth < $b->depth) ? -1 : 1; |
126 | 142 | } |
@@ -129,7 +145,9 @@ discard block |
||
129 | 145 | $pos = 0; |
130 | 146 | while (($pos = strpos($str, $open, $pos)) !== false) { |
131 | 147 | $end = strpos($str, $close, $pos); |
132 | - if ($end === false) break; |
|
148 | + if ($end === false) { |
|
149 | + break; |
|
150 | + } |
|
133 | 151 | $str = substr_replace($str, '', $pos, $end-$pos+strlen($close)); |
134 | 152 | } |
135 | 153 |
@@ -67,12 +67,16 @@ |
||
67 | 67 | } |
68 | 68 | |
69 | 69 | public function loadProperties(Hook\PropertyHook $hook) { |
70 | - foreach ($this->properties as $name => $property) $hook->registerProperty($name, $property); |
|
70 | + foreach ($this->properties as $name => $property) { |
|
71 | + $hook->registerProperty($name, $property); |
|
72 | + } |
|
71 | 73 | } |
72 | 74 | |
73 | 75 | public function createPseudoMatcher($pseudo) { |
74 | 76 | $pseudoMatcher = new Hook\PseudoMatcher($pseudo, $this->valueParser); |
75 | - foreach ($this->pseudo as $pseudoFunction) $pseudoMatcher->registerFunction(clone $pseudoFunction); |
|
77 | + foreach ($this->pseudo as $pseudoFunction) { |
|
78 | + $pseudoMatcher->registerFunction(clone $pseudoFunction); |
|
79 | + } |
|
76 | 80 | return $pseudoMatcher; |
77 | 81 | } |
78 | 82 |