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
|
|
|
public $generator = null; |
16
|
|
|
public $validators = []; |
17
|
|
|
public $processors = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Parser|callable $generator Either a parser or a function returning a parser ('generator') |
21
|
|
|
* @param callable[] $validator |
22
|
|
|
*/ |
23
|
|
|
public function __construct($generator = null, $validator = null) |
24
|
|
|
{ |
25
|
|
|
//@todo validate parser and validator |
26
|
|
|
|
27
|
|
|
$this->generator = $generator; |
28
|
|
|
if (is_callable($validator)) { |
29
|
|
|
$this->validators[] = $validator; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setGenerator($parser) |
34
|
|
|
{ |
35
|
|
|
$this->generator = $parser; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function clearValidators() |
41
|
|
|
{ |
42
|
|
|
$this->validators = []; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function addValidator($validator) |
48
|
|
|
{ |
49
|
|
|
$this->validators[] = $validator; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function addProcessor($key, $processor) |
55
|
|
|
{ |
56
|
|
|
$this->processors[$key] = $processor; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Build an instance of this parser definition. |
63
|
|
|
* |
64
|
|
|
* @param Mixed[] $arguments |
65
|
|
|
* @return Implementation |
66
|
|
|
*/ |
67
|
|
|
public function build(...$arguments) |
68
|
|
|
{ |
69
|
|
|
return new Implementation($this, $arguments); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Build an instance of this parser definition. |
74
|
|
|
* Alias of `build()` method. |
75
|
|
|
* |
76
|
|
|
* @param Mixed[] $arguments |
77
|
|
|
* @return Implementation |
78
|
|
|
*/ |
79
|
|
|
public function __invoke(...$arguments) |
80
|
|
|
{ |
81
|
|
|
return $this->build(...$arguments); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|