| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 8 |
|
| 9 |
class Helpers |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Many functions accept null/false/true argument treated as 0/0/1. |
| 13 |
* |
| 14 |
* @return float|string quotient or DIV0 if denominator is too small |
| 15 |
*/ |
| 16 |
public static function verySmallDenominator(float $numerator, float $denominator) |
| 17 |
{ |
| 18 |
return (abs($denominator) < 1.0E-12) ? ExcelError::DIV0() : ($numerator / $denominator); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Many functions accept null/false/true argument treated as 0/0/1. |
| 23 |
* @return int|float |
| 24 |
* @param mixed $number |
| 25 |
*/ |
| 26 |
public static function validateNumericNullBool($number) |
| 27 |
{ |
| 28 |
$number = Functions::flattenSingleValue($number); |
| 29 |
if ($number === null) { |
| 30 |
return 0; |
| 31 |
} |
| 32 |
if (is_bool($number)) { |
| 33 |
return (int) $number; |
| 34 |
} |
| 35 |
if (is_numeric($number)) { |
| 36 |
return 0 + $number; |
| 37 |
} |
| 38 |
|
| 39 |
throw new Exception(ExcelError::throwError($number)); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Validate numeric, but allow substitute for null. |
| 44 |
* @param null|float|int $substitute |
| 45 |
* @return float|int |
| 46 |
* @param mixed $number |
| 47 |
*/ |
| 48 |
public static function validateNumericNullSubstitution($number, $substitute) |
| 49 |
{ |
| 50 |
$number = Functions::flattenSingleValue($number); |
| 51 |
if ($number === null && $substitute !== null) { |
| 52 |
return $substitute; |
| 53 |
} |
| 54 |
if (is_numeric($number)) { |
| 55 |
return 0 + $number; |
| 56 |
} |
| 57 |
|
| 58 |
throw new Exception(ExcelError::throwError($number)); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Confirm number >= 0. |
| 63 |
* @param float|int $number |
| 64 |
*/ |
| 65 |
public static function validateNotNegative($number, ?string $except = null): void |
| 66 |
{ |
| 67 |
if ($number >= 0) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
throw new Exception($except ?? ExcelError::NAN()); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Confirm number > 0. |
| 76 |
* @param float|int $number |
| 77 |
*/ |
| 78 |
public static function validatePositive($number, ?string $except = null): void |
| 79 |
{ |
| 80 |
if ($number > 0) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
throw new Exception($except ?? ExcelError::NAN()); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Confirm number != 0. |
| 89 |
* @param float|int $number |
| 90 |
*/ |
| 91 |
public static function validateNotZero($number): void |
| 92 |
{ |
| 93 |
if ($number) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
throw new Exception(ExcelError::DIV0()); |
| 98 |
} |
| 99 |
|
| 100 |
public static function returnSign(float $number): int |
| 101 |
{ |
| 102 |
return $number ? (($number > 0) ? 1 : -1) : 0; |
| 103 |
} |
| 104 |
|
| 105 |
public static function getEven(float $number): float |
| 106 |
{ |
| 107 |
$significance = 2 * self::returnSign($number); |
| 108 |
|
| 109 |
return $significance ? (ceil($number / $significance) * $significance) : 0; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Return NAN or value depending on argument. |
| 114 |
* @return float|string |
| 115 |
*/ |
| 116 |
public static function numberOrNan(float $result) |
| 117 |
{ |
| 118 |
return is_nan($result) ? ExcelError::NAN() : $result; |
| 119 |
} |
| 120 |
} |
| 121 |
|