Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types = 1);
2
namespace TheSeer\Tokenizer;
3
 
4
class TokenCollection implements \ArrayAccess, \Iterator, \Countable {
5
 
6
    /** @var Token[] */
7
    private $tokens = [];
8
 
9
    /** @var int */
10
    private $pos;
11
 
12
    public function addToken(Token $token): void {
13
        $this->tokens[] = $token;
14
    }
15
 
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;
36
    }
37
 
38
    public function count(): int {
39
        return \count($this->tokens);
40
    }
41
 
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
}