Completed
Push — master ( c80620...0cd8d9 )
by Martijn
03:25
created

IterableParser::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Vanderlee\Comprehend\Parser\Structure;
4
5
use Vanderlee\Comprehend\Core\ArgumentsTrait;
6
use Vanderlee\Comprehend\Parser\Parser;
7
8
/**
9
 * Abstract class indicating a parser that consists of multiple parsers and can be accessed as an array.
10
 *
11
 * @author Martijn
12
 */
13
abstract class IterableParser extends Parser implements \IteratorAggregate, \ArrayAccess
14
{
15
    use ArgumentsTrait;
16
17
    /**
18
     * @var Parser[] $parsers
19
     */
20
    protected $parsers = [];
21
22
    // implements IteratorAggregate
23
24
    public function getIterator()
25
    {
26
        return new \ArrayIterator($this->parsers);
27
    }
28
29
    // implements ArrayAccess
30
31
    public function offsetSet($offset, $value)
32
    {
33
        $value = self::getArgument($value);
34
35
        if (is_null($offset)) {
36
            $this->parsers[] = $value;
37
        } else {
38
            $this->parsers[$offset] = $value;
39
        }
40
    }
41
42
    public function offsetExists($offset)
43
    {
44
        return isset($this->parsers[$offset]);
45
    }
46
47
    public function offsetUnset($offset)
48
    {
49
        unset($this->parsers[$offset]);
50
    }
51
52
    public function offsetGet($offset)
53
    {
54
        return isset($this->parsers[$offset]) ? $this->parsers[$offset] : null;
55
    }
56
}
57