Definitions   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 150
c 0
b 0
f 0
wmc 16
lcom 1
cbo 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A mergeWith() 0 7 1
A hasOperator() 0 4 1
A defineOperators() 0 6 2
A defineOperator() 0 5 1
A getOperator() 0 8 2
A getOperators() 0 4 1
A getInlineOperator() 0 8 2
A hasInlineOperator() 0 4 1
A defineInlineOperators() 0 6 2
A defineInlineOperator() 0 5 1
A getInlineOperators() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RulerZ\Target\Operators;
6
7
use RulerZ\Exception\OperatorNotFoundException;
8
9
class Definitions
10
{
11
    /**
12
     * List of operators.
13
     *
14
     * @var array
15
     */
16
    private $operators = [];
17
18
    /**
19
     * List of inline-able operators.
20
     *
21
     * @var array
22
     */
23
    private $inlineOperators = [];
24
25
    public function __construct(array $operators = [], array $inlineOperators = [])
26
    {
27
        $this->defineOperators($operators);
28
        $this->defineInlineOperators($inlineOperators);
29
    }
30
31
    public function mergeWith(self $other): self
32
    {
33
        return new static(
34
            array_merge($this->operators, $other->operators),
35
            array_merge($this->inlineOperators, $other->inlineOperators)
36
        );
37
    }
38
39
    /**
40
     * Tells if an operator exists.
41
     *
42
     * @param string $operator The operator's name.
43
     */
44
    public function hasOperator(string $operator): bool
45
    {
46
        return isset($this->operators[$operator]);
47
    }
48
49
    /**
50
     * Define operators.
51
     *
52
     * @param array<callable> $operators A list of operators to add, each one being a collable.
53
     */
54
    public function defineOperators(array $operators): void
55
    {
56
        foreach ($operators as $name => $callable) {
57
            $this->defineOperator($name, $callable);
58
        }
59
    }
60
61
    /**
62
     * Define an operator.
63
     *
64
     * @param string   $operator    The operator's name.
65
     * @param callable $transformer Callable.
66
     */
67
    public function defineOperator(string $operator, callable $transformer): void
68
    {
69
        unset($this->inlineOperators[$operator]);
70
        $this->operators[$operator] = $transformer;
71
    }
72
73
    /**
74
     * Get an operator.
75
     *
76
     * @param string $operator The operator's name.
77
     *
78
     * @throws OperatorNotFoundException
79
     */
80
    protected function getOperator(string $operator): callable
81
    {
82
        if (!$this->hasOperator($operator)) {
83
            throw new OperatorNotFoundException($operator, sprintf('Operator "%s" does not exist.', $operator));
84
        }
85
86
        return $this->operators[$operator];
87
    }
88
89
    /**
90
     * Get the operators list.
91
     *
92
     * @return array<callable>
93
     */
94
    public function getOperators(): array
95
    {
96
        return $this->operators;
97
    }
98
99
    /**
100
     * Gets an inline-able operator.
101
     *
102
     * @param string $operator The operator's name.
103
     *
104
     * @throws OperatorNotFoundException
105
     */
106
    public function getInlineOperator(string $operator): callable
107
    {
108
        if (!$this->hasInlineOperator($operator)) {
109
            throw new OperatorNotFoundException($operator, sprintf('Inline operator "%s" does not exist.', $operator));
110
        }
111
112
        return $this->inlineOperators[$operator];
113
    }
114
115
    /**
116
     * Tells if an inline-able operator exists.
117
     *
118
     * @param string $operator The operator's name.
119
     */
120
    public function hasInlineOperator(string $operator): bool
121
    {
122
        return isset($this->inlineOperators[$operator]);
123
    }
124
125
    /**
126
     * Add inline operators.
127
     *
128
     * @param array<callable> $operators A list of inline operators to add.
129
     */
130
    public function defineInlineOperators(array $operators): void
131
    {
132
        foreach ($operators as $name => $callable) {
133
            $this->defineInlineOperator($name, $callable);
134
        }
135
    }
136
137
    /**
138
     * Set an inline-able operator.
139
     *
140
     * @param string   $operator    The operator's name.
141
     * @param callable $transformer Callable.
142
     */
143
    public function defineInlineOperator(string $operator, callable $transformer)
144
    {
145
        unset($this->operators[$operator]);
146
        $this->inlineOperators[$operator] = $transformer;
147
    }
148
149
    /**
150
     * Get the inline operators list.
151
     *
152
     * @return array<callable>
153
     */
154
    public function getInlineOperators(): array
155
    {
156
        return $this->inlineOperators;
157
    }
158
}
159