-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTokens.php
59 lines (48 loc) · 1.37 KB
/
Tokens.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
class Tokens implements ArrayAccess, Countable {
private $tokens;
public function Tokens(array $tokens) {
$this->tokens = $tokens;
}
public function asString($offset, $length) {
if ($offset + $length > count($this->tokens)) {
// TODO meaningful Exception
throw new Exception();
}
$tokensAsString = '';
for ($i = $offset; $i < $offset + $length; $i++) {
$tokensAsString .= $this->tokens[$i]->asString();
}
return $tokensAsString;
}
public function asStringToCompare($offset, $length) {
if ($offset + $length > count($this->tokens)) {
// TODO meaningful Exception
throw new Exception();
}
$tokensAsStringToCompare = '';
for ($i = $offset; $i < $offset + $length; $i++) {
$tokensAsStringToCompare .= $this->tokens[$i]->asStringToCompare();
}
return $tokensAsStringToCompare;
}
public function count() {
return count($this->tokens);
}
public function offsetExists($offset) {
return isset($this->tokens[$offset]);
}
public function offsetGet($offset) {
return isset($this->tokens[$offset]) ? $this->tokens[$offset] : null;
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->tokens[] = $value;
} else {
$this->tokens[$offset] = $value;
}
}
public function offsetUnset($offset) {
unset($this->tokens[$offset]);
}
}