| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\TextData; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ErrorValue; |
| 9 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 10 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
| 11 |
|
| 12 |
class Helpers |
| 13 |
{ |
| 14 |
public static function convertBooleanValue(bool $value): string |
| 15 |
{ |
| 16 |
if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) { |
| 17 |
return $value ? '1' : '0'; |
| 18 |
} |
| 19 |
|
| 20 |
return ($value) ? Calculation::getTRUE() : Calculation::getFALSE(); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* @param mixed $value String value from which to extract characters |
| 25 |
*/ |
| 26 |
public static function extractString($value, bool $throwIfError = false): string |
| 27 |
{ |
| 28 |
if (is_bool($value)) { |
| 29 |
return self::convertBooleanValue($value); |
| 30 |
} |
| 31 |
if ($throwIfError && is_string($value) && ErrorValue::isError($value, true)) { |
| 32 |
throw new CalcExp($value); |
| 33 |
} |
| 34 |
|
| 35 |
return StringHelper::convertToString($value); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @param mixed $value |
| 40 |
*/ |
| 41 |
public static function extractInt($value, int $minValue, int $gnumericNull = 0, bool $ooBoolOk = false): int |
| 42 |
{ |
| 43 |
if ($value === null) { |
| 44 |
// usually 0, but sometimes 1 for Gnumeric |
| 45 |
$value = (Functions::getCompatibilityMode() === Functions::COMPATIBILITY_GNUMERIC) ? $gnumericNull : 0; |
| 46 |
} |
| 47 |
if (is_bool($value) && ($ooBoolOk || Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_OPENOFFICE)) { |
| 48 |
$value = (int) $value; |
| 49 |
} |
| 50 |
if (!is_numeric($value)) { |
| 51 |
throw new CalcExp(ExcelError::VALUE()); |
| 52 |
} |
| 53 |
$value = (int) $value; |
| 54 |
if ($value < $minValue) { |
| 55 |
throw new CalcExp(ExcelError::VALUE()); |
| 56 |
} |
| 57 |
|
| 58 |
return (int) $value; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @param mixed $value |
| 63 |
*/ |
| 64 |
public static function extractFloat($value): float |
| 65 |
{ |
| 66 |
if ($value === null) { |
| 67 |
$value = 0.0; |
| 68 |
} |
| 69 |
if (is_bool($value)) { |
| 70 |
$value = (float) $value; |
| 71 |
} |
| 72 |
if (!is_numeric($value)) { |
| 73 |
if (is_string($value) && ErrorValue::isError($value, true)) { |
| 74 |
throw new CalcExp($value); |
| 75 |
} |
| 76 |
|
| 77 |
throw new CalcExp(ExcelError::VALUE()); |
| 78 |
} |
| 79 |
|
| 80 |
return (float) $value; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @param mixed $value |
| 85 |
*/ |
| 86 |
public static function validateInt($value, bool $throwIfError = false): int |
| 87 |
{ |
| 88 |
if ($value === null) { |
| 89 |
$value = 0; |
| 90 |
} elseif (is_bool($value)) { |
| 91 |
$value = (int) $value; |
| 92 |
} elseif ($throwIfError && is_string($value) && !is_numeric($value)) { |
| 93 |
if (!ErrorValue::isError($value, true)) { |
| 94 |
$value = ExcelError::VALUE(); |
| 95 |
} |
| 96 |
|
| 97 |
throw new CalcExp($value); |
| 98 |
} |
| 99 |
|
| 100 |
return (int) StringHelper::convertToString($value); |
| 101 |
} |
| 102 |
} |
| 103 |
|