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

Stub   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 7 2
A parse() 0 12 3
A __set() 0 7 2
A __toString() 0 4 2
1
<?php
2
3
namespace Vanderlee\Comprehend\Parser;
4
5
use Vanderlee\Comprehend\Core\ArgumentsTrait;
6
use Vanderlee\Comprehend\Core\Context;
7
8
/**
9
 * Description of StubParser
10
 *
11
 * @property Parser|null $parser
12
 *
13
 * @author Martijn
14
 */
15
class Stub extends Parser
16
{
17
18
    use ArgumentsTrait;
19
20
    /**
21
     * @var Parser|null
22
     */
23
    private $parser = null;
24
25
    public function __set($name, $parser)
26
    {
27
        if ($name == 'parser') {
28
            return $this->parser = self::getArgument($parser);
29
        }
30
31
        throw new \InvalidArgumentException("Property `{$name}` does not exist");
32
    }
33
34
    public function __get($name)
35
    {
36
        if ($name == 'parser') {
37
            return $this->parser;
38
        }
39
40
        throw new \InvalidArgumentException("Property `{$name}` does not exist");
41
    }
42
43
    protected function parse(&$input, $offset, Context $context)
44
    {
45
        if ($this->parser === null) {
46
            throw new \UnexpectedValueException('Missing parser');
47
        }
48
49
        $match = $this->parser->parse($input, $offset, $context);
50
        if ($match->match) {
51
            return $this->success($input, $offset, $match->length, $match);
52
        }
53
54
        return $this->failure($input, $offset, $match->length);
55
    }
56
57
    public function __toString()
58
    {
59
        /** @noinspection HtmlUnknownTag */
60
        return $this->parser ? (string)$this->parser : '<undefined>';
61
    }
62
63
}
64