1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\DB\SQL; |
4
|
|
|
|
5
|
|
|
use Helix\DB; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Produces numeric expressions for the instance. |
9
|
|
|
*/ |
10
|
|
|
trait NumTrait { |
11
|
|
|
|
12
|
|
|
abstract public function __toString (); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var DB |
16
|
|
|
*/ |
17
|
|
|
protected $db; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* `ABS($this)` |
21
|
|
|
* |
22
|
|
|
* @return Num |
23
|
|
|
*/ |
24
|
|
|
public function abs () { |
25
|
|
|
return Num::factory($this->db, "ABS({$this})"); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* `($this + $arg)` |
30
|
|
|
* |
31
|
|
|
* @param number|ValueInterface $arg |
32
|
|
|
* @return Num |
33
|
|
|
*/ |
34
|
|
|
public function add ($arg) { |
35
|
|
|
return Num::factory($this->db, "({$this} + {$arg})"); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* `CEIL($this)` |
40
|
|
|
* |
41
|
|
|
* @return Num |
42
|
|
|
*/ |
43
|
|
|
public function ceil () { |
44
|
|
|
return Num::factory($this->db, "CEIL({$this})"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* `($this / $arg)` |
49
|
|
|
* |
50
|
|
|
* @param number|ValueInterface $arg |
51
|
|
|
* @return Num |
52
|
|
|
*/ |
53
|
|
|
public function divide ($arg) { |
54
|
|
|
return Num::factory($this->db, "({$this} / {$arg})"); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* `FLOOR($this)` |
59
|
|
|
* |
60
|
|
|
* @return Num |
61
|
|
|
*/ |
62
|
|
|
public function floor () { |
63
|
|
|
return Num::factory($this->db, "FLOOR({$this})"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* `($this % 2) = 0` |
68
|
|
|
* |
69
|
|
|
* @return Predicate |
70
|
|
|
*/ |
71
|
|
|
public function isEven () { |
72
|
|
|
return Predicate::factory($this->db, "({$this} % 2) = 0"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* `$this < 0` |
77
|
|
|
* |
78
|
|
|
* @return Predicate |
79
|
|
|
*/ |
80
|
|
|
public function isNegative () { |
81
|
|
|
return Predicate::factory($this->db, "{$this} < 0"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* `($this % 2) <> 0` |
86
|
|
|
* |
87
|
|
|
* @return Predicate |
88
|
|
|
*/ |
89
|
|
|
public function isOdd () { |
90
|
|
|
return Predicate::factory($this->db, "({$this} % 2) <> 0"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* `$this > 0` |
95
|
|
|
* |
96
|
|
|
* @return Predicate |
97
|
|
|
*/ |
98
|
|
|
public function isPositive () { |
99
|
|
|
return Predicate::factory($this->db, "{$this} > 0"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* `($this % $arg)` |
104
|
|
|
* |
105
|
|
|
* @param number|ValueInterface $arg |
106
|
|
|
* @return Num |
107
|
|
|
*/ |
108
|
|
|
public function modulo ($arg) { |
109
|
|
|
return Num::factory($this->db, "({$this} % {$arg})"); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* `($this * $arg)` |
114
|
|
|
* |
115
|
|
|
* @param number|ValueInterface $arg |
116
|
|
|
* @return Num |
117
|
|
|
*/ |
118
|
|
|
public function multiply ($arg) { |
119
|
|
|
return Num::factory($this->db, "({$this} * {$arg})"); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* `POW($this,$exponent)` |
124
|
|
|
* |
125
|
|
|
* @param number|ValueInterface $exponent |
126
|
|
|
* @return Num |
127
|
|
|
*/ |
128
|
|
|
public function pow ($exponent) { |
129
|
|
|
return Num::factory($this->db, "POW({$this},{$exponent})"); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* `ROUND($this,$decimals)` |
134
|
|
|
* |
135
|
|
|
* @param int $decimals |
136
|
|
|
* @return Num |
137
|
|
|
*/ |
138
|
|
|
public function round (int $decimals = 0) { |
139
|
|
|
return Num::factory($this->db, "ROUND({$this},{$decimals})"); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* `($this - $arg)` |
144
|
|
|
* |
145
|
|
|
* @param number|ValueInterface $arg |
146
|
|
|
* @return Num |
147
|
|
|
*/ |
148
|
|
|
public function subtract (ValueInterface $arg) { |
149
|
|
|
return Num::factory($this->db, "({$this} - {$arg})"); |
150
|
|
|
} |
151
|
|
|
} |