1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\DB\Fluent\Num; |
4
|
|
|
|
5
|
|
|
use Helix\DB\Fluent\Num; |
6
|
|
|
use Helix\DB\Fluent\Predicate; |
7
|
|
|
use Helix\DB\Fluent\Value\ValueTrait; |
8
|
|
|
use Helix\DB\Fluent\ValueInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Numeric expression manipulation. |
12
|
|
|
* |
13
|
|
|
* This trait does not include {@link NumCastFloatTrait}, |
14
|
|
|
* because the expression is either already a float or an integer. |
15
|
|
|
*/ |
16
|
|
|
trait NumTrait |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
use ValueTrait; |
20
|
|
|
use BaseConversionTrait; |
21
|
|
|
use NumCastIntTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* `ABS($this)` |
25
|
|
|
* |
26
|
|
|
* @return Num |
27
|
|
|
*/ |
28
|
|
|
public function abs() |
29
|
|
|
{ |
30
|
|
|
return Num::factory($this->db, "ABS({$this})"); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* `ACOS($this)` |
35
|
|
|
* |
36
|
|
|
* @return Num |
37
|
|
|
*/ |
38
|
|
|
public function acos() |
39
|
|
|
{ |
40
|
|
|
return Num::factory($this->db, "ACOS({$this})"); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* `($this + $arg)` |
45
|
|
|
* |
46
|
|
|
* @param number|ValueInterface $arg |
47
|
|
|
* @return Num |
48
|
|
|
*/ |
49
|
|
|
public function add($arg) |
50
|
|
|
{ |
51
|
|
|
$arg = $this->db->quote($arg); |
52
|
|
|
return Num::factory($this->db, "({$this} + {$arg})"); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* `ASIN($this)` |
57
|
|
|
* |
58
|
|
|
* @return Num |
59
|
|
|
*/ |
60
|
|
|
public function asin() |
61
|
|
|
{ |
62
|
|
|
return Num::factory($this->db, "ASIN({$this})"); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* `ATAN($this)` |
67
|
|
|
* |
68
|
|
|
* @return Num |
69
|
|
|
*/ |
70
|
|
|
public function atan() |
71
|
|
|
{ |
72
|
|
|
return Num::factory($this->db, "ATAN({$this})"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Bitwise `AND` |
77
|
|
|
* |
78
|
|
|
* `($this & $value)` |
79
|
|
|
* |
80
|
|
|
* @param int|ValueInterface $value |
81
|
|
|
* @return Num |
82
|
|
|
*/ |
83
|
|
|
public function bAnd($value) |
84
|
|
|
{ |
85
|
|
|
return Num::factory($this->db, "({$this} & {$value})"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Bitwise `NOT` |
90
|
|
|
* |
91
|
|
|
* `($this ~ $value)` |
92
|
|
|
* |
93
|
|
|
* @param int|ValueInterface $value |
94
|
|
|
* @return Num |
95
|
|
|
*/ |
96
|
|
|
public function bNot($value) |
97
|
|
|
{ |
98
|
|
|
return Num::factory($this->db, "({$this} ~ {$value})"); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Bitwise `OR` |
103
|
|
|
* |
104
|
|
|
* `($this | $value)` |
105
|
|
|
* |
106
|
|
|
* @param int|ValueInterface $value |
107
|
|
|
* @return Num |
108
|
|
|
*/ |
109
|
|
|
public function bOr($value) |
110
|
|
|
{ |
111
|
|
|
return Num::factory($this->db, "({$this} | {$value})"); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Bitwise shift left. |
116
|
|
|
* |
117
|
|
|
* `($this << $bits)` |
118
|
|
|
* |
119
|
|
|
* @param int $bits |
120
|
|
|
* @return Num |
121
|
|
|
*/ |
122
|
|
|
public function bSL(int $bits = 1) |
123
|
|
|
{ |
124
|
|
|
return Num::factory($this->db, "({$this} << {$bits})"); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Bitwise shift right. |
129
|
|
|
* |
130
|
|
|
* `($this >> $bits)` |
131
|
|
|
* |
132
|
|
|
* @param int $bits |
133
|
|
|
* @return Num |
134
|
|
|
*/ |
135
|
|
|
public function bSR(int $bits = 1) |
136
|
|
|
{ |
137
|
|
|
return Num::factory($this->db, "({$this} >> {$bits})"); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* `CEIL($this)` |
142
|
|
|
* |
143
|
|
|
* @return Num |
144
|
|
|
*/ |
145
|
|
|
public function ceil() |
146
|
|
|
{ |
147
|
|
|
return Num::factory($this->db, "CEIL({$this})"); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* `COS($this)` |
152
|
|
|
* |
153
|
|
|
* @return Num |
154
|
|
|
*/ |
155
|
|
|
public function cos() |
156
|
|
|
{ |
157
|
|
|
return Num::factory($this->db, "COS({$this})"); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Radians to degrees. |
162
|
|
|
* |
163
|
|
|
* `DEGREES($this)` |
164
|
|
|
* |
165
|
|
|
* @return Num |
166
|
|
|
*/ |
167
|
|
|
public function degrees() |
168
|
|
|
{ |
169
|
|
|
return Num::factory($this->db, "DEGREES({$this})"); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* `($this / $arg)` |
174
|
|
|
* |
175
|
|
|
* @param number|ValueInterface $arg |
176
|
|
|
* @return Num |
177
|
|
|
*/ |
178
|
|
|
public function div($arg) |
179
|
|
|
{ |
180
|
|
|
$arg = $this->db->quote($arg); |
181
|
|
|
return Num::factory($this->db, "({$this} / {$arg})"); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Euler's constant raised to the power of the expression. |
186
|
|
|
* |
187
|
|
|
* `EXP($this)` |
188
|
|
|
* |
189
|
|
|
* @return Num |
190
|
|
|
*/ |
191
|
|
|
public function exp() |
192
|
|
|
{ |
193
|
|
|
return Num::factory($this->db, "EXP({$this})"); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* `FLOOR($this)` |
198
|
|
|
* |
199
|
|
|
* @return Num |
200
|
|
|
*/ |
201
|
|
|
public function floor() |
202
|
|
|
{ |
203
|
|
|
return Num::factory($this->db, "FLOOR({$this})"); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* `($this % 2) = 0` |
208
|
|
|
* |
209
|
|
|
* @return Predicate |
210
|
|
|
*/ |
211
|
|
|
public function isEven() |
212
|
|
|
{ |
213
|
|
|
return Predicate::factory($this->db, "({$this} % 2) = 0"); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* `$this < 0` |
218
|
|
|
* |
219
|
|
|
* @return Predicate |
220
|
|
|
*/ |
221
|
|
|
public function isNegative() |
222
|
|
|
{ |
223
|
|
|
return Predicate::factory($this->db, "{$this} < 0"); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* `($this % 2) <> 0` |
228
|
|
|
* |
229
|
|
|
* @return Predicate |
230
|
|
|
*/ |
231
|
|
|
public function isOdd() |
232
|
|
|
{ |
233
|
|
|
return Predicate::factory($this->db, "({$this} % 2) <> 0"); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* `$this > 0` |
238
|
|
|
* |
239
|
|
|
* @return Predicate |
240
|
|
|
*/ |
241
|
|
|
public function isPositive() |
242
|
|
|
{ |
243
|
|
|
return Predicate::factory($this->db, "{$this} > 0"); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* `LN($this)` |
248
|
|
|
* |
249
|
|
|
* @return Num |
250
|
|
|
*/ |
251
|
|
|
public function ln() |
252
|
|
|
{ |
253
|
|
|
return Num::factory($this->db, "LN({$this})"); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* `LOG($base,$this)` |
258
|
|
|
* |
259
|
|
|
* > Note: This is the cross-DBMS signature. PHP's built-in function has the reverse. |
260
|
|
|
* |
261
|
|
|
* @param float $base |
262
|
|
|
* @return Num |
263
|
|
|
*/ |
264
|
|
|
public function log(float $base) |
265
|
|
|
{ |
266
|
|
|
return Num::factory($this->db, "LOG({$base},{$this})"); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* `LOG10($this)` |
271
|
|
|
* |
272
|
|
|
* @return Num |
273
|
|
|
*/ |
274
|
|
|
public function log10() |
275
|
|
|
{ |
276
|
|
|
return Num::factory($this->db, "LOG10({$this})"); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* `LOG2($this)` |
281
|
|
|
* |
282
|
|
|
* @return Num |
283
|
|
|
*/ |
284
|
|
|
public function log2() |
285
|
|
|
{ |
286
|
|
|
return Num::factory($this->db, "LOG2({$this})"); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* `($this % $divisor)` |
291
|
|
|
* |
292
|
|
|
* @param float $divisor |
293
|
|
|
* @return Num |
294
|
|
|
*/ |
295
|
|
|
public function mod(float $divisor) |
296
|
|
|
{ |
297
|
|
|
return Num::factory($this->db, "({$this} % {$divisor})"); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* `($this * $arg)` |
302
|
|
|
* |
303
|
|
|
* @param number|ValueInterface $arg |
304
|
|
|
* @return Num |
305
|
|
|
*/ |
306
|
|
|
public function mul($arg) |
307
|
|
|
{ |
308
|
|
|
$arg = $this->db->quote($arg); |
309
|
|
|
return Num::factory($this->db, "({$this} * {$arg})"); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* `POW($this,$exponent)` |
314
|
|
|
* |
315
|
|
|
* @param float $exponent |
316
|
|
|
* @return Num |
317
|
|
|
*/ |
318
|
|
|
public function pow(float $exponent) |
319
|
|
|
{ |
320
|
|
|
return Num::factory($this->db, "POW({$this},{$exponent})"); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Degrees to radians. |
325
|
|
|
* |
326
|
|
|
* `RADIANS($this)` |
327
|
|
|
* |
328
|
|
|
* @return Num |
329
|
|
|
*/ |
330
|
|
|
public function radians() |
331
|
|
|
{ |
332
|
|
|
return Num::factory($this->db, "RADIANS({$this})"); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* `POW($this,1/$radix)` |
337
|
|
|
* |
338
|
|
|
* @param int $radix Non-zero. |
339
|
|
|
* @return Num |
340
|
|
|
*/ |
341
|
|
|
public function root(int $radix) |
342
|
|
|
{ |
343
|
|
|
return Num::factory($this->db, "POW({$this},1/{$radix})"); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* `ROUND($this,$decimals)` |
348
|
|
|
* |
349
|
|
|
* @param int $decimals |
350
|
|
|
* @return Num |
351
|
|
|
*/ |
352
|
|
|
public function round(int $decimals = 0) |
353
|
|
|
{ |
354
|
|
|
return Num::factory($this->db, "ROUND({$this},{$decimals})"); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* `SIGN($this)` |
359
|
|
|
* |
360
|
|
|
* @return Num `-1`, `0`, `1` |
361
|
|
|
*/ |
362
|
|
|
public function sign() |
363
|
|
|
{ |
364
|
|
|
return Num::factory($this->db, "SIGN({$this})"); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* `SIN($this)` |
369
|
|
|
* |
370
|
|
|
* @return Num |
371
|
|
|
*/ |
372
|
|
|
public function sin() |
373
|
|
|
{ |
374
|
|
|
return Num::factory($this->db, "SIN({$this})"); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* `SQRT($this)` |
379
|
|
|
* |
380
|
|
|
* @return Num |
381
|
|
|
*/ |
382
|
|
|
public function sqrt() |
383
|
|
|
{ |
384
|
|
|
return Num::factory($this->db, "SQRT({$this})"); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* `($this - $arg)` |
389
|
|
|
* |
390
|
|
|
* @param number|ValueInterface $arg |
391
|
|
|
* @return Num |
392
|
|
|
*/ |
393
|
|
|
public function sub($arg) |
394
|
|
|
{ |
395
|
|
|
$arg = $this->db->quote($arg); |
396
|
|
|
return Num::factory($this->db, "({$this} - {$arg})"); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* `TAN($this)` |
401
|
|
|
* |
402
|
|
|
* @return Num |
403
|
|
|
*/ |
404
|
|
|
public function tan() |
405
|
|
|
{ |
406
|
|
|
return Num::factory($this->db, "TAN({$this})"); |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|