Completed
Push — master ( b7c30d...c878c6 )
by Richard
02:20
created
src/Parser/Tokens.php 1 patch
Indentation   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -6,28 +6,28 @@  discard block
 block discarded – undo
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,76 +38,76 @@  discard block
 block discarded – undo
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 && isset($keys[$i]); $i++) $key = $keys[$i];
50
-        return $key;
51
-    }
52
-
53
-    public function from($tokenType, $inclusive = false) {
54
-        return $this->sliceTokens($tokenType, "from", !$inclusive);
55
-    }
56
-
57
-    public function to($tokenType, $inclusive = false) {
58
-        return $this->sliceTokens($tokenType, "to", $inclusive);
59
-    }
60
-
61
-    private function sliceTokens($tokenType, $type, $increment = false) {
62
-        $key = $this->getKeyToSlice($tokenType);
63
-        if ($key === false) return new Tokens([]);
64
-        if ($increment) $key++;
65
-        if ($type === "from") return new Tokens(array_slice($this->tokens, $key));
66
-        else return new Tokens(array_slice($this->tokens, $this->iterator, $key));
67
-    }
68
-
69
-    public function skip($count) {
70
-        $this->iterator += $count;
71
-    }
72
-
73
-    public function splitOnToken($tokenType) {
74
-        $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 && isset($keys[$i]); $i++) $key = $keys[$i];
50
+		return $key;
51
+	}
52
+
53
+	public function from($tokenType, $inclusive = false) {
54
+		return $this->sliceTokens($tokenType, "from", !$inclusive);
55
+	}
56
+
57
+	public function to($tokenType, $inclusive = false) {
58
+		return $this->sliceTokens($tokenType, "to", $inclusive);
59
+	}
60
+
61
+	private function sliceTokens($tokenType, $type, $increment = false) {
62
+		$key = $this->getKeyToSlice($tokenType);
63
+		if ($key === false) return new Tokens([]);
64
+		if ($increment) $key++;
65
+		if ($type === "from") return new Tokens(array_slice($this->tokens, $key));
66
+		else return new Tokens(array_slice($this->tokens, $this->iterator, $key));
67
+	}
68
+
69
+	public function skip($count) {
70
+		$this->iterator += $count;
71
+	}
72
+
73
+	public function splitOnToken($tokenType) {
74
+		$splitTokens = [];
75 75
 		$i = 0;
76 76
 		foreach ($this->tokens as $token) {
77 77
 			if ($token['type'] === $tokenType) $i++;
78 78
 			else $splitTokens[$i][] = $token;
79 79
 		}
80
-        return array_map(function ($tokens) {
81
-            return new Tokens($tokens);
82
-        }, $splitTokens);
80
+		return array_map(function ($tokens) {
81
+			return new Tokens($tokens);
82
+		}, $splitTokens);
83 83
 		//return $splitTokens;
84
-    }
85
-
86
-    public function trim() {
87
-        $tokens = $this->tokens;
88
-        $tokensToTrim = [Tokenizer::WHITESPACE, Tokenizer::NEW_LINE];
89
-        // Remove end whitespace
90
-        while (in_array(end($tokens)['type'], $tokensToTrim)) {
91
-            array_pop($tokens);
92
-        }
93
-        // Remove begining whitespace
94
-        while (isset($tokens[0]) && in_array($tokens[0]['type'], $tokensToTrim)) {
95
-            array_shift($tokens);
96
-        }
97
-        return new Tokens($tokens);
98
-    }
99
-
100
-    public function removeLine() {
101
-        $tokens = $this->tokens;
102
-        foreach ($tokens as &$token) unset($token['line']);
103
-        return new Tokens($tokens);
104
-    }
105
-
106
-    public function read($offset = 0) {
107
-        return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false;
108
-    }
109
-
110
-    public function type($offset = 0) {
111
-        return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false;
112
-    }
84
+	}
85
+
86
+	public function trim() {
87
+		$tokens = $this->tokens;
88
+		$tokensToTrim = [Tokenizer::WHITESPACE, Tokenizer::NEW_LINE];
89
+		// Remove end whitespace
90
+		while (in_array(end($tokens)['type'], $tokensToTrim)) {
91
+			array_pop($tokens);
92
+		}
93
+		// Remove begining whitespace
94
+		while (isset($tokens[0]) && in_array($tokens[0]['type'], $tokensToTrim)) {
95
+			array_shift($tokens);
96
+		}
97
+		return new Tokens($tokens);
98
+	}
99
+
100
+	public function removeLine() {
101
+		$tokens = $this->tokens;
102
+		foreach ($tokens as &$token) unset($token['line']);
103
+		return new Tokens($tokens);
104
+	}
105
+
106
+	public function read($offset = 0) {
107
+		return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false;
108
+	}
109
+
110
+	public function type($offset = 0) {
111
+		return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false;
112
+	}
113 113
 }
Please login to merge, or discard this patch.