Passed
Push — master ( d9fbee...ed974e )
by y
01:35
created

ComparisonTrait::isBetween()   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 2
1
<?php
2
3
namespace Helix\DB\SQL;
4
5
use Helix\DB;
6
use Helix\DB\Select;
7
8
/**
9
 * Produces comparative predicates for the instance.
10
 */
11
trait ComparisonTrait {
12
13
    use PredicateTrait;
14
15
    /**
16
     * @var DB
17
     */
18
    protected $db;
19
20
    /**
21
     * Null-safe equality.
22
     *
23
     * - Mysql: `$this <=> $arg`, or `$this <=> ANY ($arg)`
24
     * - Sqlite: `$this IS $arg`, or `$this IS ANY ($arg)`
25
     *
26
     * @param null|bool|string|Select $arg
27
     * @return Predicate
28
     */
29
    public function is ($arg): Predicate {
30
        if ($arg === null or is_bool($arg)) {
31
            $arg = ['' => 'NULL', 1 => 'TRUE', 0 => 'FALSE'][$arg];
32
        }
33
        else {
34
            $arg = $this->db->quote($arg);
35
        }
36
        $oper = ['mysql' => '<=>', 'sqlite' => 'IS'][$this->db->getDriver()];
37
        return Predicate::compare($this, $arg, $oper, 'ANY');
38
    }
39
40
    /**
41
     * `$this BETWEEN $min AND $max` (inclusive)
42
     *
43
     * @param number|string $min
44
     * @param number|string $max
45
     * @return Predicate
46
     */
47
    public function isBetween ($min, $max) {
48
        return new Predicate("{$this} BETWEEN {$this->db->quote($min)} AND {$this->db->quote($max)}");
49
    }
50
51
    /**
52
     * `$this = $arg`, or `$this IN (...$arg)`, or `$this = ANY ($arg)`
53
     *
54
     * @param string|array|Select $arg
55
     * @return Predicate
56
     */
57
    public function isEqual ($arg) {
58
        return Predicate::compare($this, $this->db->quoteMixed($arg));
59
    }
60
61
    /**
62
     * `$this > $arg`, or `$this > ALL ($arg)`
63
     *
64
     * @param string|Select $arg
65
     * @return Predicate
66
     */
67
    public function isGreater ($arg) {
68
        return Predicate::compare($this, $this->db->quote($arg), '>', 'ALL');
69
    }
70
71
    /**
72
     * `$this >= $arg`, or `$this >= ALL ($arg)`
73
     *
74
     * @param string|Select $arg
75
     * @return Predicate
76
     */
77
    public function isGreaterOrEqual ($arg) {
78
        return Predicate::compare($this, $this->db->quote($arg), '>=', 'ALL');
79
    }
80
81
    /**
82
     * `$this < $arg`, or `$this < ALL ($arg)`
83
     *
84
     * @param string|Select $arg
85
     * @return Predicate
86
     */
87
    public function isLess ($arg) {
88
        return Predicate::compare($this, $this->db->quote($arg), '<', 'ALL');
89
    }
90
91
    /**
92
     * `$this <= $arg`, or `$this <= ALL ($arg)`
93
     *
94
     * @param string|Select $arg
95
     * @return Predicate
96
     */
97
    public function isLessOrEqual ($arg) {
98
        return Predicate::compare($this, $this->db->quote($arg), '<=', 'ALL');
99
    }
100
101
    /**
102
     * `$this LIKE $pattern`
103
     *
104
     * @param string $pattern
105
     * @return Predicate
106
     */
107
    public function isLike (string $pattern) {
108
        return Predicate::compare($this, $this->db->quote($pattern), 'LIKE');
109
    }
110
111
    /**
112
     * Null-safe inequality.
113
     *
114
     * @param null|bool|string|Select $arg
115
     * @return Predicate
116
     */
117
    public function isNot ($arg) {
118
        return $this->is($arg)->invert();
119
    }
120
121
    /**
122
     * `$this NOT BETWEEN $min AND $max` (inclusive)
123
     *
124
     * @param number|string $min
125
     * @param number|string $max
126
     * @return Predicate
127
     */
128
    public function isNotBetween ($min, $max) {
129
        return new Predicate("{$this} NOT BETWEEN {$this->db->quote($min)} AND {$this->db->quote($max)}");
130
    }
131
132
    /**
133
     * `$this <> $arg`, or `$this NOT IN (...$arg)`, or `$this <> ALL ($arg)`
134
     *
135
     * @param string|array|Select $arg
136
     * @return Predicate
137
     */
138
    public function isNotEqual ($arg) {
139
        return Predicate::compare($this, $this->db->quoteMixed($arg), '<>', 'ALL', 'NOT IN');
140
    }
141
142
    /**
143
     * `$this NOT LIKE $pattern`
144
     *
145
     * @param string $pattern
146
     * @return Predicate
147
     */
148
    public function isNotLike (string $pattern) {
149
        return Predicate::compare($this, $this->db->quote($pattern), 'NOT LIKE');
150
    }
151
152
    /**
153
     * `$this NOT REGEXP $pattern`
154
     *
155
     * @param string $pattern
156
     * @return Predicate
157
     */
158
    public function isNotRegExp (string $pattern) {
159
        return Predicate::compare($this, $this->db->quote($pattern), 'NOT REGEXP');
160
    }
161
162
    /**
163
     * `$this REGEXP $pattern`
164
     *
165
     * @param string $pattern
166
     * @return Predicate
167
     */
168
    public function isRegExp (string $pattern) {
169
        return Predicate::compare($this, $this->db->quote($pattern), 'REGEXP');
170
    }
171
}