Condition   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 72.92%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 0
loc 173
ccs 35
cts 48
cp 0.7292
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A equals() 0 4 1
A regex() 0 4 1
A notEquals() 0 4 1
A lessThan() 0 4 1
A lessThanOrEquals() 0 4 1
A greaterThan() 0 4 1
A greaterThanOrEquals() 0 4 1
A combineOr() 0 4 1
A combineAnd() 0 4 1
A combineManyAnd() 0 4 1
A combineManyOr() 0 4 1
A combineMany() 0 13 3
A getLeft() 0 4 1
A shouldEscapeLeft() 0 4 1
A getOperator() 0 4 1
A getRight() 0 4 1
A shouldEscapeRight() 0 4 1
1
<?php
2
namespace DAL;
3
4
final class Condition
5
{
6
7
    private $left;
8
    private $escapeLeft;
9
    private $operator;
10
    private $right;
11
    private $escapeRight;
12
13
    /**
14
     * @param mixed   $left
15
     * @param boolean $escapeLeft
16
     * @param string  $operator
17
     * @param mixed   $right
18
     * @param boolean $escapeRight
19
     */
20 26
    private function __construct($left, $escapeLeft, $operator, $right, $escapeRight)
21
    {
22 26
        $this->left        = $left;
23 26
        $this->escapeLeft  = $escapeLeft;
24 26
        $this->operator    = $operator;
25 26
        $this->right       = $right;
26 26
        $this->escapeRight = $escapeRight;
27 26
    }
28
29
    /**
30
     * @param string $field
31
     */
32 18
    public static function equals($field, $value)
33
    {
34 18
        return new Condition($field, false, '=', $value, true);
35
    }
36
37
    /**
38
     * @param string $field
39
     */
40 1
    public static function regex($field, $value)
41
    {
42 1
        return new Condition($field, false, 'regex', $value, true);
43
    }
44
45
    /**
46
     * @param string $field
47
     */
48 1
    public static function notEquals($field, $value)
49
    {
50 1
        return new Condition($field, false, '!=', $value, true);
51
    }
52
53
    /**
54
     * @param string $field
55
     */
56 3
    public static function lessThan($field, $value)
57
    {
58 3
        return new Condition($field, false, '<', $value, true);
59
    }
60
61
    /**
62
     * @param string $field
63
     */
64 1
    public static function lessThanOrEquals($field, $value)
65
    {
66 1
        return new Condition($field, false, '<=', $value, true);
67
    }
68
69
    /**
70
     * @param string $field
71
     */
72 1
    public static function greaterThan($field, $value)
73
    {
74 1
        return new Condition($field, false, '>', $value, true);
75
    }
76
77
    /**
78
     * @param string $field
79
     */
80 3
    public static function greaterThanOrEquals($field, $value)
81
    {
82 3
        return new Condition($field, false, '>=', $value, true);
83
    }
84
85
    /**
86
     * @param Condition $left
87
     * @param Condition $right
88
     */
89 1
    public static function combineOr(Condition $left, Condition $right)
90
    {
91 1
        return new Condition($left, false, 'or', $right, false);
92
    }
93
94
    /**
95
     * @param Condition $left
96
     * @param Condition $right
97
     */
98 1
    public static function combineAnd(Condition $left, Condition $right)
99
    {
100 1
        return new Condition($left, false, 'and', $right, false);
101
    }
102
103
    /**
104
     * @param Condition[] $values
105
     */
106
    public static function combineManyAnd(array $values)
107
    {
108
        return self::combineMany('and', $values);
109
    }
110
111
    /**
112
     * @param Condition[] $values
113
     */
114
    public static function combineManyOr(array $values)
115
    {
116
        return self::combineMany('or', $values);
117
    }
118
119
    /**
120
     * @param string      $operation
121
     * @param Condition[] $values
122
     */
123
    public static function combineMany($operation, array $values)
124
    {
125
        $valuesCount = count($values);
126
        if ($valuesCount > 1) {
127
            $left  = array_pop($values);
128
            $right = self::combineMany($operation, $values);
129
            return new Condition($left, false, $operation, $right, false);
130
        }
131
        if (1 === $valuesCount) {
132
            return $values[0];
133
        }
134
        return null;
135
    }
136
137
    /**
138
     * @return mixed
139
     */
140 26
    public function getLeft()
141
    {
142 26
        return $this->left;
143
    }
144
145
    /**
146
     * @return boolean
147
     */
148 26
    public function shouldEscapeLeft()
149
    {
150 26
        return $this->escapeLeft;
151
    }
152
153
    /**
154
     * @return string
155
     */
156 26
    public function getOperator()
157
    {
158 26
        return $this->operator;
159
    }
160
161
    /**
162
     * @return mixed
163
     */
164 26
    public function getRight()
165
    {
166 26
        return $this->right;
167
    }
168
169
    /**
170
     * @return boolean
171
     */
172 26
    public function shouldEscapeRight()
173
    {
174 26
        return $this->escapeRight;
175
    }
176
}
177