Skip to content

Commit 8d4a12b

Browse files
author
MarkBaker
committed
Enable array-readiness for more Math/Trig functions; CEILING() FLOOR() (and variants), TRUNC(), BASE() and the various Logarithms
1 parent 866dd38 commit 8d4a12b

File tree

17 files changed

+377
-31
lines changed

17 files changed

+377
-31
lines changed

src/PhpSpreadsheet/Calculation/MathTrig.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static function ATAN2($xCoordinate = null, $yCoordinate = null)
7777
* @param float $radix
7878
* @param int $minLength
7979
*
80-
* @return string the text representation with the given radix (base)
80+
* @return array|string the text representation with the given radix (base)
8181
*/
8282
public static function BASE($number, $radix, $minLength = null)
8383
{
@@ -100,7 +100,7 @@ public static function BASE($number, $radix, $minLength = null)
100100
* @param float $number the number you want to round
101101
* @param float $significance the multiple to which you want to round
102102
*
103-
* @return float|string Rounded Number, or a string containing an error
103+
* @return array|float|string Rounded Number, or a string containing an error
104104
*
105105
* @see MathTrig\Ceiling::ceiling()
106106
* Use the ceiling() method in the MathTrig\Ceiling class instead
@@ -231,7 +231,7 @@ public static function FACTDOUBLE($factVal)
231231
* @param float $number Number to round
232232
* @param float $significance Significance
233233
*
234-
* @return float|string Rounded Number, or a string containing an error
234+
* @return array|float|string Rounded Number, or a string containing an error
235235
*
236236
*@see MathTrig\Floor::floor()
237237
* Use the floor() method in the MathTrig\Floor class instead
@@ -255,7 +255,7 @@ public static function FLOOR($number, $significance = null)
255255
* @param float $significance Significance
256256
* @param int $mode direction to round negative numbers
257257
*
258-
* @return float|string Rounded Number, or a string containing an error
258+
* @return array|float|string Rounded Number, or a string containing an error
259259
*
260260
*@see MathTrig\Floor::math()
261261
* Use the math() method in the MathTrig\Floor class instead
@@ -278,7 +278,7 @@ public static function FLOORMATH($number, $significance = null, $mode = 0)
278278
* @param float $number Number to round
279279
* @param float $significance Significance
280280
*
281-
* @return float|string Rounded Number, or a string containing an error
281+
* @return array|float|string Rounded Number, or a string containing an error
282282
*
283283
*@see MathTrig\Floor::precise()
284284
* Use the precise() method in the MathTrig\Floor class instead
@@ -375,7 +375,7 @@ public static function LCM(...$args)
375375
* @param float $number The positive real number for which you want the logarithm
376376
* @param float $base The base of the logarithm. If base is omitted, it is assumed to be 10.
377377
*
378-
* @return float|string The result, or a string containing an error
378+
* @return array|float|string The result, or a string containing an error
379379
*/
380380
public static function logBase($number, $base = 10)
381381
{
@@ -941,7 +941,7 @@ public static function SUMXMY2($matrixData1, $matrixData2)
941941
* @param float $value
942942
* @param int $digits
943943
*
944-
* @return float|string Truncated value, or a string containing an error
944+
* @return array|float|string Truncated value, or a string containing an error
945945
*/
946946
public static function TRUNC($value = 0, $digits = 0)
947947
{
@@ -1358,7 +1358,7 @@ public static function builtinEXP($number)
13581358
*
13591359
* @param mixed $number Should be numeric
13601360
*
1361-
* @return float|string Rounded number
1361+
* @return array|float|string Rounded number
13621362
*/
13631363
public static function builtinLN($number)
13641364
{
@@ -1377,7 +1377,7 @@ public static function builtinLN($number)
13771377
*
13781378
* @param mixed $number Should be numeric
13791379
*
1380-
* @return float|string Rounded number
1380+
* @return array|float|string Rounded number
13811381
*/
13821382
public static function builtinLOG10($number)
13831383
{

src/PhpSpreadsheet/Calculation/MathTrig/Base.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78

89
class Base
910
{
11+
use ArrayEnabled;
12+
1013
/**
1114
* BASE.
1215
*
@@ -16,13 +19,22 @@ class Base
1619
* BASE(Number, Radix [Min_length])
1720
*
1821
* @param mixed $number expect float
22+
* Or can be an array of values
1923
* @param mixed $radix expect float
24+
* Or can be an array of values
2025
* @param mixed $minLength expect int or null
26+
* Or can be an array of values
2127
*
22-
* @return string the text representation with the given radix (base)
28+
* @return array|string the text representation with the given radix (base)
29+
* If an array of numbers is passed as an argument, then the returned result will also be an array
30+
* with the same dimensions
2331
*/
2432
public static function evaluate($number, $radix, $minLength = null)
2533
{
34+
if (is_array($number) || is_array($radix) || is_array($minLength)) {
35+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $radix, $minLength);
36+
}
37+
2638
try {
2739
$number = (float) floor(Helpers::validateNumericNullBool($number));
2840
$radix = (int) Helpers::validateNumericNullBool($radix);

src/PhpSpreadsheet/Calculation/MathTrig/Ceiling.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78

89
class Ceiling
910
{
11+
use ArrayEnabled;
12+
1013
/**
1114
* CEILING.
1215
*
@@ -18,13 +21,21 @@ class Ceiling
1821
* Excel Function:
1922
* CEILING(number[,significance])
2023
*
21-
* @param float $number the number you want the ceiling
22-
* @param float $significance the multiple to which you want to round
24+
* @param array|float $number the number you want the ceiling
25+
* Or can be an array of values
26+
* @param array|float $significance the multiple to which you want to round
27+
* Or can be an array of values
2328
*
24-
* @return float|string Rounded Number, or a string containing an error
29+
* @return array|float|string Rounded Number, or a string containing an error
30+
* If an array of numbers is passed as an argument, then the returned result will also be an array
31+
* with the same dimensions
2532
*/
2633
public static function ceiling($number, $significance = null)
2734
{
35+
if (is_array($number) || is_array($significance)) {
36+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
37+
}
38+
2839
if ($significance === null) {
2940
self::floorCheck1Arg();
3041
}
@@ -48,13 +59,22 @@ public static function ceiling($number, $significance = null)
4859
* CEILING.MATH(number[,significance[,mode]])
4960
*
5061
* @param mixed $number Number to round
62+
* Or can be an array of values
5163
* @param mixed $significance Significance
52-
* @param int $mode direction to round negative numbers
64+
* Or can be an array of values
65+
* @param array|int $mode direction to round negative numbers
66+
* Or can be an array of values
5367
*
54-
* @return float|string Rounded Number, or a string containing an error
68+
* @return array|float|string Rounded Number, or a string containing an error
69+
* If an array of numbers is passed as an argument, then the returned result will also be an array
70+
* with the same dimensions
5571
*/
5672
public static function math($number, $significance = null, $mode = 0)
5773
{
74+
if (is_array($number) || is_array($significance) || is_array($mode)) {
75+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance, $mode);
76+
}
77+
5878
try {
5979
$number = Helpers::validateNumericNullBool($number);
6080
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
@@ -82,12 +102,20 @@ public static function math($number, $significance = null, $mode = 0)
82102
* CEILING.PRECISE(number[,significance])
83103
*
84104
* @param mixed $number the number you want to round
85-
* @param float $significance the multiple to which you want to round
105+
* Or can be an array of values
106+
* @param array|float $significance the multiple to which you want to round
107+
* Or can be an array of values
86108
*
87-
* @return float|string Rounded Number, or a string containing an error
109+
* @return array|float|string Rounded Number, or a string containing an error
110+
* If an array of numbers is passed as an argument, then the returned result will also be an array
111+
* with the same dimensions
88112
*/
89113
public static function precise($number, $significance = 1)
90114
{
115+
if (is_array($number) || is_array($significance)) {
116+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
117+
}
118+
91119
try {
92120
$number = Helpers::validateNumericNullBool($number);
93121
$significance = Helpers::validateNumericNullSubstitution($significance, null);

src/PhpSpreadsheet/Calculation/MathTrig/Floor.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
78

89
class Floor
910
{
11+
use ArrayEnabled;
12+
1013
private static function floorCheck1Arg(): void
1114
{
1215
$compatibility = Functions::getCompatibilityMode();
@@ -24,12 +27,20 @@ private static function floorCheck1Arg(): void
2427
* FLOOR(number[,significance])
2528
*
2629
* @param mixed $number Expect float. Number to round
30+
* Or can be an array of values
2731
* @param mixed $significance Expect float. Significance
32+
* Or can be an array of values
2833
*
29-
* @return float|string Rounded Number, or a string containing an error
34+
* @return array|float|string Rounded Number, or a string containing an error
35+
* If an array of numbers is passed as an argument, then the returned result will also be an array
36+
* with the same dimensions
3037
*/
3138
public static function floor($number, $significance = null)
3239
{
40+
if (is_array($number) || is_array($significance)) {
41+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
42+
}
43+
3344
if ($significance === null) {
3445
self::floorCheck1Arg();
3546
}
@@ -53,13 +64,22 @@ public static function floor($number, $significance = null)
5364
* FLOOR.MATH(number[,significance[,mode]])
5465
*
5566
* @param mixed $number Number to round
67+
* Or can be an array of values
5668
* @param mixed $significance Significance
69+
* Or can be an array of values
5770
* @param mixed $mode direction to round negative numbers
71+
* Or can be an array of values
5872
*
59-
* @return float|string Rounded Number, or a string containing an error
73+
* @return array|float|string Rounded Number, or a string containing an error
74+
* If an array of numbers is passed as an argument, then the returned result will also be an array
75+
* with the same dimensions
6076
*/
6177
public static function math($number, $significance = null, $mode = 0)
6278
{
79+
if (is_array($number) || is_array($significance) || is_array($mode)) {
80+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance, $mode);
81+
}
82+
6383
try {
6484
$number = Helpers::validateNumericNullBool($number);
6585
$significance = Helpers::validateNumericNullSubstitution($significance, ($number < 0) ? -1 : 1);
@@ -79,13 +99,21 @@ public static function math($number, $significance = null, $mode = 0)
7999
* Excel Function:
80100
* FLOOR.PRECISE(number[,significance])
81101
*
82-
* @param float $number Number to round
83-
* @param float $significance Significance
102+
* @param array|float $number Number to round
103+
* Or can be an array of values
104+
* @param array|float $significance Significance
105+
* Or can be an array of values
84106
*
85-
* @return float|string Rounded Number, or a string containing an error
107+
* @return array|float|string Rounded Number, or a string containing an error
108+
* If an array of numbers is passed as an argument, then the returned result will also be an array
109+
* with the same dimensions
86110
*/
87111
public static function precise($number, $significance = 1)
88112
{
113+
if (is_array($number) || is_array($significance)) {
114+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $significance);
115+
}
116+
89117
try {
90118
$number = Helpers::validateNumericNullBool($number);
91119
$significance = Helpers::validateNumericNullSubstitution($significance, null);

src/PhpSpreadsheet/Calculation/MathTrig/Logarithms.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
56
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
67

78
class Logarithms
89
{
10+
use ArrayEnabled;
11+
912
/**
1013
* LOG_BASE.
1114
*
@@ -15,12 +18,20 @@ class Logarithms
1518
* LOG(number[,base])
1619
*
1720
* @param mixed $number The positive real number for which you want the logarithm
21+
* Or can be an array of values
1822
* @param mixed $base The base of the logarithm. If base is omitted, it is assumed to be 10.
23+
* Or can be an array of values
1924
*
20-
* @return float|string The result, or a string containing an error
25+
* @return array|float|string The result, or a string containing an error
26+
* If an array of numbers is passed as an argument, then the returned result will also be an array
27+
* with the same dimensions
2128
*/
2229
public static function withBase($number, $base = 10)
2330
{
31+
if (is_array($number) || is_array($base)) {
32+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $number, $base);
33+
}
34+
2435
try {
2536
$number = Helpers::validateNumericNullBool($number);
2637
Helpers::validatePositive($number);
@@ -39,11 +50,18 @@ public static function withBase($number, $base = 10)
3950
* Returns the result of builtin function log after validating args.
4051
*
4152
* @param mixed $number Should be numeric
53+
* Or can be an array of values
4254
*
43-
* @return float|string Rounded number
55+
* @return array|float|string Rounded number
56+
* If an array of numbers is passed as an argument, then the returned result will also be an array
57+
* with the same dimensions
4458
*/
4559
public static function base10($number)
4660
{
61+
if (is_array($number)) {
62+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number);
63+
}
64+
4765
try {
4866
$number = Helpers::validateNumericNullBool($number);
4967
Helpers::validatePositive($number);
@@ -60,11 +78,18 @@ public static function base10($number)
6078
* Returns the result of builtin function log after validating args.
6179
*
6280
* @param mixed $number Should be numeric
81+
* Or can be an array of values
6382
*
64-
* @return float|string Rounded number
83+
* @return array|float|string Rounded number
84+
* If an array of numbers is passed as an argument, then the returned result will also be an array
85+
* with the same dimensions
6586
*/
6687
public static function natural($number)
6788
{
89+
if (is_array($number)) {
90+
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $number);
91+
}
92+
6893
try {
6994
$number = Helpers::validateNumericNullBool($number);
7095
Helpers::validatePositive($number);

0 commit comments

Comments
 (0)