IterableParser::getIterator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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