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