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

Definition::clearValidators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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