1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vanderlee\Comprehend\Builder; |
4
|
|
|
|
5
|
|
|
use Vanderlee\Comprehend\Parser\Parser; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Shorthand for parser definitions |
9
|
|
|
* |
10
|
|
|
* @author Martijn |
11
|
|
|
*/ |
12
|
|
|
class Definition |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var callable|Parser |
16
|
|
|
*/ |
17
|
|
|
public $generator; |
18
|
|
|
public $validators = []; |
19
|
|
|
public $processors = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Parser|callable $generator Either a parser or a function returning a parser ('generator') |
23
|
|
|
* @param callable[] $validator |
24
|
|
|
*/ |
25
|
37 |
|
public function __construct($generator = null, $validator = null) |
26
|
|
|
{ |
27
|
|
|
//@todo validate parser and validator |
28
|
|
|
|
29
|
37 |
|
$this->generator = $generator; |
30
|
37 |
|
if (is_callable($validator)) { |
31
|
1 |
|
$this->validators[] = $validator; |
32
|
|
|
} |
33
|
37 |
|
} |
34
|
|
|
|
35
|
1 |
|
public function setGenerator($parser) |
36
|
|
|
{ |
37
|
1 |
|
$this->generator = $parser; |
38
|
|
|
|
39
|
1 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function clearValidators() |
43
|
|
|
{ |
44
|
1 |
|
$this->validators = []; |
45
|
|
|
|
46
|
1 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
public function addValidator($validator) |
50
|
|
|
{ |
51
|
3 |
|
$this->validators[] = $validator; |
52
|
|
|
|
53
|
3 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function addProcessor($key, $processor) |
57
|
|
|
{ |
58
|
1 |
|
$this->processors[$key] = $processor; |
59
|
|
|
|
60
|
1 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Build an instance of this parser definition. |
65
|
|
|
* |
66
|
|
|
* @param Mixed[] $arguments |
67
|
|
|
* @return Implementation |
68
|
|
|
*/ |
69
|
14 |
|
public function build(...$arguments) |
70
|
|
|
{ |
71
|
14 |
|
return new Implementation($this, $arguments); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Build an instance of this parser definition. |
76
|
|
|
* Alias of `build()` method. |
77
|
|
|
* |
78
|
|
|
* @param Mixed[] $arguments |
79
|
|
|
* @return Implementation |
80
|
|
|
*/ |
81
|
9 |
|
public function __invoke(...$arguments) |
82
|
|
|
{ |
83
|
9 |
|
return $this->build(...$arguments); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|