Completed
Push — master ( b928ab...26824d )
by Kevin
02:08
created

TokenCollection::slice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Kevintweber\HtmlTokenizer\Tokens;
4
5
/**
6
 * A TokenCollection is a group of tokens designed to act similiar to
7
 * an array.
8
 */
9
class TokenCollection implements \ArrayAccess, \IteratorAggregate
10
{
11
    /** @var array[Token] */
12
    private $tokens;
13
14
    /**
15
     * Constructor
16
     */
17 6
    public function __construct()
18
    {
19 6
        $this->tokens = array();
20 6
    }
21
22 3
    public function toArray()
23
    {
24 3
        $result = array();
25 3
        foreach ($this->tokens as $token) {
26 2
            $result[] = $token->toArray();
27 3
        }
28
29 3
        return $result;
30
    }
31
32
    public function reset()
33
    {
34
        return reset($this->tokens);
35
    }
36
37
    public function current()
38
    {
39
        return current($this->tokens);
40
    }
41
42
    public function end()
43
    {
44
        return end($this->tokens);
45
    }
46
47
    public function next()
48
    {
49
        return next($this->tokens);
50
    }
51
52
    public function prev()
53
    {
54
        return prev($this->tokens);
55
    }
56
57 1
    public function remove(Token $token)
58
    {
59 1
        $key = array_search($token, $this->tokens, true);
60 1
        if ($key === false) {
61 1
            return false;
62
        }
63
64 1
        unset($this->tokens[$key]);
65
66 1
        return true;
67
    }
68
69
    /**
70
     * Required by the ArrayAccess interface.
71
     */
72 2
    public function offsetExists($offset)
73
    {
74 2
        return array_key_exists($offset, $this->tokens);
75
    }
76
77
    /**
78
     * Required by the ArrayAccess interface.
79
     */
80 1
    public function offsetGet($offset)
81
    {
82 1
        return $this->tokens[$offset];
83
    }
84
85
    /**
86
     * Required by the ArrayAccess interface.
87
     */
88 5
    public function offsetSet($offset, $value)
89
    {
90 5
        if (!$value instanceof Token) {
91 1
            throw new \InvalidArgumentException('Value must be of type Token.');
92
        }
93
94 4
        $this->tokens[$offset] = $value;
95 4
    }
96
97
    /**
98
     * Required by the ArrayAccess interface.
99
     */
100 1
    public function offsetUnset($offset)
101
    {
102 1
        unset($this->tokens[$offset]);
103 1
    }
104
105 3
    public function count()
106
    {
107 3
        return count($this->tokens);
108
    }
109
110 2
    public function isEmpty()
111
    {
112 2
        return empty($this->tokens);
113
    }
114
115
    /**
116
     * Required by the IteratorAggregate interface.
117
     */
118
    public function getIterator()
119
    {
120
        return new \ArrayIterator($this->tokens);
121
    }
122
}
123