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