Passed
Push — master ( 305c34...2bac56 )
by y
01:32
created

ComparisonTrait::getChoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Helix\DB\SQL;
4
5
use Helix\DB;
6
use Helix\DB\Select;
7
8
/**
9
 * Produces comparative expressions for the instance.
10
 */
11
trait ComparisonTrait {
12
13
    use PredicateTrait;
14
15
    /**
16
     * @var DB
17
     */
18
    protected $db;
19
20
    /**
21
     * `COALESCE($this, ...$values)`
22
     *
23
     * @param array $values
24
     * @return Value
25
     */
26
    public function coalesce (array $values) {
27
        array_unshift($values, $this);
28
        $values = implode(',', $this->db->quoteArray($values));
29
        return new Value($this->db, "COALESCE({$values})");
30
    }
31
32
    /**
33
     * `CASE $this ... END`
34
     *
35
     * @param string[] $values `[when => then]`
36
     * @return Choice
37
     */
38
    public function getChoice (array $values) {
39
        return new Choice($this->db, $this, $values);
0 ignored issues
show
Bug introduced by
$this of type Helix\DB\SQL\ComparisonTrait is incompatible with the type Helix\DB\SQL\ValueInterface|string expected by parameter $subject of Helix\DB\SQL\Choice::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        return new Choice($this->db, /** @scrutinizer ignore-type */ $this, $values);
Loading history...
40
    }
41
42
    /**
43
     * Null-safe equality.
44
     *
45
     * - Mysql: `$this <=> $arg`, or `$this <=> ANY ($arg)`
46
     * - Sqlite: `$this IS $arg`, or `$this IS ANY ($arg)`
47
     *
48
     * @param null|bool|string|Select $arg
49
     * @return Predicate
50
     */
51
    public function is ($arg): Predicate {
52
        if ($arg === null or is_bool($arg)) {
53
            $arg = ['' => 'NULL', 1 => 'TRUE', 0 => 'FALSE'][$arg];
54
        }
55
        else {
56
            $arg = $this->db->quote($arg);
57
        }
58
        $oper = ['mysql' => '<=>', 'sqlite' => 'IS'][$this->db->getDriver()];
59
        return Predicate::compare($this, $arg, $oper, 'ANY');
60
    }
61
62
    /**
63
     * `$this BETWEEN $min AND $max` (inclusive)
64
     *
65
     * @param number|string $min
66
     * @param number|string $max
67
     * @return Predicate
68
     */
69
    public function isBetween ($min, $max) {
70
        return new Predicate("{$this} BETWEEN {$this->db->quote($min)} AND {$this->db->quote($max)}");
71
    }
72
73
    /**
74
     * `$this = $arg`, or `$this IN (...$arg)`, or `$this = ANY ($arg)`
75
     *
76
     * @param string|array|Select $arg
77
     * @return Predicate
78
     */
79
    public function isEqual ($arg) {
80
        return Predicate::compare($this, $this->db->quoteMixed($arg));
81
    }
82
83
    /**
84
     * `$this > $arg`, or `$this > ALL ($arg)`
85
     *
86
     * @param string|Select $arg
87
     * @return Predicate
88
     */
89
    public function isGreater ($arg) {
90
        return Predicate::compare($this, $this->db->quote($arg), '>', 'ALL');
91
    }
92
93
    /**
94
     * `$this >= $arg`, or `$this >= ALL ($arg)`
95
     *
96
     * @param string|Select $arg
97
     * @return Predicate
98
     */
99
    public function isGreaterOrEqual ($arg) {
100
        return Predicate::compare($this, $this->db->quote($arg), '>=', 'ALL');
101
    }
102
103
    /**
104
     * `$this < $arg`, or `$this < ALL ($arg)`
105
     *
106
     * @param string|Select $arg
107
     * @return Predicate
108
     */
109
    public function isLess ($arg) {
110
        return Predicate::compare($this, $this->db->quote($arg), '<', 'ALL');
111
    }
112
113
    /**
114
     * `$this <= $arg`, or `$this <= ALL ($arg)`
115
     *
116
     * @param string|Select $arg
117
     * @return Predicate
118
     */
119
    public function isLessOrEqual ($arg) {
120
        return Predicate::compare($this, $this->db->quote($arg), '<=', 'ALL');
121
    }
122
123
    /**
124
     * `$this LIKE $pattern`
125
     *
126
     * @param string $pattern
127
     * @return Predicate
128
     */
129
    public function isLike (string $pattern) {
130
        return Predicate::compare($this, $this->db->quote($pattern), 'LIKE');
131
    }
132
133
    /**
134
     * Null-safe inequality.
135
     *
136
     * @param null|bool|string|Select $arg
137
     * @return Predicate
138
     */
139
    public function isNot ($arg) {
140
        return $this->is($arg)->invert();
141
    }
142
143
    /**
144
     * `$this NOT BETWEEN $min AND $max` (inclusive)
145
     *
146
     * @param number|string $min
147
     * @param number|string $max
148
     * @return Predicate
149
     */
150
    public function isNotBetween ($min, $max) {
151
        return new Predicate("{$this} NOT BETWEEN {$this->db->quote($min)} AND {$this->db->quote($max)}");
152
    }
153
154
    /**
155
     * `$this <> $arg`, or `$this NOT IN (...$arg)`, or `$this <> ALL ($arg)`
156
     *
157
     * @param string|array|Select $arg
158
     * @return Predicate
159
     */
160
    public function isNotEqual ($arg) {
161
        return Predicate::compare($this, $this->db->quoteMixed($arg), '<>', 'ALL', 'NOT IN');
162
    }
163
164
    /**
165
     * `$this NOT LIKE $pattern`
166
     *
167
     * @param string $pattern
168
     * @return Predicate
169
     */
170
    public function isNotLike (string $pattern) {
171
        return Predicate::compare($this, $this->db->quote($pattern), 'NOT LIKE');
172
    }
173
174
    /**
175
     * `$this NOT REGEXP $pattern`
176
     *
177
     * @param string $pattern
178
     * @return Predicate
179
     */
180
    public function isNotRegExp (string $pattern) {
181
        return Predicate::compare($this, $this->db->quote($pattern), 'NOT REGEXP');
182
    }
183
184
    /**
185
     * `$this REGEXP $pattern`
186
     *
187
     * @param string $pattern
188
     * @return Predicate
189
     */
190
    public function isRegExp (string $pattern) {
191
        return Predicate::compare($this, $this->db->quote($pattern), 'REGEXP');
192
    }
193
}