Completed
Push — master ( 409692...b7c30d )
by Richard
02:24
created

Tokens   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 0
dl 0
loc 105
rs 9.6
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A count() 0 3 1
A current() 0 3 1
A key() 0 3 1
A next() 0 3 1
A valid() 0 3 1
A rewind() 0 3 1
A getKeysOfTokenType() 0 3 1
A getKeyToSlice() 0 7 4
A from() 0 3 1
A to() 0 3 1
A sliceTokens() 0 7 4
A skip() 0 3 1
A splitOnToken() 0 12 3
A trim() 0 12 4
A removeLine() 0 5 2
A read() 0 3 2
A type() 0 3 2
1
<?php
2
/* @description     Transformation Style Sheets - Revolutionising PHP templating    *
3
 * @author          Tom Butler [email protected]                                             *
4
 * @copyright       2015 Tom Butler <[email protected]> | https://r.je/                      *
5
 * @license         http://www.opensource.org/licenses/bsd-license.php  BSD License *
6
 * @version         1.0                                                             */
7
namespace Transphporm\Parser;
8
class Tokens implements \Iterator, \Countable {
9
    private $tokens;
10
    private $iterator = 0;
11
12
    public function __construct(array $tokens) {
13
        $this->tokens = $tokens;
14
    }
15
16
    public function count() {
17
        return count($this->tokens);
18
    }
19
20
    // Iterator Functions
21
    public function current() {
22
        return $this->tokens[$this->iterator];
23
    }
24
25
    public function key() {
26
        return $this->iterator;
27
    }
28
29
    public function next() {
30
        ++$this->iterator;
31
	}
32
33
	public function valid() {
34
		return isset($this->tokens[$this->iterator]);
35
	}
36
37
	public function rewind() {
38
		$this->iterator = 0;
39
	}
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 = [];
75
		$i = 0;
76
		foreach ($this->tokens as $token) {
77
			if ($token['type'] === $tokenType) $i++;
78
			else $splitTokens[$i][] = $token;
79
		}
80
        return array_map(function ($tokens) {
81
            return new Tokens($tokens);
82
        }, $splitTokens);
83
		//return $splitTokens;
84
    }
85
86
    public function trim() {
87
        $tokens = $this->tokens;
88
        // Remove end whitespace
89
        while (end($tokens)['type'] === Tokenizer::WHITESPACE) {
90
            array_pop($tokens);
91
        }
92
        // Remove begining whitespace
93
        while (isset($tokens[0]) && $tokens[0]['type'] === Tokenizer::WHITESPACE) {
94
            array_shift($tokens);
95
        }
96
        return new Tokens($tokens);
97
    }
98
99
    public function removeLine() {
100
        $tokens = $this->tokens;
101
        foreach ($tokens as &$token) unset($token['line']);
102
        return new Tokens($tokens);
103
    }
104
105
    public function read($offset = 0) {
106
        return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false;
107
    }
108
109
    public function type($offset = 0) {
110
        return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false;
111
    }
112
}
113