OperatorManager   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 15
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 70
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isOperator() 0 10 2
B buildOperator() 0 27 7
A getOperator() 0 10 3
A getOperand() 0 10 3
1
<?php
2
/**
3
 * Ember Db - An embeddable document database for php.
4
 * Copyright (C) 2016 Alexander During
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * @link      http://github.com/alexanderduring/php-ember-db
20
 * @copyright Copyright (C) 2016 Alexander During
21
 * @license   http://www.gnu.org/licenses GNU General Public License v3.0
22
 */
23
24
namespace EmberDb\Filter;
25
26
use EmberDb\Filter\Operator\ElemMatch;
27
use EmberDb\Filter\Operator\GreaterThan;
28
use EmberDb\Filter\Operator\GreaterThanEqual;
29
use EmberDb\Filter\Operator\LowerThan;
30
use EmberDb\Filter\Operator\LowerThanEqual;
31
use EmberDb\Filter\Operator\NotEqual;
32
use EmberDb\Logger;
33
34
class OperatorManager
35
{
36
    public function isOperator($operatorArray)
37
    {
38
        $operator = $this->getOperator($operatorArray);
39
        $isOperator = in_array($operator, array('$gt', '$gte', '$lt', '$lte', '$ne', '$elemMatch'));
40
        if ($isOperator) {
41
            Logger::log('"'.$operator.'" is in '.json_encode(array('$gt', '$gte', '$lt', '$lte', '$ne', '$elemMatch')).', so it is an operator.\n');
42
        }
43
44
        return $isOperator;
45
    }
46
47
48
49
    public function buildOperator($operatorArray)
50
    {
51
        switch ($this->getOperator($operatorArray)) {
52
            case '$gt':
53
                $operator = new GreaterThan($this->getOperand($operatorArray));
54
                break;
55
            case '$gte':
56
                $operator = new GreaterThanEqual($this->getOperand($operatorArray));
57
                break;
58
            case '$lt':
59
                $operator = new LowerThan($this->getOperand($operatorArray));
60
                break;
61
            case '$lte':
62
                $operator = new LowerThanEqual($this->getOperand($operatorArray));
63
                break;
64
            case '$ne':
65
                $operator = new NotEqual($this->getOperand($operatorArray));
66
                break;
67
            case '$elemMatch':
68
                $operator = new ElemMatch($this->getOperand($operatorArray));
69
                break;
70
            default:
71
                $operator = null;
72
        }
73
74
        return $operator;
75
    }
76
77
78
79
    private function getOperator($operatorArray)
80
    {
81
        if (is_array($operatorArray) && count($operatorArray) == 1) {
82
            $operator = array_keys($operatorArray)[0];
83
        } else {
84
            $operator = null;
85
        }
86
87
        return $operator;
88
    }
89
90
91
92
    private function getOperand($operatorArray)
93
    {
94
        if (is_array($operatorArray) && count($operatorArray) == 1) {
95
            $operand = $operatorArray[$this->getOperator($operatorArray)];
96
        } else {
97
            $operand = null;
98
        }
99
100
        return $operand;
101
    }
102
103
}
104