From b3ff2e347f318dff63ba0a95e51f0ea34de39c37 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Fri, 11 Feb 2022 20:08:34 +0100 Subject: [PATCH] All Text functions made array-ready --- phpstan-baseline.neon | 7 ++- .../Calculation/Financial/Dollar.php | 7 ++- src/PhpSpreadsheet/Calculation/TextData.php | 20 +++---- .../Calculation/TextData/Format.php | 58 +++++++++++++++++-- .../Calculation/TextData/Replace.php | 33 ++++++++++- .../Calculation/TextData/Text.php | 35 ++++++++++- .../Functions/TextData/DollarTest.php | 23 ++++++++ .../Functions/TextData/ExactTest.php | 23 ++++++++ .../Functions/TextData/FixedTest.php | 23 ++++++++ .../Functions/TextData/LenTest.php | 23 ++++++++ .../Functions/TextData/NumberValueTest.php | 22 +++++++ .../Functions/TextData/ReplaceTest.php | 26 +++++++++ .../Functions/TextData/SubstituteTest.php | 21 +++++++ .../Calculation/Functions/TextData/TTest.php | 22 +++++++ .../Functions/TextData/TextTest.php | 29 ++++++++++ .../Functions/TextData/ValueTest.php | 20 +++++++ 16 files changed, 369 insertions(+), 23 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 4f4330e6..4c60cfe7 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1946,7 +1946,7 @@ parameters: path: src/PhpSpreadsheet/Calculation/TextData/Format.php - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\Format\\:\\:VALUE\\(\\) should return DateTimeInterface\\|float\\|int\\|string but returns mixed\\.$#" + message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\Format\\:\\:VALUE\\(\\) should return array\\|DateTimeInterface\\|float\\|int\\|string but returns mixed\\.$#" count: 2 path: src/PhpSpreadsheet/Calculation/TextData/Format.php @@ -1990,6 +1990,11 @@ parameters: count: 1 path: src/PhpSpreadsheet/Calculation/TextData/Helpers.php + - + message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\\\Replace\\:\\:substitute\\(\\) should return array\\|string but returns mixed\\.$#" + count: 1 + path: src/PhpSpreadsheet/Calculation/TextData/Replace.php + - message: "#^Variable \\$value on left side of \\?\\? always exists and is not nullable\\.$#" count: 1 diff --git a/src/PhpSpreadsheet/Calculation/Financial/Dollar.php b/src/PhpSpreadsheet/Calculation/Financial/Dollar.php index 26886a94..0dde0c16 100644 --- a/src/PhpSpreadsheet/Calculation/Financial/Dollar.php +++ b/src/PhpSpreadsheet/Calculation/Financial/Dollar.php @@ -18,12 +18,17 @@ class Dollar * The format used is $#,##0.00_);($#,##0.00).. * * @param mixed $number The value to format, or can be an array of numbers + * Or can be an array of values * @param mixed $precision The number of digits to display to the right of the decimal point (as an integer). * If precision is negative, number is rounded to the left of the decimal point. * If you omit precision, it is assumed to be 2 * Or can be an array of precision values + * + * @return array|string + * If an array of values is passed for either of the arguments, then the returned result + * will also be an array with matching dimensions */ - public static function format($number, $precision = 2): string + public static function format($number, $precision = 2) { return Format::DOLLAR($number, $precision); } diff --git a/src/PhpSpreadsheet/Calculation/TextData.php b/src/PhpSpreadsheet/Calculation/TextData.php index c8b9e58a..6757a8d8 100644 --- a/src/PhpSpreadsheet/Calculation/TextData.php +++ b/src/PhpSpreadsheet/Calculation/TextData.php @@ -102,7 +102,7 @@ class TextData * If decimals is negative, number is rounded to the left of the decimal point. * If you omit decimals, it is assumed to be 2 * - * @return string + * @return array|string */ public static function DOLLAR($value = 0, $decimals = 2) { @@ -156,7 +156,7 @@ class TextData * @param int $decimals * @param bool $no_commas * - * @return string + * @return array|string */ public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = false) { @@ -224,7 +224,7 @@ class TextData * * @param string $value Value * - * @return int + * @return array|int */ public static function STRINGLENGTH($value = '') { @@ -297,7 +297,7 @@ class TextData * @param int $chars Number of characters * @param string $newText String to replace in defined position * - * @return string + * @return array|string */ public static function REPLACE($oldText, $start, $chars, $newText) { @@ -316,7 +316,7 @@ class TextData * @param string $toText To Value * @param int $instance Instance Number * - * @return string + * @return array|string */ public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0) { @@ -332,7 +332,7 @@ class TextData * * @param mixed $testValue Value to check * - * @return null|string + * @return null|array|string */ public static function RETURNSTRING($testValue = '') { @@ -349,7 +349,7 @@ class TextData * @param mixed $value Value to check * @param string $format Format mask to use * - * @return string + * @return array|string */ public static function TEXTFORMAT($value, $format) { @@ -365,7 +365,7 @@ class TextData * * @param mixed $value Value to check * - * @return DateTimeInterface|float|int|string A string if arguments are invalid + * @return array|DateTimeInterface|float|int|string A string if arguments are invalid */ public static function VALUE($value = '') { @@ -383,7 +383,7 @@ class TextData * @param string $decimalSeparator decimal separator, defaults to locale defined value * @param string $groupSeparator group/thosands separator, defaults to locale defined value * - * @return float|string + * @return array|float|string */ public static function NUMBERVALUE($value = '', $decimalSeparator = null, $groupSeparator = null) { @@ -402,7 +402,7 @@ class TextData * @param mixed $value1 * @param mixed $value2 * - * @return bool + * @return array|bool */ public static function EXACT($value1, $value2) { diff --git a/src/PhpSpreadsheet/Calculation/TextData/Format.php b/src/PhpSpreadsheet/Calculation/TextData/Format.php index 69b80bde..ea579013 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Format.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Format.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; use DateTimeInterface; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel; use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; use PhpOffice\PhpSpreadsheet\Calculation\Functions; @@ -13,6 +14,8 @@ use PhpOffice\PhpSpreadsheet\Style\NumberFormat; class Format { + use ArrayEnabled; + /** * DOLLAR. * @@ -20,12 +23,22 @@ class Format * The format used is $#,##0.00_);($#,##0.00).. * * @param mixed $value The value to format + * Or can be an array of values * @param mixed $decimals The number of digits to display to the right of the decimal point (as an integer). * If decimals is negative, number is rounded to the left of the decimal point. * If you omit decimals, it is assumed to be 2 + * Or can be an array of values + * + * @return array|string + * If an array of values is passed for either of the arguments, then the returned result + * will also be an array with matching dimensions */ - public static function DOLLAR($value = 0, $decimals = 2): string + public static function DOLLAR($value = 0, $decimals = 2) { + if (is_array($value) || is_array($decimals)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $decimals); + } + try { $value = Helpers::extractFloat($value); $decimals = Helpers::extractInt($decimals, -100, 0, true); @@ -52,11 +65,22 @@ class Format * FIXED. * * @param mixed $value The value to format + * Or can be an array of values * @param mixed $decimals Integer value for the number of decimal places that should be formatted + * Or can be an array of values * @param mixed $noCommas Boolean value indicating whether the value should have thousands separators or not + * Or can be an array of values + * + * @return array|string + * If an array of values is passed for either of the arguments, then the returned result + * will also be an array with matching dimensions */ - public static function FIXEDFORMAT($value, $decimals = 2, $noCommas = false): string + public static function FIXEDFORMAT($value, $decimals = 2, $noCommas = false) { + if (is_array($value) || is_array($decimals) || is_array($noCommas)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $decimals, $noCommas); + } + try { $value = Helpers::extractFloat($value); $decimals = Helpers::extractInt($decimals, -100, 0, true); @@ -85,10 +109,20 @@ class Format * TEXT. * * @param mixed $value The value to format + * Or can be an array of values * @param mixed $format A string with the Format mask that should be used + * Or can be an array of values + * + * @return array|string + * If an array of values is passed for either of the arguments, then the returned result + * will also be an array with matching dimensions */ - public static function TEXTFORMAT($value, $format): string + public static function TEXTFORMAT($value, $format) { + if (is_array($value) || is_array($format)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $format); + } + $value = Helpers::extractString($value); $format = Helpers::extractString($format); @@ -122,11 +156,18 @@ class Format * VALUE. * * @param mixed $value Value to check + * Or can be an array of values * - * @return DateTimeInterface|float|int|string A string if arguments are invalid + * @return array|DateTimeInterface|float|int|string A string if arguments are invalid + * If an array of values is passed for the argument, then the returned result + * will also be an array with matching dimensions */ public static function VALUE($value = '') { + if (is_array($value)) { + return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); + } + try { $value = self::convertValue($value); } catch (CalcExp $e) { @@ -191,13 +232,20 @@ class Format * NUMBERVALUE. * * @param mixed $value The value to format + * Or can be an array of values * @param mixed $decimalSeparator A string with the decimal separator to use, defaults to locale defined value + * Or can be an array of values * @param mixed $groupSeparator A string with the group/thousands separator to use, defaults to locale defined value + * Or can be an array of values * - * @return float|string + * @return array|float|string */ public static function NUMBERVALUE($value = '', $decimalSeparator = null, $groupSeparator = null) { + if (is_array($value) || is_array($decimalSeparator) || is_array($groupSeparator)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $decimalSeparator, $groupSeparator); + } + try { $value = self::convertValue($value); $decimalSeparator = self::getDecimalSeparator($decimalSeparator); diff --git a/src/PhpSpreadsheet/Calculation/TextData/Replace.php b/src/PhpSpreadsheet/Calculation/TextData/Replace.php index 36c13590..2d3b69f3 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Replace.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Replace.php @@ -2,21 +2,36 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; use PhpOffice\PhpSpreadsheet\Calculation\Functions; class Replace { + use ArrayEnabled; + /** * REPLACE. * * @param mixed $oldText The text string value to modify + * Or can be an array of values * @param mixed $start Integer offset for start character of the replacement + * Or can be an array of values * @param mixed $chars Integer number of characters to replace from the start offset + * Or can be an array of values * @param mixed $newText String to replace in the defined position + * Or can be an array of values + * + * @return array|string + * If an array of values is passed for either of the arguments, then the returned result + * will also be an array with matching dimensions */ - public static function replace($oldText, $start, $chars, $newText): string + public static function replace($oldText, $start, $chars, $newText) { + if (is_array($oldText) || is_array($start) || is_array($chars) || is_array($newText)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $oldText, $start, $chars, $newText); + } + try { $start = Helpers::extractInt($start, 1, 0, true); $chars = Helpers::extractInt($chars, 0, 0, true); @@ -36,12 +51,24 @@ class Replace * SUBSTITUTE. * * @param mixed $text The text string value to modify + * Or can be an array of values * @param mixed $fromText The string value that we want to replace in $text + * Or can be an array of values * @param mixed $toText The string value that we want to replace with in $text + * Or can be an array of values * @param mixed $instance Integer instance Number for the occurrence of frmText to change + * Or can be an array of values + * + * @return array|string + * If an array of values is passed for either of the arguments, then the returned result + * will also be an array with matching dimensions */ - public static function substitute($text = '', $fromText = '', $toText = '', $instance = null): string + public static function substitute($text = '', $fromText = '', $toText = '', $instance = null) { + if (is_array($text) || is_array($fromText) || is_array($toText) || is_array($instance)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $text, $fromText, $toText, $instance); + } + try { $text = Helpers::extractString($text); $fromText = Helpers::extractString($fromText); @@ -71,7 +98,7 @@ class Replace } if ($pos !== false) { - return self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText); + return Functions::scalar(self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText)); } return $text; diff --git a/src/PhpSpreadsheet/Calculation/TextData/Text.php b/src/PhpSpreadsheet/Calculation/TextData/Text.php index 6f8253ea..7247a35b 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Text.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Text.php @@ -2,17 +2,29 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Functions; class Text { + use ArrayEnabled; + /** * LEN. * * @param mixed $value String Value + * Or can be an array of values + * + * @return array|int + * If an array of values is passed for the argument, then the returned result + * will also be an array with matching dimensions */ - public static function length($value = ''): int + public static function length($value = '') { + if (is_array($value)) { + return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value); + } + $value = Helpers::extractString($value); return mb_strlen($value ?? '', 'UTF-8'); @@ -24,10 +36,20 @@ class Text * Use EXACT to test text being entered into a document. * * @param mixed $value1 String Value + * Or can be an array of values * @param mixed $value2 String Value + * Or can be an array of values + * + * @return array|bool + * If an array of values is passed for either of the arguments, then the returned result + * will also be an array with matching dimensions */ - public static function exact($value1, $value2): bool + public static function exact($value1, $value2) { + if (is_array($value1) || is_array($value2)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $value1, $value2); + } + $value1 = Helpers::extractString($value1); $value2 = Helpers::extractString($value2); @@ -38,11 +60,18 @@ class Text * RETURNSTRING. * * @param mixed $testValue Value to check + * Or can be an array of values * - * @return null|string + * @return null|array|string + * If an array of values is passed for the argument, then the returned result + * will also be an array with matching dimensions */ public static function test($testValue = '') { + if (is_array($testValue)) { + return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $testValue); + } + $testValue = Functions::flattenSingleValue($testValue); if (is_string($testValue)) { diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/DollarTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/DollarTest.php index 272a5b40..e2b415dd 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/DollarTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/DollarTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class DollarTest extends AllSetupTeardown { /** @@ -33,4 +35,25 @@ class DollarTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/DOLLAR.php'; } + + /** + * @dataProvider providerDollarArray + */ + public function testDollarArray(array $expectedResult, string $argument1, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=DOLLAR({$argument1}, {$argument2})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerDollarArray(): array + { + return [ + 'row vector #1' => [[['-$123.32', '$123.46', '$12,345.68']], '{-123.321, 123.456, 12345.6789}', '2'], + 'column vector #1' => [[['-$123.32'], ['$123.46'], ['$12,345.68']], '{-123.321; 123.456; 12345.6789}', '2'], + 'matrix #1' => [[['-$123.46', '$12,345.68'], ['-$123.456', '$12,345.679']], '{-123.456, 12345.6789}', '{2; 3}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ExactTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ExactTest.php index fc0f481e..fed029eb 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ExactTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ExactTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class ExactTest extends AllSetupTeardown { /** @@ -33,4 +35,25 @@ class ExactTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/EXACT.php'; } + + /** + * @dataProvider providerExactArray + */ + public function testExactArray(array $expectedResult, string $argument1, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=EXACT({$argument1}, {$argument2})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerExactArray(): array + { + return [ + 'row vector #1' => [[[true, false, false]], '{"PHP", "php", "PHP8"}', '"PHP"'], + 'column vector #1' => [[[false], [true], [false]], '{"php"; "PHP"; "PHP8"}', '"PHP"'], + 'matrix #1' => [[[false, true], [false, true]], '{"TRUE", "FALSE"; TRUE, FALSE}', '"FALSE"'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/FixedTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/FixedTest.php index a6b6e197..6c857956 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/FixedTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/FixedTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class FixedTest extends AllSetupTeardown { /** @@ -39,4 +41,25 @@ class FixedTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/FIXED.php'; } + + /** + * @dataProvider providerFixedArray + */ + public function testFixedArray(array $expectedResult, string $argument1, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=FIXED({$argument1}, {$argument2})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerFixedArray(): array + { + return [ + 'row vector #1' => [[['-123.32', '123.46', '12,345.68']], '{-123.321, 123.456, 12345.6789}', '2'], + 'column vector #1' => [[['-123.32'], ['123.46'], ['12,345.68']], '{-123.321; 123.456; 12345.6789}', '2'], + 'matrix #1' => [[['-123.46', '12,345.68'], ['-123.456', '12,345.679']], '{-123.456, 12345.6789}', '{2; 3}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LenTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LenTest.php index b2ce124e..68f804d8 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LenTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LenTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class LenTest extends AllSetupTeardown { /** @@ -28,4 +30,25 @@ class LenTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/LEN.php'; } + + /** + * @dataProvider providerLenArray + */ + public function testLenArray(array $expectedResult, string $array): void + { + $calculation = Calculation::getInstance(); + + $formula = "=LEN({$array})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerLenArray(): array + { + return [ + 'row vector' => [[[3, 11, 14]], '{"PHP", "Hello World", "PhpSpreadsheet"}'], + 'column vector' => [[[3], [11], [14]], '{"PHP"; "Hello World"; "PhpSpreadsheet"}'], + 'matrix' => [[[3, 9], [11, 14]], '{"PHP", "ElePHPant"; "Hello World", "PhpSpreadsheet"}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/NumberValueTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/NumberValueTest.php index 86d8ca8b..a100695f 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/NumberValueTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/NumberValueTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class NumberValueTest extends AllSetupTeardown { /** @@ -39,4 +41,24 @@ class NumberValueTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/NUMBERVALUE.php'; } + + /** + * @dataProvider providerNumberValueArray + */ + public function testNumberValueArray(array $expectedResult, string $argument1, string $argument2, string $argument3): void + { + $calculation = Calculation::getInstance(); + + $formula = "=NumberValue({$argument1}, {$argument2}, {$argument3})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerNumberValueArray(): array + { + return [ + 'row vector #1' => [[[-123.321, 123.456, 12345.6789]], '{"-123,321", "123,456", "12 345,6789"}', '","', '" "'], + 'column vector #1' => [[[-123.321], [123.456], [12345.6789]], '{"-123,321"; "123,456"; "12 345,6789"}', '","', '" "'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ReplaceTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ReplaceTest.php index 0e120864..c64c4fe1 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ReplaceTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ReplaceTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class ReplaceTest extends AllSetupTeardown { /** @@ -46,4 +48,28 @@ class ReplaceTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/REPLACE.php'; } + + /** + * @dataProvider providerReplaceArray + */ + public function testReplaceArray( + array $expectedResult, + string $oldText, + string $start, + string $chars, + string $newText + ): void { + $calculation = Calculation::getInstance(); + + $formula = "=REPLACE({$oldText}, {$start}, {$chars}, {$newText})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerReplaceArray(): array + { + return [ + 'row vector' => [[['Elephpant', 'ElePHPant']], '"Elephant"', '4', '2', '{"php", "PHP"}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/SubstituteTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/SubstituteTest.php index bbb05591..dfb1e952 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/SubstituteTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/SubstituteTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class SubstituteTest extends AllSetupTeardown { /** @@ -46,4 +48,23 @@ class SubstituteTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/SUBSTITUTE.php'; } + + /** + * @dataProvider providerSubstituteArray + */ + public function testSubstituteArray(array $expectedResult, string $oldText, string $fromText, string $toText): void + { + $calculation = Calculation::getInstance(); + + $formula = "=SUBSTITUTE({$oldText}, {$fromText}, {$toText})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerSubstituteArray(): array + { + return [ + 'row vector' => [[['ElePHPant', 'EleFFant']], '"Elephant"', '"ph"', '{"PHP", "FF"}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TTest.php index 9ca47f91..e8111a2d 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\TextData; use PHPUnit\Framework\TestCase; @@ -23,4 +24,25 @@ class TTest extends TestCase { return require 'tests/data/Calculation/TextData/T.php'; } + + /** + * @dataProvider providerTArray + */ + public function testTArray(array $expectedResult, string $argument): void + { + $calculation = Calculation::getInstance(); + + $formula = "=T({$argument})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerTArray(): array + { + return [ + 'row vector #1' => [[['PHP', null, 'PHP8']], '{"PHP", 99, "PHP8"}'], + 'column vector #1' => [[[null], ['PHP'], [null]], '{12; "PHP"; 1.2}'], + 'matrix #1' => [[['TRUE', 'FALSE'], [null, null]], '{"TRUE", "FALSE"; TRUE, FALSE}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TextTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TextTest.php index 3ede2ffe..38ad410f 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TextTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TextTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class TextTest extends AllSetupTeardown { /** @@ -33,4 +35,31 @@ class TextTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/TEXT.php'; } + + /** + * @dataProvider providerTextArray + */ + public function testTextArray(array $expectedResult, string $argument1, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=TEXT({$argument1}, {$argument2})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerTextArray(): array + { + return [ + 'row vector' => [[['123.75%', '1 19/80']], '1.2375', '{"0.00%", "0 ??/???"}'], + 'matrix vector' => [ + [ + ['$ -1,234.57', '(1,234.57)'], + ['$ 9,876.54', '9,876.54'], + ], + '{-1234.5678; 9876.5432}', + '{"$ #,##0.00", "#,##0.00;(#,##0.00)"}', + ], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ValueTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ValueTest.php index fbef254b..cc7b3623 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ValueTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ValueTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; class ValueTest extends AllSetupTeardown @@ -65,4 +66,23 @@ class ValueTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/VALUE.php'; } + + /** + * @dataProvider providerValueArray + */ + public function testValueArray(array $expectedResult, string $argument): void + { + $calculation = Calculation::getInstance(); + + $formula = "=VALUE({$argument})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerValueArray(): array + { + return [ + 'row vector' => [[[44604, -1234.567]], '{"12-Feb-2022", "$ -1,234.567"}'], + ]; + } }