1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
<?php
namespace LesserPHP\Functions;
use Exception;
use LesserPHP\Constants;
use LesserPHP\Utils\Asserts;
use LesserPHP\Utils\Util;
/**
* Implements the math functions for LESS
*
* @link https://lesscss.org/functions/#math-functions
*/
class Math extends AbstractFunctionCollection
{
/** @inheritdoc */
public function getFunctions(): array
{
return [
'ceil' => [$this, 'ceil'],
'floor' => [$this, 'floor'],
'percentage' => [$this, 'percentage'],
'round' => [$this, 'round'],
'sqrt' => [$this, 'sqrt'],
'abs' => [$this, 'abs'],
'sin' => [$this, 'sin'],
'asin' => [$this, 'asin'],
'cos' => [$this, 'cos'],
'acos' => [$this, 'acos'],
'tan' => [$this, 'tan'],
'atan' => [$this, 'atan'],
'pi' => [$this, 'pi'],
'pow' => [$this, 'pow'],
'mod' => [$this, 'mod'],
'min' => [$this, 'min'],
'max' => [$this, 'max'],
];
}
/**
* Rounds up to the next highest integer
*
* @link https://lesscss.org/functions/#math-functions-ceil
* @throws Exception
*/
public function ceil(array $arg): array
{
$value = Asserts::assertNumber($arg);
return ['number', ceil($value), $arg[2]];
}
/**
* Rounds down to the next lowest integer
*
* @link https://lesscss.org/functions/#math-functions-floor
* @throws Exception
*/
public function floor(array $arg): array
{
$value = Asserts::assertNumber($arg);
return ['number', floor($value), $arg[2]];
}
/**
* Converts a floating point number into a percentage string
*
* @link https://lesscss.org/functions/#math-functions-percentage
* @throws Exception
*/
public function percentage(array $arg): array
{
$num = Asserts::assertNumber($arg);
return ['number', $num * 100, '%'];
}
/**
* Applies rounding
*
* @link https://lesscss.org/functions/#math-functions-round
* @throws Exception
*/
public function round(array $arg): array
{
if ($arg[0] != 'list') {
$value = Asserts::assertNumber($arg);
return ['number', round($value), $arg[2]];
} else {
$value = Asserts::assertNumber($arg[2][0]);
$precision = Asserts::assertNumber($arg[2][1]);
return ['number', round($value, $precision), $arg[2][0][2]];
}
}
/**
* Calculates square root of a number
*
* @link https://lesscss.org/functions/#math-functions-sqrt
* @throws Exception
*/
public function sqrt(array $num): float
{
return sqrt(Asserts::assertNumber($num));
}
/**
* Calculates absolute value of a number. Keeps units as they are.
*
* @link https://lesscss.org/functions/#math-functions-abs
* @throws Exception
*/
public function abs(array $num): array
{
return ['number', abs(Asserts::assertNumber($num)), $num[2]];
}
/**
* Calculates sine function
*
* @link https://lesscss.org/functions/#math-functions-sin
* @throws Exception
*/
public function sin(array $num): float
{
return sin(Asserts::assertNumber($num));
}
/**
* Calculates arcsine function
*
* @link https://lesscss.org/functions/#math-functions-asin
* @throws Exception
*/
public function asin(array $num): array
{
$num = asin(Asserts::assertNumber($num));
return ['number', $num, 'rad'];
}
/**
* Calculates cosine function
*
* @link https://lesscss.org/functions/#math-functions-cos
* @throws Exception
*/
public function cos(array $num): float
{
return cos(Asserts::assertNumber($num));
}
/**
* Calculates arccosine function
*
* @link https://lesscss.org/functions/#math-functions-acos
* @throws Exception
*/
public function acos(array $num): array
{
$num = acos(Asserts::assertNumber($num));
return ['number', $num, 'rad'];
}
/**
* Calculates tangent function
*
* @link https://lesscss.org/functions/#math-functions-tan
* @throws Exception
*/
public function tan(array $num): float
{
return tan(Asserts::assertNumber($num));
}
/**
* Calculates arctangent function
*
* @link https://lesscss.org/functions/#math-functions-atan
* @throws Exception
*/
public function atan(array $num): array
{
$num = atan(Asserts::assertNumber($num));
return ['number', $num, 'rad'];
}
/**
* Return the value of pi
*
* @link https://lesscss.org/functions/#math-functions-pi
*/
public function pi(): float
{
return pi();
}
/**
* Returns the value of the first argument raised to the power of the second argument.
*
* @link https://lesscss.org/functions/#math-functions-pow
* @throws Exception
*/
public function pow(array $args): array
{
[$base, $exp] = Asserts::assertArgs($args, 2, 'pow');
return ['number', Asserts::assertNumber($base) ** Asserts::assertNumber($exp), $args[2][0][2]];
}
/**
* Returns the value of the first argument modulus second argument.
*
* @link https://lesscss.org/functions/#math-functions-mod
* @throws Exception
*/
public function mod(array $args): array
{
[$a, $b] = Asserts::assertArgs($args, 2, 'mod');
return ['number', Asserts::assertNumber($a) % Asserts::assertNumber($b), $args[2][0][2]];
}
/**
* Returns the lowest of one or more values
*
* @link https://lesscss.org/functions/#math-functions-min
* @throws Exception
*/
public function min(array $args): array
{
$values = Asserts::assertMinArgs($args, 1, 'min');
$first_format = $values[0][2];
$min_index = 0;
$min_value = $values[0][1];
for ($a = 0; $a < sizeof($values); $a++) {
$converted = Util::convert($values[$a], $first_format);
if ($converted[1] < $min_value) {
$min_index = $a;
$min_value = $values[$a][1];
}
}
return $values[$min_index];
}
/**
* Returns the highest of one or more values
*
* @link https://lesscss.org/functions/#math-functions-max
* @throws Exception
*/
public function max(array $args): array
{
$values = Asserts::assertMinArgs($args, 1, 'max');
$first_format = $values[0][2];
$max_index = 0;
$max_value = $values[0][1];
for ($a = 0; $a < sizeof($values); $a++) {
$converted = Util::convert($values[$a], $first_format);
if ($converted[1] > $max_value) {
$max_index = $a;
$max_value = $values[$a][1];
}
}
return $values[$max_index];
}
}
|