1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vanderlee\Comprehend\Builder; |
4
|
|
|
|
5
|
|
|
use Vanderlee\Comprehend\Core\ArgumentsTrait; |
6
|
|
|
use Vanderlee\Comprehend\Parser\Parser; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Shorthand for parser definitions |
10
|
|
|
* |
11
|
|
|
* @author Martijn |
12
|
|
|
*/ |
13
|
|
|
class Definition |
14
|
|
|
{ |
15
|
|
|
use ArgumentsTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var callable|Parser |
19
|
|
|
*/ |
20
|
|
|
public $generator; |
21
|
|
|
public $validators = []; |
22
|
|
|
public $processors = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Parser|callable $generator Either a parser or a function returning a parser ('generator') |
26
|
|
|
* @param callable[] $validator |
27
|
|
|
*/ |
28
|
37 |
|
public function __construct($generator = null, $validator = null) |
29
|
|
|
{ |
30
|
|
|
//@todo validate parser and validator |
31
|
|
|
|
32
|
37 |
|
$this->generator = $generator; |
33
|
37 |
|
if (is_callable($validator)) { |
34
|
1 |
|
$this->validators[] = $validator; |
35
|
|
|
} |
36
|
37 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* (Re-)define this definition from any supported source |
40
|
|
|
* |
41
|
|
|
* @param mixed $definition |
42
|
|
|
* |
43
|
|
|
* @throws InvalidArgumentException |
44
|
|
|
*/ |
45
|
16 |
|
public function define($definition) { |
46
|
16 |
|
if ($definition instanceof Definition) { |
47
|
1 |
|
$this->generator = $definition->generator; |
48
|
1 |
|
$this->validators = $definition->validators; |
49
|
1 |
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
15 |
|
if ($definition instanceof Parser |
53
|
15 |
|
|| is_callable($definition)) { |
54
|
12 |
|
$this->generator = $definition; |
55
|
12 |
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
11 |
|
$this->generator = self::getArgument($definition); |
59
|
9 |
|
} |
60
|
|
|
|
61
|
1 |
|
public function setGenerator($parser) |
62
|
|
|
{ |
63
|
1 |
|
$this->generator = $parser; |
64
|
|
|
|
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function clearValidators() |
69
|
|
|
{ |
70
|
1 |
|
$this->validators = []; |
71
|
|
|
|
72
|
1 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
public function addValidator($validator) |
76
|
|
|
{ |
77
|
3 |
|
$this->validators[] = $validator; |
78
|
|
|
|
79
|
3 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function addProcessor($key, $processor) |
83
|
|
|
{ |
84
|
1 |
|
$this->processors[$key] = $processor; |
85
|
|
|
|
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Build an instance of this parser definition. |
91
|
|
|
* |
92
|
|
|
* @param Mixed[] $arguments |
93
|
|
|
* |
94
|
|
|
* @return Implementation |
95
|
|
|
*/ |
96
|
14 |
|
public function build(...$arguments) |
97
|
|
|
{ |
98
|
14 |
|
return new Implementation($this, $arguments); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Build an instance of this parser definition. |
103
|
|
|
* Alias of `build()` method. |
104
|
|
|
* |
105
|
|
|
* @param Mixed[] $arguments |
106
|
|
|
* |
107
|
|
|
* @return Implementation |
108
|
|
|
*/ |
109
|
9 |
|
public function __invoke(...$arguments) |
110
|
|
|
{ |
111
|
9 |
|
return $this->build(...$arguments); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|