Passed
Push — master ( f81576...cae37d )
by Marcin
02:27 queued 10s
created

FilterParam   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
eloc 26
dl 0
loc 123
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A isWhere() 0 9 1
A isWhereLike() 0 3 1
A __construct() 0 9 2
A isWhereIn() 0 3 1
A isWhereNotIn() 0 3 1
A isWhereLikeLeft() 0 3 1
A isWhereNot() 0 3 1
A isWhereNotNull() 0 3 1
A isWhereNull() 0 3 1
A isWhereLikeRight() 0 3 1
A isWhereRegexp() 0 3 1
1
<?php
2
/**
3
 * Created by Marcin.
4
 * Date: 16.06.2018
5
 * Time: 19:17
6
 */
7
declare(strict_types=1);
8
9
namespace Mrcnpdlk\Lib\UrlSearchParser\Criteria;
10
11
use function in_array;
12
use Mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException;
13
14
/**
15
 * Class FilterParam
16
 */
17
class FilterParam
18
{
19
    /**
20
     * @var string
21
     */
22
    public $param;
23
    /**
24
     * @var string
25
     */
26
    public $operator;
27
    /**
28
     * @var string|null
29
     */
30
    public $sqlOperator;
31
    /**
32
     * @var mixed
33
     */
34
    public $value;
35
36
    /**
37
     * FilterParam constructor.
38
     *
39
     * @param string $param
40
     * @param string $operator
41
     * @param mixed  $value
42
     *
43
     * @throws \Mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException
44
     */
45 5
    public function __construct(string $param, string $operator, $value)
46
    {
47 5
        $this->param    = $param;
48 5
        $this->operator = strtolower(trim($operator));
49 5
        if (!array_key_exists($this->operator, Filter::$allowedOperators)) {
50 1
            throw new InvalidParamException(sprintf('Invalid operator "%s". Only one of "%s" is allowed', $this->operator, implode(',', array_keys(Filter::$allowedOperators))));
51
        }
52 4
        $this->sqlOperator = Filter::$allowedOperators[$operator];
53 4
        $this->value       = $value;
54 4
    }
55
56
    /**
57
     * @return bool
58
     */
59 1
    public function isWhere(): bool
60
    {
61 1
        return in_array($this->operator, [
62
            Filter::PARAM_EQ,
63
            Filter::PARAM_GT,
64
            Filter::PARAM_GTE,
65
            Filter::PARAM_LT,
66
            Filter::PARAM_LTE,
67
        ], true);
68
    }
69
70
    /**
71
     * @return bool
72
     */
73 1
    public function isWhereIn(): bool
74
    {
75 1
        return Filter::PARAM_IN === $this->operator;
76
    }
77
78
    /**
79
     * @return bool
80
     */
81 1
    public function isWhereLike(): bool
82
    {
83 1
        return Filter::PARAM_LIKE === $this->operator;
84
    }
85
86
    /**
87
     * @return bool
88
     */
89 1
    public function isWhereLikeLeft(): bool
90
    {
91 1
        return Filter::PARAM_LIKE_LEFT === $this->operator;
92
    }
93
94
    /**
95
     * @return bool
96
     */
97 1
    public function isWhereLikeRight(): bool
98
    {
99 1
        return Filter::PARAM_LIKE_RIGHT === $this->operator;
100
    }
101
102
    /**
103
     * @return bool
104
     */
105 1
    public function isWhereNotIn(): bool
106
    {
107 1
        return Filter::PARAM_NOTIN === $this->operator;
108
    }
109
110
    /**
111
     * @return bool
112
     */
113 2
    public function isWhereNotNull(): bool
114
    {
115 2
        return Filter::PARAM_NOTNULL === $this->operator;
116
    }
117
118
    /**
119
     * @return bool
120
     */
121 2
    public function isWhereNull(): bool
122
    {
123 2
        return Filter::PARAM_NULL === $this->operator;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129 1
    public function isWhereNot(): bool
130
    {
131 1
        return Filter::PARAM_NOT === $this->operator;
132
    }
133
134
    /**
135
     * @return bool
136
     */
137 1
    public function isWhereRegexp(): bool
138
    {
139 1
        return Filter::PARAM_REGEXP === $this->operator;
140
    }
141
}
142