diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 3ac485b9..4f4330e6 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1930,21 +1930,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Calculation/TextData.php - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\:\\:SEARCHINSENSITIVE\\(\\) should return string but returns int\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/TextData.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\:\\:SEARCHSENSITIVE\\(\\) should return string but returns int\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/TextData.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\TextData\\:\\:TRIMNONPRINTABLE\\(\\) should return string but returns string\\|null\\.$#" - count: 1 - path: src/PhpSpreadsheet/Calculation/TextData.php - - message: "#^Parameter \\#1 \\$glue of function implode expects string, mixed given\\.$#" count: 1 diff --git a/src/PhpSpreadsheet/Calculation/DateTimeExcel/NetworkDays.php b/src/PhpSpreadsheet/Calculation/DateTimeExcel/NetworkDays.php index 9a87af27..3b8942ba 100644 --- a/src/PhpSpreadsheet/Calculation/DateTimeExcel/NetworkDays.php +++ b/src/PhpSpreadsheet/Calculation/DateTimeExcel/NetworkDays.php @@ -30,6 +30,8 @@ class NetworkDays * @param mixed $dateArgs An array of dates (such as holidays) to exclude from the calculation * * @return array|int|string Interval between the dates + * If an array of values is passed for the $startDate or $endDate arguments, then the returned result + * will also be an array with matching dimensions */ public static function count($startDate, $endDate, ...$dateArgs) { diff --git a/src/PhpSpreadsheet/Calculation/DateTimeExcel/YearFrac.php b/src/PhpSpreadsheet/Calculation/DateTimeExcel/YearFrac.php index 6babf633..8dad16d8 100644 --- a/src/PhpSpreadsheet/Calculation/DateTimeExcel/YearFrac.php +++ b/src/PhpSpreadsheet/Calculation/DateTimeExcel/YearFrac.php @@ -26,7 +26,7 @@ class YearFrac * * @param mixed $startDate Excel date serial value (float), PHP date timestamp (integer), * PHP DateTime object, or a standard date string - * Or can be an array of methods + * Or can be an array of values * @param mixed $endDate Excel date serial value (float), PHP date timestamp (integer), * PHP DateTime object, or a standard date string * Or can be an array of methods diff --git a/src/PhpSpreadsheet/Calculation/TextData.php b/src/PhpSpreadsheet/Calculation/TextData.php index b74abbc0..c8b9e58a 100644 --- a/src/PhpSpreadsheet/Calculation/TextData.php +++ b/src/PhpSpreadsheet/Calculation/TextData.php @@ -34,7 +34,7 @@ class TextData * * @param mixed $stringValue Value to check * - * @return string + * @return null|array|string */ public static function TRIMNONPRINTABLE($stringValue = '') { @@ -50,7 +50,7 @@ class TextData * * @param mixed $stringValue Value to check * - * @return string + * @return array|string */ public static function TRIMSPACES($stringValue = '') { @@ -110,17 +110,17 @@ class TextData } /** - * SEARCHSENSITIVE. + * FIND. * * @Deprecated 1.18.0 * * @see Use the sensitive() method in the TextData\Search class instead * - * @param string $needle The string to look for - * @param string $haystack The string in which to look - * @param int $offset Offset within $haystack + * @param array|string $needle The string to look for + * @param array|string $haystack The string in which to look + * @param array|int $offset Offset within $haystack * - * @return string + * @return array|int|string */ public static function SEARCHSENSITIVE($needle, $haystack, $offset = 1) { @@ -128,17 +128,17 @@ class TextData } /** - * SEARCHINSENSITIVE. + * SEARCH. * * @Deprecated 1.18.0 * * @see Use the insensitive() method in the TextData\Search class instead * - * @param string $needle The string to look for - * @param string $haystack The string in which to look - * @param int $offset Offset within $haystack + * @param array|string $needle The string to look for + * @param array|string $haystack The string in which to look + * @param array|int $offset Offset within $haystack * - * @return string + * @return array|int|string */ public static function SEARCHINSENSITIVE($needle, $haystack, $offset = 1) { @@ -170,10 +170,10 @@ class TextData * * @see Use the left() method in the TextData\Extract class instead * - * @param string $value Value - * @param int $chars Number of characters + * @param array|string $value Value + * @param array|int $chars Number of characters * - * @return string + * @return array|string */ public static function LEFT($value = '', $chars = 1) { @@ -187,11 +187,11 @@ class TextData * * @see Use the mid() method in the TextData\Extract class instead * - * @param string $value Value - * @param int $start Start character - * @param int $chars Number of characters + * @param array|string $value Value + * @param array|int $start Start character + * @param array|int $chars Number of characters * - * @return string + * @return array|string */ public static function MID($value = '', $start = 1, $chars = null) { @@ -205,10 +205,10 @@ class TextData * * @see Use the right() method in the TextData\Extract class instead * - * @param string $value Value - * @param int $chars Number of characters + * @param array|string $value Value + * @param array|int $chars Number of characters * - * @return string + * @return array|string */ public static function RIGHT($value = '', $chars = 1) { @@ -420,7 +420,7 @@ class TextData * @param mixed $ignoreEmpty * @param mixed $args * - * @return string + * @return array|string */ public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args) { @@ -436,10 +436,10 @@ class TextData * * @see Use the builtinREPT() method in the TextData\Concatenate class instead * - * @param string $str Should be numeric + * @param array|string $str Should be numeric * @param mixed $number Should be int * - * @return string + * @return array|string */ public static function builtinREPT($str, $number) { diff --git a/src/PhpSpreadsheet/Calculation/TextData/Concatenate.php b/src/PhpSpreadsheet/Calculation/TextData/Concatenate.php index d53fc822..a7356f82 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Concatenate.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Concatenate.php @@ -2,10 +2,13 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Functions; class Concatenate { + use ArrayEnabled; + /** * CONCATENATE. * @@ -28,12 +31,28 @@ class Concatenate /** * TEXTJOIN. * - * @param mixed $delimiter - * @param mixed $ignoreEmpty - * @param mixed $args + * @param mixed $delimiter The delimter to use between the joined arguments + * Or can be an array of values + * @param mixed $ignoreEmpty true/false Flag indicating whether empty arguments should be skipped + * Or can be an array of values + * @param mixed $args The values to join + * + * @return array|string The joined string + * If an array of values is passed for the $delimiter or $ignoreEmpty arguments, then the returned result + * will also be an array with matching dimensions */ - public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args): string + public static function TEXTJOIN($delimiter, $ignoreEmpty, ...$args) { + if (is_array($delimiter) || is_array($ignoreEmpty)) { + return self::evaluateArrayArgumentsSubset( + [self::class, __FUNCTION__], + 2, + $delimiter, + $ignoreEmpty, + ...$args + ); + } + $delimiter = Functions::flattenSingleValue($delimiter); $ignoreEmpty = Functions::flattenSingleValue($ignoreEmpty); // Loop through arguments @@ -55,10 +74,20 @@ class Concatenate * Returns the result of builtin function round after validating args. * * @param mixed $stringValue The value to repeat + * Or can be an array of values * @param mixed $repeatCount The number of times the string value should be repeated + * Or can be an array of values + * + * @return array|string The repeated string + * If an array of values is passed for the $stringValue or $repeatCount arguments, then the returned result + * will also be an array with matching dimensions */ - public static function builtinREPT($stringValue, $repeatCount): string + public static function builtinREPT($stringValue, $repeatCount) { + if (is_array($stringValue) || is_array($repeatCount)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $stringValue, $repeatCount); + } + $repeatCount = Functions::flattenSingleValue($repeatCount); $stringValue = Helpers::extractString($stringValue); diff --git a/src/PhpSpreadsheet/Calculation/TextData/Extract.php b/src/PhpSpreadsheet/Calculation/TextData/Extract.php index 7f18e0c6..d29f80ca 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Extract.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Extract.php @@ -2,18 +2,31 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; class Extract { + use ArrayEnabled; + /** * LEFT. * * @param mixed $value String value from which to extract characters + * Or can be an array of values * @param mixed $chars The number of characters to extract (as an integer) + * Or can be an array of values + * + * @return array|string The joined string + * If an array of values is passed for the $value or $chars arguments, then the returned result + * will also be an array with matching dimensions */ - public static function left($value, $chars = 1): string + public static function left($value, $chars = 1) { + if (is_array($value) || is_array($chars)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $chars); + } + try { $value = Helpers::extractString($value); $chars = Helpers::extractInt($chars, 0, 1); @@ -28,11 +41,22 @@ class Extract * MID. * * @param mixed $value String value from which to extract characters + * Or can be an array of values * @param mixed $start Integer offset of the first character that we want to extract + * Or can be an array of values * @param mixed $chars The number of characters to extract (as an integer) + * Or can be an array of values + * + * @return array|string The joined string + * If an array of values is passed for the $value, $start or $chars arguments, then the returned result + * will also be an array with matching dimensions */ - public static function mid($value, $start, $chars): string + public static function mid($value, $start, $chars) { + if (is_array($value) || is_array($start) || is_array($chars)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $start, $chars); + } + try { $value = Helpers::extractString($value); $start = Helpers::extractInt($start, 1); @@ -48,10 +72,20 @@ class Extract * RIGHT. * * @param mixed $value String value from which to extract characters + * Or can be an array of values * @param mixed $chars The number of characters to extract (as an integer) + * Or can be an array of values + * + * @return array|string The joined string + * If an array of values is passed for the $value or $chars arguments, then the returned result + * will also be an array with matching dimensions */ - public static function right($value, $chars = 1): string + public static function right($value, $chars = 1) { + if (is_array($value) || is_array($chars)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $chars); + } + try { $value = Helpers::extractString($value); $chars = Helpers::extractInt($chars, 0, 1); diff --git a/src/PhpSpreadsheet/Calculation/TextData/Search.php b/src/PhpSpreadsheet/Calculation/TextData/Search.php index c9eed2e5..b6a72380 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Search.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Search.php @@ -2,23 +2,35 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; class Search { + use ArrayEnabled; + /** * FIND (case sensitive search). * * @param mixed $needle The string to look for + * Or can be an array of values * @param mixed $haystack The string in which to look + * Or can be an array of values * @param mixed $offset Integer offset within $haystack to start searching from + * Or can be an array of values * - * @return int|string + * @return array|int|string The offset where the first occurrence of needle was found in the haystack + * If an array of values is passed for the $value or $chars arguments, then the returned result + * will also be an array with matching dimensions */ public static function sensitive($needle, $haystack, $offset = 1) { + if (is_array($needle) || is_array($haystack) || is_array($offset)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $needle, $haystack, $offset); + } + try { $needle = Helpers::extractString($needle); $haystack = Helpers::extractString($haystack); @@ -45,13 +57,22 @@ class Search * SEARCH (case insensitive search). * * @param mixed $needle The string to look for + * Or can be an array of values * @param mixed $haystack The string in which to look + * Or can be an array of values * @param mixed $offset Integer offset within $haystack to start searching from + * Or can be an array of values * - * @return int|string + * @return array|int|string The offset where the first occurrence of needle was found in the haystack + * If an array of values is passed for the $value or $chars arguments, then the returned result + * will also be an array with matching dimensions */ public static function insensitive($needle, $haystack, $offset = 1) { + if (is_array($needle) || is_array($haystack) || is_array($offset)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $needle, $haystack, $offset); + } + try { $needle = Helpers::extractString($needle); $haystack = Helpers::extractString($haystack); diff --git a/src/PhpSpreadsheet/Calculation/TextData/Trim.php b/src/PhpSpreadsheet/Calculation/TextData/Trim.php index 22f4554b..e52c0095 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Trim.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Trim.php @@ -2,17 +2,28 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; + class Trim { + use ArrayEnabled; + /** * CLEAN. * * @param mixed $stringValue String Value to check + * Or can be an array of values * - * @return null|string + * @return null|array|string + * If an array of values is passed as the argument, then the returned result will also be an array + * with the same dimensions */ public static function nonPrintable($stringValue = '') { + if (is_array($stringValue)) { + return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $stringValue); + } + $stringValue = Helpers::extractString($stringValue); return preg_replace('/[\\x00-\\x1f]/', '', "$stringValue"); @@ -22,11 +33,18 @@ class Trim * TRIM. * * @param mixed $stringValue String Value to check + * Or can be an array of values * - * @return string + * @return array|string + * If an array of values is passed as the argument, then the returned result will also be an array + * with the same dimensions */ public static function spaces($stringValue = '') { + if (is_array($stringValue)) { + return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $stringValue); + } + $stringValue = Helpers::extractString($stringValue); return trim(preg_replace('/ +/', ' ', trim("$stringValue", ' ')) ?? '', ' '); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CleanTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CleanTest.php index 03939882..4297d580 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CleanTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CleanTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class CleanTest extends AllSetupTeardown { /** @@ -28,4 +30,25 @@ class CleanTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/CLEAN.php'; } + + /** + * @dataProvider providerCleanArray + */ + public function testCleanArray(array $expectedResult, string $array): void + { + $calculation = Calculation::getInstance(); + + $formula = "=CLEAN({$array})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerCleanArray(): array + { + return [ + 'row vector' => [[['PHP', 'MS Excel', 'Open/Libre Office']], '{"PHP", "MS Excel", "Open/Libre Office"}'], + 'column vector' => [[['PHP'], ['MS Excel'], ['Open/Libre Office']], '{"PHP"; "MS Excel"; "Open/Libre Office"}'], + 'matrix' => [[['PHP', 'MS Excel'], ['PhpSpreadsheet', 'Open/Libre Office']], '{"PHP", "MS Excel"; "PhpSpreadsheet", "Open/Libre Office"}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/FindTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/FindTest.php index 14e48b7a..27ed3762 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/FindTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/FindTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class FindTest extends AllSetupTeardown { /** @@ -39,4 +41,25 @@ class FindTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/FIND.php'; } + + /** + * @dataProvider providerFindArray + */ + public function testFindArray(array $expectedResult, string $argument1, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=FIND({$argument1}, {$argument2})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerFindArray(): array + { + return [ + 'row vector #1' => [[[3, 4, '#VALUE!']], '"l"', '{"Hello", "World", "PhpSpreadsheet"}'], + 'column vector #1' => [[[3], [4], ['#VALUE!']], '"l"', '{"Hello"; "World"; "PhpSpreadsheet"}'], + 'matrix #1' => [[[3, 4], ['#VALUE!', 5]], '"l"', '{"Hello", "World"; "PhpSpreadsheet", "Excel"}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LeftTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LeftTest.php index 37ea2f3d..7f709d64 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LeftTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LeftTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Settings; @@ -180,4 +181,26 @@ class LeftTest extends AllSetupTeardown ], ]; } + + /** + * @dataProvider providerLeftArray + */ + public function testLeftArray(array $expectedResult, string $argument1, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=LEFT({$argument1}, {$argument2})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerLeftArray(): array + { + return [ + 'row vector #1' => [[['Hel', 'Wor', 'Php']], '{"Hello", "World", "PhpSpreadsheet"}', '3'], + 'column vector #1' => [[['Hel'], ['Wor'], ['Php']], '{"Hello"; "World"; "PhpSpreadsheet"}', '3'], + 'matrix #1' => [[['Hel', 'Wor'], ['Php', 'Exc']], '{"Hello", "World"; "PhpSpreadsheet", "Excel"}', '3'], + 'column vector #2' => [[['Php'], ['PhpSp']], '"PhpSpreadsheet"', '{3; 5}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/MidTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/MidTest.php index a1eebf47..e4ff9343 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/MidTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/MidTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Settings; @@ -202,4 +203,24 @@ class MidTest extends AllSetupTeardown ], ]; } + + /** + * @dataProvider providerMidArray + */ + public function testMidArray(array $expectedResult, string $argument1, string $argument2, string $argument3): void + { + $calculation = Calculation::getInstance(); + + $formula = "=MID({$argument1}, {$argument2}, {$argument3})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerMidArray(): array + { + return [ + 'row vector #1' => [[['lo Wor', 'Spread']], '{"Hello World", "PhpSpreadsheet"}', '4', '6'], + 'column vector #1' => [[[' Wor'], ['read']], '{"Hello World"; "PhpSpreadsheet"}', '6', '4'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ReptTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ReptTest.php index ea86d1b6..a36324e1 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ReptTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ReptTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class ReptTest extends AllSetupTeardown { /** @@ -33,4 +35,26 @@ class ReptTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/REPT.php'; } + + /** + * @dataProvider providerReptArray + */ + public function testReptArray(array $expectedResult, string $argument1, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=REPT({$argument1}, {$argument2})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerReptArray(): array + { + return [ + 'row vector #1' => [[['PHPPHPPHP', 'HAHAHA', 'HOHOHO']], '{"PHP", "HA", "HO"}', '3'], + 'column vector #1' => [[['PHPPHPPHP'], ['HAHAHA'], ['HOHOHO']], '{"PHP"; "HA"; "HO"}', '3'], + 'matrix #1' => [[['PHPPHP', '❤️🐘💚❤️🐘💚'], ['HAHA', 'HOHO']], '{"PHP", "❤️🐘💚"; "HA", "HO"}', '2'], + 'row vector #2' => [[[' PHP PHP PHP ', ' PHP PHP ']], '" PHP "', '{3, 2}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/RightTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/RightTest.php index b9051d0d..d8de3e42 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/RightTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/RightTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Settings; @@ -180,4 +181,26 @@ class RightTest extends AllSetupTeardown ], ]; } + + /** + * @dataProvider providerRightArray + */ + public function testRightArray(array $expectedResult, string $argument1, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=RIGHT({$argument1}, {$argument2})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerRightArray(): array + { + return [ + 'row vector #1' => [[['llo', 'rld', 'eet']], '{"Hello", "World", "PhpSpreadsheet"}', '3'], + 'column vector #1' => [[['llo'], ['rld'], ['eet']], '{"Hello"; "World"; "PhpSpreadsheet"}', '3'], + 'matrix #1' => [[['llo', 'rld'], ['eet', 'cel']], '{"Hello", "World"; "PhpSpreadsheet", "Excel"}', '3'], + 'column vector #2' => [[['eet'], ['sheet']], '"PhpSpreadsheet"', '{3; 5}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/SearchTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/SearchTest.php index 5f1c526d..1cd5dccc 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/SearchTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/SearchTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class SearchTest extends AllSetupTeardown { /** @@ -39,4 +41,25 @@ class SearchTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/SEARCH.php'; } + + /** + * @dataProvider providerSearchArray + */ + public function testSearchArray(array $expectedResult, string $argument1, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=SEARCH({$argument1}, {$argument2})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerSearchArray(): array + { + return [ + 'row vector #1' => [[[3, 4, '#VALUE!']], '"L"', '{"Hello", "World", "PhpSpreadsheet"}'], + 'column vector #1' => [[[3], [4], ['#VALUE!']], '"L"', '{"Hello"; "World"; "PhpSpreadsheet"}'], + 'matrix #1' => [[[3, 4], ['#VALUE!', 5]], '"L"', '{"Hello", "World"; "PhpSpreadsheet", "Excel"}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TextJoinTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TextJoinTest.php index 60b21661..85834333 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TextJoinTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TextJoinTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class TextJoinTest extends AllSetupTeardown { /** @@ -32,4 +34,25 @@ class TextJoinTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/TEXTJOIN.php'; } + + /** + * @dataProvider providerTextjoinArray + */ + public function testTextjoinArray(array $expectedResult, string $delimiter, string $blanks, string $texts): void + { + $calculation = Calculation::getInstance(); + + $formula = "=TEXTJOIN({$delimiter}, {$blanks}, {$texts})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerTextjoinArray(): array + { + return [ + 'row vector #1' => [[['AB,CD,EF', 'AB;CD;EF']], '{",", ";"}', 'FALSE', '"AB", "CD", "EF"'], + 'column vector #1' => [[['AB--CD--EF'], ['AB|CD|EF']], '{"--"; "|"}', 'FALSE', '"AB", "CD", "EF"'], + 'matrix #1' => [[['AB,CD,EF', 'AB;CD;EF'], ['AB-CD-EF', 'AB|CD|EF']], '{",", ";"; "-", "|"}', 'FALSE', '"AB", "CD", "EF"'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TrimTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TrimTest.php index 133e850f..c9fe4d81 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TrimTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/TrimTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class TrimTest extends AllSetupTeardown { /** @@ -28,4 +30,25 @@ class TrimTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/TRIM.php'; } + + /** + * @dataProvider providerTrimArray + */ + public function testTrimArray(array $expectedResult, string $array): void + { + $calculation = Calculation::getInstance(); + + $formula = "=TRIM({$array})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerTrimArray(): array + { + return [ + 'row vector' => [[['PHP', 'MS Excel', 'Open/Libre Office']], '{" PHP ", " MS Excel ", " Open/Libre Office "}'], + 'column vector' => [[['PHP'], ['MS Excel'], ['Open/Libre Office']], '{" PHP "; " MS Excel "; " Open/Libre Office "}'], + 'matrix' => [[['PHP', 'MS Excel'], ['PhpSpreadsheet', 'Open/Libre Office']], '{" PHP ", " MS Excel "; " PhpSpreadsheet ", " Open/Libre Office "}'], + ]; + } }