1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © 2018 Thomas Klein, All rights reserved. |
4
|
|
|
* See LICENSE bundled with this library for license details. |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace LogicTree\Operator; |
9
|
|
|
|
10
|
|
|
use LogicTree\Operator\Comparator\EmptyOperator; |
11
|
|
|
use LogicTree\Operator\Comparator\EqOperator; |
12
|
|
|
use LogicTree\Operator\Comparator\GteqOperator; |
13
|
|
|
use LogicTree\Operator\Comparator\GtOperator; |
14
|
|
|
use LogicTree\Operator\Comparator\IdenOperator; |
15
|
|
|
use LogicTree\Operator\Comparator\LteqOperator; |
16
|
|
|
use LogicTree\Operator\Comparator\LtOperator; |
17
|
|
|
use LogicTree\Operator\Comparator\NeqOperator; |
18
|
|
|
use LogicTree\Operator\Comparator\NidenOperator; |
19
|
|
|
use LogicTree\Operator\Comparator\NullOperator; |
20
|
|
|
use LogicTree\Operator\Comparator\RegexpOperator; |
21
|
|
|
use LogicTree\Operator\Logical\AndOperator; |
22
|
|
|
use LogicTree\Operator\Logical\NandOperator; |
23
|
|
|
use LogicTree\Operator\Logical\NorOperator; |
24
|
|
|
use LogicTree\Operator\Logical\OrOperator; |
25
|
|
|
use LogicTree\Operator\Logical\XnorOperator; |
26
|
|
|
use LogicTree\Operator\Logical\XorOperator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class OperatorPool |
30
|
|
|
* @api |
31
|
|
|
*/ |
32
|
|
|
final class OperatorPool |
33
|
|
|
{ |
34
|
|
|
public const TYPE_LOGICAL = 'logical'; |
35
|
|
|
public const TYPE_COMPARATOR = 'comparator'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Default operators code and class name listed by types |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private const DEFAULT_OPERATORS = [ |
|
|
|
|
43
|
|
|
OperatorPool::TYPE_COMPARATOR => [ |
|
|
|
|
44
|
|
|
EmptyOperator::CODE => EmptyOperator::class, |
45
|
|
|
EqOperator::CODE => EqOperator::class, |
46
|
|
|
GteqOperator::CODE => GteqOperator::class, |
47
|
|
|
GtOperator::CODE => GtOperator::class, |
48
|
|
|
IdenOperator::CODE => IdenOperator::class, |
49
|
|
|
LteqOperator::CODE => LteqOperator::class, |
50
|
|
|
LtOperator::CODE => LtOperator::class, |
51
|
|
|
NeqOperator::CODE => NeqOperator::class, |
52
|
|
|
NidenOperator::CODE => NidenOperator::class, |
53
|
|
|
NullOperator::CODE => NullOperator::class, |
54
|
|
|
RegexpOperator::CODE => RegexpOperator::class, |
55
|
|
|
], |
56
|
|
|
OperatorPool::TYPE_LOGICAL => [ |
|
|
|
|
57
|
|
|
AndOperator::CODE => AndOperator::class, |
58
|
|
|
OrOperator::CODE => OrOperator::class, |
59
|
|
|
XorOperator::CODE => XorOperator::class, |
60
|
|
|
NandOperator::CODE => NandOperator::class, |
61
|
|
|
NorOperator::CODE => NorOperator::class, |
62
|
|
|
XnorOperator::CODE => XnorOperator::class, |
63
|
|
|
], |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Operators list |
68
|
|
|
* |
69
|
|
|
* @var array[\LogicTree\Operator\OperatorInterface[]] |
|
|
|
|
70
|
|
|
*/ |
71
|
|
|
private $operators = []; |
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* OperatorPool constructor |
75
|
|
|
* |
76
|
|
|
* @param array $operators Operators object listed by types |
77
|
|
|
*/ |
78
|
|
|
public function __construct(array $operators = []) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$typeOperators = \array_replace_recursive($this->retrieveDefaultOperators(), $operators); |
81
|
|
|
|
82
|
|
|
foreach ($typeOperators as $type => $operatorList) { |
83
|
|
|
foreach ($operatorList as $operatorCode => $operator) { |
84
|
|
|
$this->addOperator($type, $operatorCode, $operator); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieve an operator by its type and code |
91
|
|
|
* |
92
|
|
|
* @param string $type |
93
|
|
|
* @param string $operatorCode |
94
|
|
|
* @return \LogicTree\Operator\OperatorInterface |
95
|
|
|
*/ |
96
|
|
|
public function getOperator(string $type, string $operatorCode): OperatorInterface |
97
|
|
|
{ |
98
|
|
|
if (!isset($this->operators[$type][$operatorCode])) { |
99
|
|
|
throw new \LogicException( |
100
|
|
|
\sprintf('No registered operator for the type "%s" and code "%s".', $type, $operatorCode) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->operators[$type][$operatorCode]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Add an operator by its type and code |
109
|
|
|
* |
110
|
|
|
* @param string $type |
111
|
|
|
* @param string $operatorCode |
112
|
|
|
* @param \LogicTree\Operator\OperatorInterface $operator |
113
|
|
|
* @return \LogicTree\Operator\OperatorPool |
114
|
|
|
*/ |
115
|
|
|
public function addOperator(string $type, string $operatorCode, OperatorInterface $operator): OperatorPool |
116
|
|
|
{ |
117
|
|
|
if (!isset($this->operators[$type][$operatorCode])) { |
118
|
|
|
if (!isset($this->operators[$type])) { |
119
|
|
|
$this->operators[$type] = []; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->operators[$type][$operatorCode] = $operator; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Retrieve the default instantiated operators |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
private function retrieveDefaultOperators(): array |
134
|
|
|
{ |
135
|
|
|
return \array_map(function ($operators) { |
136
|
|
|
return \array_map(function ($operator) { |
137
|
|
|
return new $operator(); |
138
|
|
|
}, $operators); |
139
|
|
|
}, self::DEFAULT_OPERATORS); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|