AttributeIterator::offsetGet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Thruster\Component\XMLIterator;
4
5
use Iterator as IteratorInterface;
6
use Countable;
7
use ArrayAccess;
8
use BadMethodCallException;
9
10
/**
11
 * Class AttributeIterator
12
 *
13
 * @package Thruster\Component\XMLIterator
14
 * @author  Aurimas Niekis <[email protected]>
15
 */
16
class AttributeIterator implements IteratorInterface, Countable, ArrayAccess
17
{
18
    /**
19
     * @var XMLReader
20
     */
21
    private $reader;
22
23
    /**
24
     * @var bool
25
     */
26
    private $valid;
27
28
    /**
29
     * @var array
30
     */
31
    private $array;
32
33 18
    public function __construct(XMLReader $reader)
34
    {
35 18
        $this->reader = $reader;
36 18
    }
37
38
    public function count()
39
    {
40
        return $this->reader->attributeCount;
41
    }
42
43 9
    public function current()
44
    {
45 9
        return $this->reader->value;
46
    }
47
48 9
    public function key()
49
    {
50 9
        return $this->reader->name;
51
    }
52
53 9
    public function next()
54
    {
55 9
        $this->valid = $this->reader->moveToNextAttribute();
56
57 9
        if (!$this->valid) {
58 9
            $this->reader->moveToElement();
59
        }
60 9
    }
61
62 18
    public function rewind()
63
    {
64 18
        $this->valid = $this->reader->moveToFirstAttribute();
65 18
    }
66
67 18
    public function valid()
68
    {
69 18
        return $this->valid;
70
    }
71
72 18
    public function getArrayCopy()
73
    {
74 18
        if ($this->array === null) {
75 18
            $this->array = iterator_to_array($this);
76
        }
77
78 18
        return $this->array;
79
    }
80
81
    public function getAttributeNames()
82
    {
83
        return array_keys($this->getArrayCopy());
84
    }
85
86
    public function offsetExists($offset)
87
    {
88
        $attributes = $this->getArrayCopy();
89
90
        return isset($attributes[$offset]);
91
    }
92
93
    public function offsetGet($offset)
94
    {
95
        $attributes = $this->getArrayCopy();
96
97
        return $attributes[$offset];
98
    }
99
100
    public function offsetSet($offset, $value)
101
    {
102
        throw new BadMethodCallException('XMLReader attributes are read-only');
103
    }
104
105
    public function offsetUnset($offset)
106
    {
107
        throw new BadMethodCallException('XMLReader attributes are read-only');
108
    }
109
110
    /**
111
     * @return XMLReader
112
     */
113
    public function getReader()
114
    {
115
        return $this->getReader();
116
    }
117
}
118