OperatorPool::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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 = [
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
43
        OperatorPool::TYPE_COMPARATOR => [
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
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 => [
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
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[]]
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[\LogicTree\Operator\OperatorInterface[]] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
70
     */
71
    private $operators = [];
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
72
73
    /**
74
     * OperatorPool constructor
75
     *
76
     * @param array $operators Operators object listed by types
77
     */
78
    public function __construct(array $operators = [])
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
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] = [];
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
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