-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3164 from PHPOffice/CalcEngine-Refactor_Formatted…
…-Numbers Refactoring for checks on strings containing formatted numeric values
- Loading branch information
Showing
8 changed files
with
311 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace PhpOffice\PhpSpreadsheet\Calculation\Engine; | ||
|
||
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; | ||
|
||
class FormattedNumber | ||
{ | ||
/** Constants */ | ||
/** Regular Expressions */ | ||
// Fraction | ||
private const STRING_REGEXP_FRACTION = '~^\s*(-?)((\d*)\s+)?(\d+\/\d+)\s*$~'; | ||
|
||
private const STRING_REGEXP_PERCENT = '~^(?:(?: *(?<PrefixedSign>[-+])? *\% *(?<PrefixedSign2>[-+])? *(?<PrefixedValue>[0-9]+\.?[0-9*]*(?:E[-+]?[0-9]*)?) *)|(?: *(?<PostfixedSign>[-+])? *(?<PostfixedValue>[0-9]+\.?[0-9]*(?:E[-+]?[0-9]*)?) *\% *))$~i'; | ||
|
||
private const STRING_CONVERSION_LIST = [ | ||
[self::class, 'convertToNumberIfNumeric'], | ||
[self::class, 'convertToNumberIfFraction'], | ||
[self::class, 'convertToNumberIfPercent'], | ||
]; | ||
|
||
/** | ||
* Identify whether a string contains a formatted numeric value, | ||
* and convert it to a numeric if it is. | ||
* | ||
* @param string $operand string value to test | ||
*/ | ||
public static function convertToNumberIfFormatted(string &$operand): bool | ||
{ | ||
foreach (self::STRING_CONVERSION_LIST as $conversionMethod) { | ||
if ($conversionMethod($operand) === true) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Identify whether a string contains a numeric value, | ||
* and convert it to a numeric if it is. | ||
* | ||
* @param string $operand string value to test | ||
*/ | ||
public static function convertToNumberIfNumeric(string &$operand): bool | ||
{ | ||
if (is_numeric($operand)) { | ||
$operand = (float) $operand; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Identify whether a string contains a fractional numeric value, | ||
* and convert it to a numeric if it is. | ||
* | ||
* @param string $operand string value to test | ||
*/ | ||
public static function convertToNumberIfFraction(string &$operand): bool | ||
{ | ||
if (preg_match(self::STRING_REGEXP_FRACTION, $operand, $match)) { | ||
$sign = ($match[1] === '-') ? '-' : '+'; | ||
$wholePart = ($match[3] === '') ? '' : ($sign . $match[3]); | ||
$fractionFormula = '=' . $wholePart . $sign . $match[4]; | ||
$operand = Calculation::getInstance()->_calculateFormulaValue($fractionFormula); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Identify whether a string contains a percentage, and if so, | ||
* convert it to a numeric. | ||
* | ||
* @param string $operand string value to test | ||
*/ | ||
public static function convertToNumberIfPercent(string &$operand): bool | ||
{ | ||
$match = []; | ||
if (preg_match(self::STRING_REGEXP_PERCENT, $operand, $match, PREG_UNMATCHED_AS_NULL)) { | ||
//Calculate the percentage | ||
$sign = ($match['PrefixedSign'] ?? $match['PrefixedSign2'] ?? $match['PostfixedSign']) ?? ''; | ||
$operand = (float) ($sign . ($match['PostfixedValue'] ?? $match['PrefixedValue'])) / 100; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.