1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Number functions. |
5
|
|
|
* |
6
|
|
|
* This file is part of PinkCrab Function Constructors. |
7
|
|
|
* |
8
|
|
|
* PinkCrab Function Constructors is free software: you can redistribute it and/or modify it under the terms of the |
9
|
|
|
* GNU General Public License as published by the Free Software Foundation, either version 2 |
10
|
|
|
* of the License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* PinkCrab Function Constructors is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without |
13
|
|
|
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
14
|
|
|
* See the GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License along with PinkCrab Function Constructors. |
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
18
|
|
|
* |
19
|
|
|
* @author Glynn Quelch <[email protected]> |
20
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html |
21
|
|
|
* @package PinkCrab\FunctionConstructors |
22
|
|
|
* @since 0.0.1 |
23
|
|
|
* |
24
|
|
|
* @template Number of int|float |
25
|
|
|
* @phpstan-template Number of int|float |
26
|
|
|
* @psalm-template Number of int|float |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
declare(strict_types=1); |
30
|
|
|
|
31
|
|
|
namespace PinkCrab\FunctionConstructors\Numbers; |
32
|
|
|
|
33
|
|
|
use Closure; |
34
|
|
|
use InvalidArgumentException; |
35
|
|
|
use PinkCrab\FunctionConstructors\Comparisons as C; |
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Used to accumulate integers |
39
|
|
|
* |
40
|
|
|
* @param int $initial |
41
|
|
|
* @return Closure(int|null):(Closure|int) |
42
|
|
|
*/ |
43
|
|
|
function accumulatorInt(int $initial = 0): Closure |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @param int|null $value |
47
|
|
|
* @return Closure(int|null):(Closure|int)|int |
48
|
|
|
*/ |
49
|
|
|
return function (?int $value = null) use ($initial) { |
50
|
|
|
if ($value) { |
|
|
|
|
51
|
|
|
$initial += $value; |
52
|
|
|
} |
53
|
|
|
return $value ? accumulatorInt($initial) : $initial; |
54
|
|
|
}; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Used to accumulate floats |
59
|
|
|
* |
60
|
|
|
* @param float $initial |
61
|
|
|
* @return Closure(float|null):(Closure|float) |
62
|
|
|
*/ |
63
|
|
|
function accumulatorFloat(float $initial = 0): Closure |
64
|
|
|
{ |
65
|
|
|
/** |
66
|
|
|
* @param float|null $value |
67
|
|
|
* @return Closure(float|null):(Closure|float)|float |
68
|
|
|
*/ |
69
|
|
|
return function (?float $value = null) use ($initial) { |
70
|
|
|
if ($value) { |
71
|
|
|
$initial += $value; |
72
|
|
|
} |
73
|
|
|
return $value ? accumulatorFloat($initial) : $initial; |
74
|
|
|
}; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns a function for adding a fixed amount. |
79
|
|
|
* |
80
|
|
|
* @param Number $initial Defaults to 0 |
81
|
|
|
* @return Closure(Number):Number |
82
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
83
|
|
|
*/ |
84
|
|
|
function sum($initial = 0): Closure |
85
|
|
|
{ |
86
|
|
|
if (!C\isNumber($initial)) { |
|
|
|
|
87
|
|
|
throw new InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int)'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Number $value |
92
|
|
|
* @return int|float |
93
|
|
|
*/ |
94
|
|
|
return function ($value) use ($initial) { |
95
|
|
|
return $initial + $value; |
96
|
|
|
}; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns a function for adding a fixed amount. |
102
|
|
|
* |
103
|
|
|
* @param int $initial Defaults to 0 |
104
|
|
|
* @return Closure(Number):Number |
105
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
106
|
|
|
*/ |
107
|
|
|
function subtract($initial = 0): Closure |
108
|
|
|
{ |
109
|
|
|
if (!C\isNumber($initial)) { |
|
|
|
|
110
|
|
|
throw new InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int)'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Number $value |
115
|
|
|
* @return int|float |
116
|
|
|
*/ |
117
|
|
|
return function ($value) use ($initial) { |
118
|
|
|
return $value - $initial; |
119
|
|
|
}; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns a function for multiplying a fixed amount. |
125
|
|
|
* |
126
|
|
|
* @param Number $initial Defaults to 1 |
127
|
|
|
* @return Closure(Number):Number |
128
|
|
|
* @throws InvalidArgumentException |
129
|
|
|
*/ |
130
|
|
|
function multiply($initial = 1): Closure |
131
|
|
|
{ |
132
|
|
|
if (!C\isNumber($initial)) { |
|
|
|
|
133
|
|
|
throw new InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int)'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Number $value |
138
|
|
|
* @return Number |
139
|
|
|
*/ |
140
|
|
|
return function ($value) use ($initial) { |
141
|
|
|
return $value * $initial; |
142
|
|
|
}; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns a function for divideBy a fixed amount. |
149
|
|
|
* |
150
|
|
|
* @param float $divisor The value to divide the passed value by |
151
|
|
|
* @return Closure(Number):float |
152
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
153
|
|
|
*/ |
154
|
|
|
function divideBy($divisor = 1): Closure |
155
|
|
|
{ |
156
|
|
|
if (!C\isNumber($divisor)) { |
|
|
|
|
157
|
|
|
throw new \InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int)'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param float $value |
162
|
|
|
* @return float |
163
|
|
|
*/ |
164
|
|
|
return function ($value) use ($divisor): float { |
165
|
|
|
return $value / $divisor; |
166
|
|
|
}; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns a function for divideInto a fixed amount. |
171
|
|
|
* |
172
|
|
|
* @param float $dividend The value to divide the passed value by |
173
|
|
|
* @return Closure(Number):float |
174
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
175
|
|
|
*/ |
176
|
|
|
function divideInto($dividend = 1): Closure |
177
|
|
|
{ |
178
|
|
|
if (!C\isNumber($dividend)) { |
|
|
|
|
179
|
|
|
throw new \InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int)'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param float $value |
184
|
|
|
* @return float |
185
|
|
|
*/ |
186
|
|
|
return function ($value) use ($dividend): float { |
187
|
|
|
return $dividend / $value; |
188
|
|
|
}; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Returns a function for getting the remainder with a fixed divisor. |
193
|
|
|
* |
194
|
|
|
* @param float $divisor |
195
|
|
|
* @return Closure(Number):float |
196
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
197
|
|
|
*/ |
198
|
|
|
function remainderBy($divisor = 1): Closure |
199
|
|
|
{ |
200
|
|
|
if (!C\isNumber($divisor)) { |
|
|
|
|
201
|
|
|
throw new \InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int)'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param float $value |
206
|
|
|
* @return float |
207
|
|
|
*/ |
208
|
|
|
return function ($value) use ($divisor): float { |
209
|
|
|
return $value % $divisor; |
210
|
|
|
}; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns a function for getting the remainder with a fixed dividend. |
215
|
|
|
* |
216
|
|
|
* @param float $dividend |
217
|
|
|
* @return Closure(Number):float |
218
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
219
|
|
|
*/ |
220
|
|
|
function remainderInto($dividend = 1): Closure |
221
|
|
|
{ |
222
|
|
|
if (!C\isNumber($dividend)) { |
|
|
|
|
223
|
|
|
throw new \InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int)'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param float $value |
228
|
|
|
* @return float |
229
|
|
|
*/ |
230
|
|
|
return function ($value) use ($dividend): float { |
231
|
|
|
return $dividend % $value; |
232
|
|
|
}; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Returns a function for checking if a number is multiple of the pre defined value. |
237
|
|
|
* |
238
|
|
|
* @param Number $multiple |
239
|
|
|
* @return Closure(Number):bool |
240
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
241
|
|
|
*/ |
242
|
|
|
function isMultipleOf($multiple): Closure |
243
|
|
|
{ |
244
|
|
|
if (!C\isNumber($multiple)) { |
|
|
|
|
245
|
|
|
throw new \InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int)'); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param Number $value |
250
|
|
|
* @return bool |
251
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
252
|
|
|
*/ |
253
|
|
|
return function ($value) use ($multiple): bool { |
254
|
|
|
if (!C\isNumber($value)) { |
|
|
|
|
255
|
|
|
throw new \InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int)'); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
// Return false if 0 |
259
|
|
|
if ($value === 0) { |
260
|
|
|
return false; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $value % $multiple === 0; |
264
|
|
|
}; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Returns a function for checking if a number has a factor of another number. |
269
|
|
|
* |
270
|
|
|
* @param Number $factor |
271
|
|
|
* @return Closure(Number):bool |
272
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
273
|
|
|
*/ |
274
|
|
|
function isFactorOf($factor): Closure |
275
|
|
|
{ |
276
|
|
|
if (!C\isNumber($factor)) { |
|
|
|
|
277
|
|
|
throw new \InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int) for the factor'); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param Number $value |
282
|
|
|
* @return bool |
283
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
284
|
|
|
*/ |
285
|
|
|
return function ($value) use ($factor): bool { |
286
|
|
|
if (!C\isNumber($value)) { |
|
|
|
|
287
|
|
|
throw new \InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int) for the value'); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
// Return false if 0 |
291
|
|
|
if ($value === 0) { |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return $factor % $value === 0; |
296
|
|
|
}; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Returns a function for getting the remainder with a fixed dividend. |
302
|
|
|
* |
303
|
|
|
* @param int $precision Number of decimal places. |
304
|
|
|
* @return Closure(Number):float |
305
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
306
|
|
|
*/ |
307
|
|
|
function round($precision = 1): Closure |
308
|
|
|
{ |
309
|
|
|
if (!C\isNumber($precision)) { |
|
|
|
|
310
|
|
|
throw new \InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int)'); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @param Number $value |
315
|
|
|
* @return float |
316
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
317
|
|
|
*/ |
318
|
|
|
return function ($value) use ($precision): float { |
319
|
|
|
if (!C\isNumber($value)) { |
|
|
|
|
320
|
|
|
throw new \InvalidArgumentException("Num\\round() only accepts a valid Number ( Int|Float -> Float )"); |
321
|
|
|
} |
322
|
|
|
return \round(\floatval($value), $precision); |
323
|
|
|
}; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Returns a closure for raising the power of the passed by value, by a pre defined exponent. |
328
|
|
|
* |
329
|
|
|
* @param Number $exponent |
330
|
|
|
* @return Closure(Number):Number |
331
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
332
|
|
|
*/ |
333
|
|
|
function power($exponent): Closure |
334
|
|
|
{ |
335
|
|
|
if (!C\isNumber($exponent)) { |
|
|
|
|
336
|
|
|
throw new \InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int) for the exponent'); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @param Number $value |
341
|
|
|
* @return Number |
342
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
343
|
|
|
*/ |
344
|
|
|
return function ($value) use ($exponent) { |
345
|
|
|
if (!C\isNumber($value)) { |
|
|
|
|
346
|
|
|
throw new \InvalidArgumentException('Num\\power() only accepts a valid Number ( Int|Float )'); |
347
|
|
|
} |
348
|
|
|
return \pow($value, $exponent); |
349
|
|
|
}; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Returns closure for getting the pre defined root of a passed value. |
354
|
|
|
* |
355
|
|
|
* @param Number $root |
356
|
|
|
* @return Closure(Number):Number |
357
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
358
|
|
|
*/ |
359
|
|
|
function root($root): Closure |
360
|
|
|
{ |
361
|
|
|
if (!C\isNumber($root)) { |
|
|
|
|
362
|
|
|
throw new \InvalidArgumentException(__FUNCTION__ . 'only accepts a Number (Float or Int) for the root'); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @param Number $value |
367
|
|
|
* @return Number |
368
|
|
|
* @throws InvalidArgumentException If neither int or float passed. |
369
|
|
|
*/ |
370
|
|
|
return function ($value) use ($root) { |
371
|
|
|
if (!C\isNumber($value)) { |
|
|
|
|
372
|
|
|
throw new \InvalidArgumentException('Num\\root() only accepts a valid Number ( Int|Float )'); |
373
|
|
|
} |
374
|
|
|
return pow($value, (1 / $root)); |
375
|
|
|
}; |
376
|
|
|
} |
377
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths