Definition::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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