Completed
Push — master ( 0ada29...ecc437 )
by Richard
03:53 queued 11s
created

Tokens::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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 View Code Duplication
    public function from($tokenType, $inclusive = false) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
        $keys = $this->getKeysOfTokenType($tokenType);
47
        if (count($keys) === 0) return new Tokens([]);
48
        $key = $keys[0];
49
        for ($i = 0; $key < $this->iterator; $i++) $key = $keys[$i];
50
        if (!$inclusive) $key++;
51
        return new Tokens(array_slice($this->tokens, $key));
52
    }
53
54 View Code Duplication
    public function to($tokenType, $inclusive = false) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
        $keys = $this->getKeysOfTokenType($tokenType);
56
        if (empty($keys)) return new Tokens([]);
57
        $key = $keys[0];
58
        for ($i = 0; $key < $this->iterator; $i++) $key = $keys[$i];
59
        if ($inclusive) $key++;
60
        return new Tokens(array_slice($this->tokens, 0, $key));
61
    }
62
63
    public function skip($count) {
64
        $this->iterator += $count;
65
    }
66
67
    public function splitOnToken($tokenType) {
68
        $splitTokens = [];
69
		$i = 0;
70
		foreach ($this->tokens as $token) {
71
			if ($token['type'] === $tokenType) $i++;
72
			else $splitTokens[$i][] = $token;
73
		}
74
        return array_map(function ($tokens) {
75
            return new Tokens($tokens);
76
        }, $splitTokens);
77
		//return $splitTokens;
78
    }
79
80
    public function trim() {
81
        $tokens = $this->tokens;
82
        // Remove end whitespace
83
        while (end($tokens)['type'] === Tokenizer::WHITESPACE) {
84
            array_pop($tokens);
85
        }
86
        // Remove begining whitespace
87
        while (isset($tokens[0]) && $tokens[0]['type'] === Tokenizer::WHITESPACE) {
88
            array_shift($tokens);
89
        }
90
        return new Tokens($tokens);
91
    }
92
93
    public function read($offset = 0) {
94
        return isset($this->tokens[$offset]) ? $this->tokens[$offset]['value'] : false;
95
    }
96
97
    public function type($offset = 0) {
98
        return isset($this->tokens[$offset]) ? $this->tokens[$offset]['type'] : false;   
99
    }
100
}
101