Skip to content

Commit 8093d67

Browse files
author
MarkBaker
committed
Enable array-readiness for more Math/Trig functions; CEILING() FLOOR() (and variants), and TRUNC()
1 parent 866dd38 commit 8093d67

File tree

11 files changed

+234
-20
lines changed

11 files changed

+234
-20
lines changed

src/PhpSpreadsheet/Calculation/MathTrig.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -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
{

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/Trunc.php

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

33
namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
44

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

78
class Trunc
89
{
10+
use ArrayEnabled;
11+
912
/**
1013
* TRUNC.
1114
*
1215
* Truncates value to the number of fractional digits by number_digits.
1316
*
14-
* @param float $value
15-
* @param int $digits
17+
* @param array|float $value
18+
* Or can be an array of values
19+
* @param array|int $digits
20+
* Or can be an array of values
1621
*
17-
* @return float|string Truncated value, or a string containing an error
22+
* @return array|float|string Truncated value, or a string containing an error
23+
* If an array of numbers is passed as an argument, then the returned result will also be an array
24+
* with the same dimensions
1825
*/
1926
public static function evaluate($value = 0, $digits = 0)
2027
{
28+
if (is_array($value) || is_array($digits)) {
29+
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $digits);
30+
}
31+
2132
try {
2233
$value = Helpers::validateNumericNullBool($value);
2334
$digits = Helpers::validateNumericNullSubstitution($digits, null);

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/CeilingMathTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6+
57
class CeilingMathTest extends AllSetupTeardown
68
{
79
/**
@@ -27,4 +29,23 @@ public function providerCEILINGMATH(): array
2729
{
2830
return require 'tests/data/Calculation/MathTrig/CEILINGMATH.php';
2931
}
32+
33+
/**
34+
* @dataProvider providerCeilingArray
35+
*/
36+
public function testCeilingArray(array $expectedResult, string $argument1, string $argument2): void
37+
{
38+
$calculation = Calculation::getInstance();
39+
40+
$formula = "=CEILING.MATH({$argument1}, {$argument2})";
41+
$result = $calculation->_calculateFormulaValue($formula);
42+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
43+
}
44+
45+
public function providerCeilingArray(): array
46+
{
47+
return [
48+
'matrix' => [[[3.15, 3.142], [3.1416, 3.141594]], '3.1415926536', '{0.01, 0.002; 0.00005, 0.000002}'],
49+
];
50+
}
3051
}

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/CeilingPreciseTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6+
57
class CeilingPreciseTest extends AllSetupTeardown
68
{
79
/**
@@ -27,4 +29,23 @@ public function providerFLOORPRECISE(): array
2729
{
2830
return require 'tests/data/Calculation/MathTrig/CEILINGPRECISE.php';
2931
}
32+
33+
/**
34+
* @dataProvider providerCeilingArray
35+
*/
36+
public function testCeilingArray(array $expectedResult, string $argument1, string $argument2): void
37+
{
38+
$calculation = Calculation::getInstance();
39+
40+
$formula = "=CEILING.PRECISE({$argument1}, {$argument2})";
41+
$result = $calculation->_calculateFormulaValue($formula);
42+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
43+
}
44+
45+
public function providerCeilingArray(): array
46+
{
47+
return [
48+
'matrix' => [[[3.15, 3.142], [3.1416, 3.141594]], '3.1415926536', '{0.01, 0.002; 0.00005, 0.000002}'],
49+
];
50+
}
3051
}

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/CeilingTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6+
57
class CeilingTest extends AllSetupTeardown
68
{
79
/**
@@ -54,4 +56,23 @@ public function testCEILINGExcel1Arg(): void
5456
$result = $sheet->getCell('A1')->getCalculatedValue();
5557
self::assertEqualsWithDelta(6, $result, 1E-12);
5658
}
59+
60+
/**
61+
* @dataProvider providerCeilingArray
62+
*/
63+
public function testCeilingArray(array $expectedResult, string $argument1, string $argument2): void
64+
{
65+
$calculation = Calculation::getInstance();
66+
67+
$formula = "=CEILING({$argument1}, {$argument2})";
68+
$result = $calculation->_calculateFormulaValue($formula);
69+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
70+
}
71+
72+
public function providerCeilingArray(): array
73+
{
74+
return [
75+
'matrix' => [[[3.15, 3.142], [3.1416, 3.141594]], '3.1415926536', '{0.01, 0.002; 0.00005, 0.000002}'],
76+
];
77+
}
5778
}

tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/FloorMathTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
44

5+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6+
57
class FloorMathTest extends AllSetupTeardown
68
{
79
/**
@@ -27,4 +29,23 @@ public function providerFLOORMATH(): array
2729
{
2830
return require 'tests/data/Calculation/MathTrig/FLOORMATH.php';
2931
}
32+
33+
/**
34+
* @dataProvider providerFloorArray
35+
*/
36+
public function testFloorArray(array $expectedResult, string $argument1, string $argument2): void
37+
{
38+
$calculation = Calculation::getInstance();
39+
40+
$formula = "=FLOOR.MATH({$argument1}, {$argument2})";
41+
$result = $calculation->_calculateFormulaValue($formula);
42+
self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14);
43+
}
44+
45+
public function providerFloorArray(): array
46+
{
47+
return [
48+
'matrix' => [[[3.14, 3.14], [3.14155, 3.141592]], '3.1415926536', '{0.01, 0.002; 0.00005, 0.000002}'],
49+
];
50+
}
3051
}

0 commit comments

Comments
 (0)