diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 61167005..46ee16d0 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -927,7 +927,12 @@ parameters: - message: "#^Parameter \\#1 \\$year of static method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Helpers\\:\\:daysPerYear\\(\\) expects int\\|string, array\\|int\\|string given\\.$#" - count: 3 + count: 1 + path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php + + - + message: "#^Parameter \\#1 \\$year of static method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Helpers\\:\\:daysPerYear\\(\\) expects int\\|string, mixed given\\.$#" + count: 2 path: src/PhpSpreadsheet/Calculation/Financial/Coupons.php - @@ -976,12 +981,12 @@ parameters: path: src/PhpSpreadsheet/Calculation/Financial/Dollar.php - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\AccruedInterest\\:\\:atMaturity\\(\\) should return float\\|string but returns array\\|string\\.$#" + message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\AccruedInterest\\:\\:atMaturity\\(\\) should return float\\|string but returns mixed\\.$#" count: 1 path: src/PhpSpreadsheet/Calculation/Financial/Securities/AccruedInterest.php - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\AccruedInterest\\:\\:periodic\\(\\) should return float\\|string but returns array\\|string\\.$#" + message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\AccruedInterest\\:\\:periodic\\(\\) should return float\\|string but returns mixed\\.$#" count: 2 path: src/PhpSpreadsheet/Calculation/Financial/Securities/AccruedInterest.php @@ -991,12 +996,12 @@ parameters: path: src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Price\\:\\:priceAtMaturity\\(\\) should return float\\|string but returns array\\|string\\.$#" - count: 3 + message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Price\\:\\:priceAtMaturity\\(\\) should return float\\|string but returns mixed\\.$#" + count: 4 path: src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Price\\:\\:priceDiscounted\\(\\) should return float\\|string but returns array\\|string\\.$#" + message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Price\\:\\:priceDiscounted\\(\\) should return float\\|string but returns mixed\\.$#" count: 1 path: src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php @@ -1011,22 +1016,22 @@ parameters: path: src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Rates\\:\\:discount\\(\\) should return float\\|string but returns array\\|string\\.$#" + message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Rates\\:\\:discount\\(\\) should return float\\|string but returns mixed\\.$#" count: 1 path: src/PhpSpreadsheet/Calculation/Financial/Securities/Rates.php - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Rates\\:\\:interest\\(\\) should return float\\|string but returns array\\|string\\.$#" + message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Rates\\:\\:interest\\(\\) should return float\\|string but returns mixed\\.$#" count: 1 path: src/PhpSpreadsheet/Calculation/Financial/Securities/Rates.php - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Yields\\:\\:yieldAtMaturity\\(\\) should return float\\|string but returns array\\|string\\.$#" + message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Yields\\:\\:yieldAtMaturity\\(\\) should return float\\|string but returns mixed\\.$#" count: 3 path: src/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Yields\\:\\:yieldDiscounted\\(\\) should return float\\|string but returns array\\|string\\.$#" + message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Securities\\\\Yields\\:\\:yieldDiscounted\\(\\) should return float\\|string but returns mixed\\.$#" count: 1 path: src/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php @@ -1036,7 +1041,7 @@ parameters: path: src/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php - - message: "#^Parameter \\#1 \\$year of static method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Helpers\\:\\:daysPerYear\\(\\) expects int\\|string, array\\|int\\|string given\\.$#" + message: "#^Parameter \\#1 \\$year of static method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Financial\\\\Helpers\\:\\:daysPerYear\\(\\) expects int\\|string, mixed given\\.$#" count: 3 path: src/PhpSpreadsheet/Calculation/Financial/TreasuryBill.php diff --git a/src/PhpSpreadsheet/Calculation/Financial.php b/src/PhpSpreadsheet/Calculation/Financial.php index 9d933b4a..4215a516 100644 --- a/src/PhpSpreadsheet/Calculation/Financial.php +++ b/src/PhpSpreadsheet/Calculation/Financial.php @@ -587,10 +587,10 @@ class Financial * @see Financial\Dollar::decimal() * Use the decimal() method in the Financial\Dollar class instead * - * @param float $fractional_dollar Fractional Dollar - * @param int $fraction Fraction + * @param array|float $fractional_dollar Fractional Dollar + * @param array|int $fraction Fraction * - * @return float|string + * @return array|float|string */ public static function DOLLARDE($fractional_dollar = null, $fraction = 0) { @@ -612,10 +612,10 @@ class Financial * @see Financial\Dollar::fractional() * Use the fractional() method in the Financial\Dollar class instead * - * @param float $decimal_dollar Decimal Dollar - * @param int $fraction Fraction + * @param array|float $decimal_dollar Decimal Dollar + * @param array|int $fraction Fraction * - * @return float|string + * @return array|float|string */ public static function DOLLARFR($decimal_dollar = null, $fraction = 0) { diff --git a/src/PhpSpreadsheet/Calculation/Financial/Amortization.php b/src/PhpSpreadsheet/Calculation/Financial/Amortization.php index ba7fb521..b16cc23f 100644 --- a/src/PhpSpreadsheet/Calculation/Financial/Amortization.php +++ b/src/PhpSpreadsheet/Calculation/Financial/Amortization.php @@ -168,7 +168,7 @@ class Amortization if ( ($basis == FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL) && - ($yearFrac < 1) && (DateTimeExcel\Helpers::isLeapYear($purchasedYear)) + ($yearFrac < 1) && (Functions::scalar(DateTimeExcel\Helpers::isLeapYear($purchasedYear))) ) { $yearFrac *= 365 / 366; } diff --git a/src/PhpSpreadsheet/Calculation/Financial/Coupons.php b/src/PhpSpreadsheet/Calculation/Financial/Coupons.php index e97a7ea8..ad487def 100644 --- a/src/PhpSpreadsheet/Calculation/Financial/Coupons.php +++ b/src/PhpSpreadsheet/Calculation/Financial/Coupons.php @@ -64,7 +64,7 @@ class Coupons return $e->getMessage(); } - $daysPerYear = Helpers::daysPerYear(DateTimeExcel\DateParts::year($settlement), $basis); + $daysPerYear = Helpers::daysPerYear(Functions::scalar(DateTimeExcel\DateParts::year($settlement)), $basis); if (is_string($daysPerYear)) { return Functions::VALUE(); } @@ -134,7 +134,7 @@ class Coupons case FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL: // Actual/actual if ($frequency == FinancialConstants::FREQUENCY_ANNUAL) { - $daysPerYear = (int) Helpers::daysPerYear(DateTimeExcel\DateParts::year($settlement), $basis); + $daysPerYear = (int) Helpers::daysPerYear(Functions::scalar(DateTimeExcel\DateParts::year($settlement)), $basis); return $daysPerYear / $frequency; } diff --git a/src/PhpSpreadsheet/Calculation/Financial/Dollar.php b/src/PhpSpreadsheet/Calculation/Financial/Dollar.php index 7bebb391..be000796 100644 --- a/src/PhpSpreadsheet/Calculation/Financial/Dollar.php +++ b/src/PhpSpreadsheet/Calculation/Financial/Dollar.php @@ -2,21 +2,25 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\Financial; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\TextData\Format; class Dollar { + use ArrayEnabled; + /** * DOLLAR. * * This function converts a number to text using currency format, with the decimals rounded to the specified place. * The format used is $#,##0.00_);($#,##0.00).. * - * @param mixed $number The value to format + * @param mixed $number The value to format, or can be an array of numbers * @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 */ public static function format($number, $precision = 2): string { @@ -34,12 +38,18 @@ class Dollar * DOLLARDE(fractional_dollar,fraction) * * @param mixed $fractionalDollar Fractional Dollar + * Or can be an array of values * @param mixed $fraction Fraction + * Or can be an array of values * - * @return float|string + * @return array|float|string */ public static function decimal($fractionalDollar = null, $fraction = 0) { + if (is_array($fractionalDollar) || is_array($fraction)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $fractionalDollar, $fraction); + } + $fractionalDollar = Functions::flattenSingleValue($fractionalDollar); $fraction = (int) Functions::flattenSingleValue($fraction); @@ -51,8 +61,8 @@ class Dollar return Functions::DIV0(); } - $dollars = floor($fractionalDollar); - $cents = fmod($fractionalDollar, 1); + $dollars = ($fractionalDollar < 0) ? ceil($fractionalDollar): floor($fractionalDollar); + $cents = fmod($fractionalDollar, 1.0); $cents /= $fraction; $cents *= 10 ** ceil(log10($fraction)); @@ -70,12 +80,18 @@ class Dollar * DOLLARFR(decimal_dollar,fraction) * * @param mixed $decimalDollar Decimal Dollar + * Or can be an array of values * @param mixed $fraction Fraction + * Or can be an array of values * - * @return float|string + * @return array|float|string */ public static function fractional($decimalDollar = null, $fraction = 0) { + if (is_array($decimalDollar) || is_array($fraction)) { + return self::evaluateArrayArguments([self::class, __FUNCTION__], $decimalDollar, $fraction); + } + $decimalDollar = Functions::flattenSingleValue($decimalDollar); $fraction = (int) Functions::flattenSingleValue($fraction); diff --git a/src/PhpSpreadsheet/Calculation/Financial/Securities/AccruedInterest.php b/src/PhpSpreadsheet/Calculation/Financial/Securities/AccruedInterest.php index e167429b..95996b4e 100644 --- a/src/PhpSpreadsheet/Calculation/Financial/Securities/AccruedInterest.php +++ b/src/PhpSpreadsheet/Calculation/Financial/Securities/AccruedInterest.php @@ -78,12 +78,12 @@ class AccruedInterest return $e->getMessage(); } - $daysBetweenIssueAndSettlement = YearFrac::fraction($issue, $settlement, $basis); + $daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis)); if (!is_numeric($daysBetweenIssueAndSettlement)) { // return date error return $daysBetweenIssueAndSettlement; } - $daysBetweenFirstInterestAndSettlement = YearFrac::fraction($firstInterest, $settlement, $basis); + $daysBetweenFirstInterestAndSettlement = Functions::scalar(YearFrac::fraction($firstInterest, $settlement, $basis)); if (!is_numeric($daysBetweenFirstInterestAndSettlement)) { // return date error return $daysBetweenFirstInterestAndSettlement; @@ -140,7 +140,7 @@ class AccruedInterest return $e->getMessage(); } - $daysBetweenIssueAndSettlement = YearFrac::fraction($issue, $settlement, $basis); + $daysBetweenIssueAndSettlement = Functions::scalar(YearFrac::fraction($issue, $settlement, $basis)); if (!is_numeric($daysBetweenIssueAndSettlement)) { // return date error return $daysBetweenIssueAndSettlement; diff --git a/src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php b/src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php index 7d8d5a32..cb893707 100644 --- a/src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php +++ b/src/PhpSpreadsheet/Calculation/Financial/Securities/Price.php @@ -134,7 +134,7 @@ class Price return $e->getMessage(); } - $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis); + $daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); if (!is_numeric($daysBetweenSettlementAndMaturity)) { // return date error return $daysBetweenSettlementAndMaturity; @@ -194,23 +194,23 @@ class Price return $e->getMessage(); } - $daysPerYear = Helpers::daysPerYear(DateTimeExcel\DateParts::year($settlement), $basis); + $daysPerYear = Functions::scalar(Helpers::daysPerYear(DateTimeExcel\DateParts::year($settlement), $basis)); if (!is_numeric($daysPerYear)) { return $daysPerYear; } - $daysBetweenIssueAndSettlement = DateTimeExcel\YearFrac::fraction($issue, $settlement, $basis); + $daysBetweenIssueAndSettlement = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $settlement, $basis)); if (!is_numeric($daysBetweenIssueAndSettlement)) { // return date error return $daysBetweenIssueAndSettlement; } $daysBetweenIssueAndSettlement *= $daysPerYear; - $daysBetweenIssueAndMaturity = DateTimeExcel\YearFrac::fraction($issue, $maturity, $basis); + $daysBetweenIssueAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $maturity, $basis)); if (!is_numeric($daysBetweenIssueAndMaturity)) { // return date error return $daysBetweenIssueAndMaturity; } $daysBetweenIssueAndMaturity *= $daysPerYear; - $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis); + $daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); if (!is_numeric($daysBetweenSettlementAndMaturity)) { // return date error return $daysBetweenSettlementAndMaturity; diff --git a/src/PhpSpreadsheet/Calculation/Financial/Securities/Rates.php b/src/PhpSpreadsheet/Calculation/Financial/Securities/Rates.php index c5c5211b..84218a96 100644 --- a/src/PhpSpreadsheet/Calculation/Financial/Securities/Rates.php +++ b/src/PhpSpreadsheet/Calculation/Financial/Securities/Rates.php @@ -63,7 +63,7 @@ class Rates return Functions::NAN(); } - $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis); + $daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); if (!is_numeric($daysBetweenSettlementAndMaturity)) { // return date error return $daysBetweenSettlementAndMaturity; @@ -126,7 +126,7 @@ class Rates return Functions::NAN(); } - $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis); + $daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); if (!is_numeric($daysBetweenSettlementAndMaturity)) { // return date error return $daysBetweenSettlementAndMaturity; diff --git a/src/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php b/src/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php index aa626935..bb2e8ae2 100644 --- a/src/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php +++ b/src/PhpSpreadsheet/Calculation/Financial/Securities/Yields.php @@ -61,7 +61,7 @@ class Yields if (!is_numeric($daysPerYear)) { return $daysPerYear; } - $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis); + $daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); if (!is_numeric($daysBetweenSettlementAndMaturity)) { // return date error return $daysBetweenSettlementAndMaturity; @@ -126,19 +126,19 @@ class Yields if (!is_numeric($daysPerYear)) { return $daysPerYear; } - $daysBetweenIssueAndSettlement = DateTimeExcel\YearFrac::fraction($issue, $settlement, $basis); + $daysBetweenIssueAndSettlement = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $settlement, $basis)); if (!is_numeric($daysBetweenIssueAndSettlement)) { // return date error return $daysBetweenIssueAndSettlement; } $daysBetweenIssueAndSettlement *= $daysPerYear; - $daysBetweenIssueAndMaturity = DateTimeExcel\YearFrac::fraction($issue, $maturity, $basis); + $daysBetweenIssueAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($issue, $maturity, $basis)); if (!is_numeric($daysBetweenIssueAndMaturity)) { // return date error return $daysBetweenIssueAndMaturity; } $daysBetweenIssueAndMaturity *= $daysPerYear; - $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis); + $daysBetweenSettlementAndMaturity = Functions::scalar(DateTimeExcel\YearFrac::fraction($settlement, $maturity, $basis)); if (!is_numeric($daysBetweenSettlementAndMaturity)) { // return date error return $daysBetweenSettlementAndMaturity; diff --git a/src/PhpSpreadsheet/Calculation/Financial/TreasuryBill.php b/src/PhpSpreadsheet/Calculation/Financial/TreasuryBill.php index c60af0b0..b2c84627 100644 --- a/src/PhpSpreadsheet/Calculation/Financial/TreasuryBill.php +++ b/src/PhpSpreadsheet/Calculation/Financial/TreasuryBill.php @@ -43,7 +43,7 @@ class TreasuryBill $daysBetweenSettlementAndMaturity = $maturity - $settlement; $daysPerYear = Helpers::daysPerYear( - DateTimeExcel\DateParts::year($maturity), + Functions::scalar(DateTimeExcel\DateParts::year($maturity)), FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL ); @@ -88,7 +88,7 @@ class TreasuryBill $daysBetweenSettlementAndMaturity = $maturity - $settlement; $daysPerYear = Helpers::daysPerYear( - DateTimeExcel\DateParts::year($maturity), + Functions::scalar(DateTimeExcel\DateParts::year($maturity)), FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL ); @@ -134,7 +134,7 @@ class TreasuryBill $daysBetweenSettlementAndMaturity = $maturity - $settlement; $daysPerYear = Helpers::daysPerYear( - DateTimeExcel\DateParts::year($maturity), + Functions::scalar(DateTimeExcel\DateParts::year($maturity)), FinancialConstants::BASIS_DAYS_PER_YEAR_ACTUAL ); diff --git a/src/PhpSpreadsheet/Calculation/MathTrig/Sqrt.php b/src/PhpSpreadsheet/Calculation/MathTrig/Sqrt.php index b81c54c9..bb9f15fd 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig/Sqrt.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig/Sqrt.php @@ -16,7 +16,7 @@ class Sqrt * * @param mixed $number Should be numeric, or can be an array of numbers * - * @return array|float|string square roor + * @return array|float|string square root * If an array of numbers is passed as the argument, then the returned result will also be an array * with the same dimensions */ diff --git a/src/PhpSpreadsheet/Calculation/TextData/CaseConvert.php b/src/PhpSpreadsheet/Calculation/TextData/CaseConvert.php index 664cc2d8..0e9cbf44 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/CaseConvert.php +++ b/src/PhpSpreadsheet/Calculation/TextData/CaseConvert.php @@ -2,20 +2,32 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; class CaseConvert { + use ArrayEnabled; + /** * LOWERCASE. * * Converts a string value to upper case. * * @param mixed $mixedCaseValue The string value to convert to lower case + * Or can be an array of values + * + * @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 lower($mixedCaseValue): string + public static function lower($mixedCaseValue) { + if (is_array($mixedCaseValue)) { + return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $mixedCaseValue); + } + $mixedCaseValue = Functions::flattenSingleValue($mixedCaseValue); $mixedCaseValue = Helpers::extractString($mixedCaseValue); @@ -28,9 +40,18 @@ class CaseConvert * Converts a string value to upper case. * * @param mixed $mixedCaseValue The string value to convert to upper case + * Or can be an array of values + * + * @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 upper($mixedCaseValue): string + public static function upper($mixedCaseValue) { + if (is_array($mixedCaseValue)) { + return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $mixedCaseValue); + } + $mixedCaseValue = Functions::flattenSingleValue($mixedCaseValue); $mixedCaseValue = Helpers::extractString($mixedCaseValue); @@ -43,9 +64,18 @@ class CaseConvert * Converts a string value to proper or title case. * * @param mixed $mixedCaseValue The string value to convert to title case + * Or can be an array of values + * + * @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 proper($mixedCaseValue): string + public static function proper($mixedCaseValue) { + if (is_array($mixedCaseValue)) { + return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $mixedCaseValue); + } + $mixedCaseValue = Functions::flattenSingleValue($mixedCaseValue); $mixedCaseValue = Helpers::extractString($mixedCaseValue); diff --git a/src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php b/src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php index 0aca57e6..714d789e 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php +++ b/src/PhpSpreadsheet/Calculation/TextData/CharacterConvert.php @@ -2,17 +2,29 @@ namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; use PhpOffice\PhpSpreadsheet\Calculation\Functions; class CharacterConvert { + use ArrayEnabled; + /** * CHAR. * * @param mixed $character Integer Value to convert to its character representation + * Or can be an array of values + * + * @return array|string The character 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 character($character): string + public static function character($character) { + if (is_array($character)) { + return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $character); + } + $character = Helpers::validateInt($character); $min = Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE ? 0 : 1; if ($character < $min || $character > 255) { @@ -27,11 +39,18 @@ class CharacterConvert * CODE. * * @param mixed $characters String character to convert to its ASCII value + * Or can be an array of values * - * @return int|string A string if arguments are invalid + * @return array|int|string A string if arguments are invalid + * 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 code($characters) { + if (is_array($characters)) { + return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $characters); + } + $characters = Helpers::extractString($characters); if ($characters === '') { return Functions::VALUE(); diff --git a/src/PhpSpreadsheet/Calculation/TextData/Format.php b/src/PhpSpreadsheet/Calculation/TextData/Format.php index 3286de0c..69b80bde 100644 --- a/src/PhpSpreadsheet/Calculation/TextData/Format.php +++ b/src/PhpSpreadsheet/Calculation/TextData/Format.php @@ -146,14 +146,14 @@ class Format Functions::setReturnDateType(Functions::RETURNDATE_EXCEL); if (strpos($value, ':') !== false) { - $timeValue = DateTimeExcel\TimeValue::fromString($value); + $timeValue = Functions::scalar(DateTimeExcel\TimeValue::fromString($value)); if ($timeValue !== Functions::VALUE()) { Functions::setReturnDateType($dateSetting); return $timeValue; } } - $dateValue = DateTimeExcel\DateValue::fromString($value); + $dateValue = Functions::scalar(DateTimeExcel\DateValue::fromString($value)); if ($dateValue !== Functions::VALUE()) { Functions::setReturnDateType($dateSetting); diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/DollarDeTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/DollarDeTest.php index 7a4ed042..bfb93804 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/DollarDeTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/Financial/DollarDeTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\Financial; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Calculation\Financial; use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PHPUnit\Framework\TestCase; @@ -28,4 +29,52 @@ class DollarDeTest extends TestCase { return require 'tests/data/Calculation/Financial/DOLLARDE.php'; } + + /** + * @dataProvider providerDollarDeArray + */ + public function testDollarDeArray(array $expectedResult, string $argument1, string $argument2): void + { + $calculation = Calculation::getInstance(); + + $formula = "=DollarDe({$argument1},{$argument2})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-12); + } + + public function providerDollarDeArray(): array + { + return [ + 'first argument row vector' => [ + [[1.125, 2.0625, -12.625, 3.5]], + '{1.02, 2.01, -12.1, 1.4}', + '16', + ], + 'first argument column vector' => [ + [[1.0625], [2.03125], [-12.3125], [2.25]], + '{1.02; 2.01; -12.1; 1.4}', + '32', + ], + 'first argument matrix' => [ + [[1.05, 2.25], [-12.5, 2.0]], + '{1.02, 2.1; -12.2, 1.4}', + '4', + ], + 'second argument row vector' => [ + [[4.25, 3.625, 6.125, 4.5625]], + '3.5', + '{4, 8, 16, 32}', + ], + 'second argument column vector' => [ + [[5.5], [4.25], [3.625], [6.125]], + '3.5', + '{2; 4; 8; 16}', + ], + 'second argument matrix' => [ + [[-4.875, -3.9375], [-9.25, -7.6875]], + '-3.75', + '{4, 8; 12, 16}', + ], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CharTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CharTest.php index 66d01cb0..5cdca685 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CharTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CharTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class CharTest extends AllSetupTeardown { /** @@ -28,4 +30,25 @@ class CharTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/CHAR.php'; } + + /** + * @dataProvider providerCharArray + */ + public function testCharArray(array $expectedResult, string $array): void + { + $calculation = Calculation::getInstance(); + + $formula = "=CHAR({$array})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerCharArray(): array + { + return [ + 'row vector' => [[['P', 'H', 'P']], '{80, 72, 80}'], + 'column vector' => [[['P'], ['H'], ['P']], '{80; 72; 80}'], + 'matrix' => [[['Y', 'o'], ['l', 'o']], '{89, 111; 108, 111}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CodeTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CodeTest.php index 52baf1a1..d6867f63 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CodeTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/CodeTest.php @@ -2,6 +2,8 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; + class CodeTest extends AllSetupTeardown { /** @@ -28,4 +30,25 @@ class CodeTest extends AllSetupTeardown { return require 'tests/data/Calculation/TextData/CODE.php'; } + + /** + * @dataProvider providerCodeArray + */ + public function testCodeArray(array $expectedResult, string $array): void + { + $calculation = Calculation::getInstance(); + + $formula = "=CODE({$array})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerCodeArray(): array + { + return [ + 'row vector' => [[[80, 72, 80]], '{"P", "H", "P"}'], + 'column vector' => [[[80], [72], [80]], '{"P"; "H"; "P"}'], + 'matrix' => [[[89, 111], [108, 111]], '{"Y", "o"; "l", "o"}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LowerTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LowerTest.php index 4639624a..3b191843 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LowerTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/LowerTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Settings; class LowerTest extends AllSetupTeardown @@ -64,4 +65,25 @@ class LowerTest extends AllSetupTeardown ['ложь', 'bg', false], ]; } + + /** + * @dataProvider providerLowerArray + */ + public function testLowerArray(array $expectedResult, string $array): void + { + $calculation = Calculation::getInstance(); + + $formula = "=LOWER({$array})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerLowerArray(): array + { + return [ + 'row vector' => [[["let's", 'all change', 'case']], '{"lEt\'S", "aLl chAngE", "cAsE"}'], + 'column vector' => [[["let's"], ['all change'], ['case']], '{"lEt\'S"; "aLl chAngE"; "cAsE"}'], + 'matrix' => [[['build all', 'your workbooks'], ['with', 'phpspreadsheet']], '{"bUIld aLL", "yOUr WOrkBOOks"; "wiTH", "PhpSpreadsheet"}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ProperTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ProperTest.php index 51968d83..d5e86c12 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ProperTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/ProperTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Settings; class ProperTest extends AllSetupTeardown @@ -64,4 +65,25 @@ class ProperTest extends AllSetupTeardown ['Ложь', 'bg', false], ]; } + + /** + * @dataProvider providerProperArray + */ + public function testProperArray(array $expectedResult, string $array): void + { + $calculation = Calculation::getInstance(); + + $formula = "=PROPER({$array})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerProperArray(): array + { + return [ + 'row vector' => [[["Let's", 'All Change', 'Case']], '{"lEt\'S", "aLl chAngE", "cAsE"}'], + 'column vector' => [[["Let's"], ['All Change'], ['Case']], '{"lEt\'S"; "aLl chAngE"; "cAsE"}'], + 'matrix' => [[['Build All', 'Your Workbooks'], ['With', 'Phpspreadsheet']], '{"bUIld aLL", "yOUr WOrkBOOks"; "wiTH", "PhpSpreadsheet"}'], + ]; + } } diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/UpperTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/UpperTest.php index 0147b5c1..da089ca7 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/UpperTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/TextData/UpperTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\TextData; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Settings; class UpperTest extends AllSetupTeardown @@ -64,4 +65,25 @@ class UpperTest extends AllSetupTeardown ['ЛОЖЬ', 'bg', false], ]; } + + /** + * @dataProvider providerUpperArray + */ + public function testUpperArray(array $expectedResult, string $array): void + { + $calculation = Calculation::getInstance(); + + $formula = "=UPPER({$array})"; + $result = $calculation->_calculateFormulaValue($formula); + self::assertEqualsWithDelta($expectedResult, $result, 1.0e-14); + } + + public function providerUpperArray(): array + { + return [ + 'row vector' => [[["LET'S", 'ALL CHANGE', 'CASE']], '{"lEt\'S", "aLl chAngE", "cAsE"}'], + 'column vector' => [[["LET'S"], ['ALL CHANGE'], ['CASE']], '{"lEt\'S"; "aLl chAngE"; "cAsE"}'], + 'matrix' => [[['BUILD ALL', 'YOUR WORKBOOKS'], ['WITH', 'PHPSPREADSHEET']], '{"bUIld aLL", "yOUr WOrkBOOks"; "wiTH", "PhpSpreadsheet"}'], + ]; + } }