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