@@ -41,86 +41,73 @@ final class OperatorPool
41
41
public const TYPE_LOGICAL = 'logical ' ;
42
42
public const TYPE_COMPARATOR = 'comparator ' ;
43
43
44
- /**
45
- * Default operators code and class name listed by types
46
- *
47
- * @var array
48
- */
49
- private const DEFAULT_OPERATORS = [
50
- OperatorPool::TYPE_COMPARATOR => [
51
- EmptyOperator::CODE => EmptyOperator::class,
52
- EqOperator::CODE => EqOperator::class,
53
- GteqOperator::CODE => GteqOperator::class,
54
- GtOperator::CODE => GtOperator::class,
55
- IdenOperator::CODE => IdenOperator::class,
56
- InIdenOperator::CODE => InIdenOperator::class,
57
- InOperator::CODE => InOperator::class,
58
- LteqOperator::CODE => LteqOperator::class,
59
- LtOperator::CODE => LtOperator::class,
60
- NeqOperator::CODE => NeqOperator::class,
61
- NidenOperator::CODE => NidenOperator::class,
62
- NinIdenOperator::CODE => NinIdenOperator::class,
63
- NinOperator::CODE => NinOperator::class,
64
- NotNullOperator::CODE => NotNullOperator::class,
65
- NullOperator::CODE => NullOperator::class,
66
- RegexpOperator::CODE => RegexpOperator::class,
67
- ],
68
- OperatorPool::TYPE_LOGICAL => [
69
- AndOperator::CODE => AndOperator::class,
70
- OrOperator::CODE => OrOperator::class,
71
- XorOperator::CODE => XorOperator::class,
72
- NandOperator::CODE => NandOperator::class,
73
- NorOperator::CODE => NorOperator::class,
74
- XnorOperator::CODE => XnorOperator::class,
75
- ],
76
- ];
77
-
78
44
/**
79
45
* @var array[\LogicTree\Operator\OperatorInterface[]]
80
46
*/
81
- private $ operators = [];
47
+ private array $ operators = [];
48
+
49
+ public static function defaultOperators (): array
50
+ {
51
+ return [
52
+ OperatorType::Comparator->value => [
53
+ EmptyOperator::CODE => EmptyOperator::class,
54
+ EqOperator::CODE => EqOperator::class,
55
+ GteqOperator::CODE => GteqOperator::class,
56
+ GtOperator::CODE => GtOperator::class,
57
+ IdenOperator::CODE => IdenOperator::class,
58
+ InIdenOperator::CODE => InIdenOperator::class,
59
+ InOperator::CODE => InOperator::class,
60
+ LteqOperator::CODE => LteqOperator::class,
61
+ LtOperator::CODE => LtOperator::class,
62
+ NeqOperator::CODE => NeqOperator::class,
63
+ NidenOperator::CODE => NidenOperator::class,
64
+ NinIdenOperator::CODE => NinIdenOperator::class,
65
+ NinOperator::CODE => NinOperator::class,
66
+ NotNullOperator::CODE => NotNullOperator::class,
67
+ NullOperator::CODE => NullOperator::class,
68
+ RegexpOperator::CODE => RegexpOperator::class,
69
+ ],
70
+ OperatorType::Logical->value => [
71
+ AndOperator::CODE => AndOperator::class,
72
+ OrOperator::CODE => OrOperator::class,
73
+ XorOperator::CODE => XorOperator::class,
74
+ NandOperator::CODE => NandOperator::class,
75
+ NorOperator::CODE => NorOperator::class,
76
+ XnorOperator::CODE => XnorOperator::class,
77
+ ],
78
+ ];
79
+ }
82
80
83
81
public function __construct (array $ operators = [])
84
82
{
85
83
$ typeOperators = array_merge_recursive ($ this ->retrieveDefaultOperators (), $ operators );
86
84
87
85
foreach ($ typeOperators as $ type => $ operatorList ) {
88
86
foreach ($ operatorList as $ operatorCode => $ operator ) {
89
- $ this ->addOperator ($ type , $ operatorCode , $ operator );
87
+ $ this ->addOperator (OperatorType:: from ( $ type) , $ operatorCode , $ operator );
90
88
}
91
89
}
92
90
}
93
91
94
- public function getOperator (string $ type , string $ operatorCode ): OperatorInterface
92
+ public function getOperator (OperatorType $ type , string $ operatorCode ): OperatorInterface
95
93
{
96
- if (!isset ($ this ->operators [$ type ][$ operatorCode ])) {
97
- throw new LogicException (
98
- sprintf ('No registered operator for the type "%s" and code "%s". ' , $ type , $ operatorCode )
99
- );
100
- }
101
-
102
- return $ this ->operators [$ type ][$ operatorCode ];
94
+ return $ this ->operators [$ type ->value ][$ operatorCode ] ?? throw new LogicException (
95
+ sprintf ('No registered operator for the type "%s" and code "%s". ' , $ type , $ operatorCode )
96
+ );
103
97
}
104
98
105
- public function addOperator (string $ type , string $ operatorCode , OperatorInterface $ operator ): OperatorPool
99
+ public function addOperator (OperatorType $ type , string $ operatorCode , OperatorInterface $ operator ): OperatorPool
106
100
{
107
- if (!isset ($ this ->operators [$ type ][$ operatorCode ])) {
108
- if (!isset ($ this ->operators [$ type ])) {
109
- $ this ->operators [$ type ] = [];
110
- }
111
-
112
- $ this ->operators [$ type ][$ operatorCode ] = $ operator ;
113
- }
101
+ $ this ->operators [$ type ->value ][$ operatorCode ] = $ operator ;
114
102
115
103
return $ this ;
116
104
}
117
105
118
106
private function retrieveDefaultOperators (): array
119
107
{
120
- return array_map (static function ($ operators ) {
121
- return array_map (static function ($ operator ) {
122
- return new $ operator ();
123
- }, $ operators );
124
- }, self ::DEFAULT_OPERATORS );
108
+ return array_map (
109
+ static fn ($ operators ) => array_map (static fn ($ operator ) => new $ operator (), $ operators ),
110
+ static ::defaultOperators ()
111
+ );
125
112
}
126
113
}
0 commit comments