@@ -6,7 +6,7 @@ |
||
6 | 6 | * @version 1.0 */ |
7 | 7 | namespace Transphporm\TSSFunction; |
8 | 8 | /* Handles data() and iteration() function calls from the stylesheet */ |
9 | -class Data implements \Transphporm\TSSFunction{ |
|
9 | +class Data implements \Transphporm\TSSFunction { |
|
10 | 10 | private $data; |
11 | 11 | private $dataKey; |
12 | 12 | private $functionSet; |
@@ -18,8 +18,11 @@ |
||
18 | 18 | } |
19 | 19 | |
20 | 20 | public function run(array $args, \DomElement $element = null) { |
21 | - if ($this->dataKey === "root") $data = $this->data->getData(null, 'data'); |
|
22 | - else $data = $this->data->getData($element, $this->dataKey); |
|
21 | + if ($this->dataKey === "root") { |
|
22 | + $data = $this->data->getData(null, 'data'); |
|
23 | + } else { |
|
24 | + $data = $this->data->getData($element, $this->dataKey); |
|
25 | + } |
|
23 | 26 | $parser = new \Transphporm\Parser\Value($this->functionSet, true); |
24 | 27 | $return = $parser->parseTokens($args, $data); |
25 | 28 | return $return[0]; |
@@ -11,14 +11,14 @@ |
||
11 | 11 | $json = $args[0]; |
12 | 12 | |
13 | 13 | if (trim($json)[0] != '{') { |
14 | - $path = $this->baseDir . $json; |
|
15 | - if (!file_exists($path)) throw new \Exception('File does not exist at: ' . $path); |
|
14 | + $path = $this->baseDir.$json; |
|
15 | + if (!file_exists($path)) throw new \Exception('File does not exist at: '.$path); |
|
16 | 16 | $json = file_get_contents($json); |
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 | } |
@@ -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 | } |
@@ -1,29 +1,29 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Transphporm\TSSFunction; |
3 | 3 | class Json implements \Transphporm\TSSFunction { |
4 | - private $baseDir; |
|
4 | + private $baseDir; |
|
5 | 5 | |
6 | - public function __construct(&$baseDir) { |
|
7 | - $this->baseDir = &$baseDir; |
|
8 | - } |
|
6 | + public function __construct(&$baseDir) { |
|
7 | + $this->baseDir = &$baseDir; |
|
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->baseDir . $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->baseDir . $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 | } |
@@ -51,7 +51,7 @@ |
||
51 | 51 | |
52 | 52 | public function extract($last, $autoLookup) { |
53 | 53 | $value = $this->read($last); |
54 | - if ($value && ($autoLookup || is_array($this->data)) ) { |
|
54 | + if ($value && ($autoLookup || is_array($this->data))) { |
|
55 | 55 | return $value; |
56 | 56 | } |
57 | 57 | throw new \UnexpectedValueException('Not found'); |
@@ -14,16 +14,23 @@ discard block |
||
14 | 14 | } |
15 | 15 | |
16 | 16 | public function traverse($key) { |
17 | - if (isset($this->data->{$key})) $this->data = $this->data->{$key}; |
|
18 | - else if (is_array($this->data) && isset($this->data[$key])) $this->data = $this->data[$key]; |
|
17 | + if (isset($this->data->{$key})) { |
|
18 | + $this->data = $this->data->{$key}; |
|
19 | + } else if (is_array($this->data) && isset($this->data[$key])) { |
|
20 | + $this->data = $this->data[$key]; |
|
21 | + } |
|
19 | 22 | } |
20 | 23 | |
21 | 24 | public function read($value) { |
22 | 25 | if (is_array($this->data)) { |
23 | - if (isset($this->data[$value])) return $this->data[$value]; |
|
26 | + if (isset($this->data[$value])) { |
|
27 | + return $this->data[$value]; |
|
28 | + } |
|
29 | + } else if (isset($this->data->$value)) { |
|
30 | + return $this->data->$value; |
|
31 | + } else { |
|
32 | + return false; |
|
24 | 33 | } |
25 | - else if (isset($this->data->$value)) return $this->data->$value; |
|
26 | - else return false; |
|
27 | 34 | } |
28 | 35 | |
29 | 36 | public function isFunctionSet() { |
@@ -36,7 +43,9 @@ discard block |
||
36 | 43 | |
37 | 44 | public function parseNested($parser, $token, $funcName) { |
38 | 45 | $args = $parser->parseTokens($token['value'], $this->data); |
39 | - if ($args[0] == $this->data) $args = []; |
|
46 | + if ($args[0] == $this->data) { |
|
47 | + $args = []; |
|
48 | + } |
|
40 | 49 | return $this->callFunc($funcName, $args, $this->data); |
41 | 50 | } |
42 | 51 | |
@@ -45,8 +54,11 @@ discard block |
||
45 | 54 | } |
46 | 55 | |
47 | 56 | private function callFuncOnObject($obj, $func, $args) { |
48 | - if (isset($obj->$func) && is_callable($obj->$func)) return call_user_func_array($obj->$func, $args); |
|
49 | - else return call_user_func_array([$obj, $func], $args); |
|
57 | + if (isset($obj->$func) && is_callable($obj->$func)) { |
|
58 | + return call_user_func_array($obj->$func, $args); |
|
59 | + } else { |
|
60 | + return call_user_func_array([$obj, $func], $args); |
|
61 | + } |
|
50 | 62 | } |
51 | 63 | |
52 | 64 | public function extract($last, $autoLookup) { |
@@ -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; |
@@ -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, 0, $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, 0, $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 | } |
@@ -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); |