Passed
Push — master ( 4344d4...81d6c9 )
by Martijn
02:43
created

Definition::copy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
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
     * @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