|
1 | 1 | <?php declare(strict_types = 1); |
2 | 2 | namespace TheSeer\Tokenizer; |
3 | 3 |
|
4 | | -class TokenCollection implements \ArrayAccess, \Iterator, \Countable { |
| 4 | +use ArrayIterator; |
| 5 | +use Countable; |
| 6 | +use Iterator; |
| 7 | +use IteratorAggregate; |
| 8 | + |
| 9 | +/** |
| 10 | + * @implements IteratorAggregate<int, Token> |
| 11 | + */ |
| 12 | +class TokenCollection implements IteratorAggregate, Countable { |
5 | 13 |
|
6 | 14 | /** @var Token[] */ |
7 | 15 | private $tokens = []; |
8 | 16 |
|
9 | | - /** @var int */ |
10 | | - private $pos; |
11 | | - |
12 | 17 | public function addToken(Token $token): void { |
13 | 18 | $this->tokens[] = $token; |
14 | 19 | } |
15 | 20 |
|
16 | | - public function current(): Token { |
17 | | - return \current($this->tokens); |
18 | | - } |
19 | | - |
20 | | - public function key(): int { |
21 | | - return \key($this->tokens); |
22 | | - } |
23 | | - |
24 | | - public function next(): void { |
25 | | - \next($this->tokens); |
26 | | - $this->pos++; |
27 | | - } |
28 | | - |
29 | | - public function valid(): bool { |
30 | | - return $this->count() > $this->pos; |
31 | | - } |
32 | | - |
33 | | - public function rewind(): void { |
34 | | - \reset($this->tokens); |
35 | | - $this->pos = 0; |
| 21 | + public function getIterator(): Iterator |
| 22 | + { |
| 23 | + return new ArrayIterator($this->tokens); |
36 | 24 | } |
37 | 25 |
|
38 | 26 | public function count(): int { |
39 | 27 | return \count($this->tokens); |
40 | 28 | } |
41 | 29 |
|
42 | | - public function offsetExists($offset): bool { |
43 | | - return isset($this->tokens[$offset]); |
44 | | - } |
45 | | - |
46 | | - /** |
47 | | - * @throws TokenCollectionException |
48 | | - */ |
49 | | - public function offsetGet($offset): Token { |
50 | | - if (!$this->offsetExists($offset)) { |
51 | | - throw new TokenCollectionException( |
52 | | - \sprintf('No Token at offest %s', $offset) |
53 | | - ); |
54 | | - } |
55 | | - |
56 | | - return $this->tokens[$offset]; |
57 | | - } |
58 | | - |
59 | | - /** |
60 | | - * @param Token $value |
61 | | - * |
62 | | - * @throws TokenCollectionException |
63 | | - */ |
64 | | - public function offsetSet($offset, $value): void { |
65 | | - if (!\is_int($offset)) { |
66 | | - $type = \gettype($offset); |
67 | | - |
68 | | - throw new TokenCollectionException( |
69 | | - \sprintf( |
70 | | - 'Offset must be of type integer, %s given', |
71 | | - $type === 'object' ? \get_class($value) : $type |
72 | | - ) |
73 | | - ); |
74 | | - } |
75 | | - |
76 | | - if (!$value instanceof Token) { |
77 | | - $type = \gettype($value); |
78 | | - |
79 | | - throw new TokenCollectionException( |
80 | | - \sprintf( |
81 | | - 'Value must be of type %s, %s given', |
82 | | - Token::class, |
83 | | - $type === 'object' ? \get_class($value) : $type |
84 | | - ) |
85 | | - ); |
86 | | - } |
87 | | - $this->tokens[$offset] = $value; |
88 | | - } |
89 | | - |
90 | | - public function offsetUnset($offset): void { |
91 | | - unset($this->tokens[$offset]); |
92 | | - } |
93 | 30 | } |
0 commit comments