Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  use 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       *
  24       * @param mixed $number
  25       *
  26       * @return float|int
  27       */
  28      public static function validateNumericNullBool($number)
  29      {
  30          $number = Functions::flattenSingleValue($number);
  31          if ($number === null) {
  32              return 0;
  33          }
  34          if (is_bool($number)) {
  35              return (int) $number;
  36          }
  37          if (is_numeric($number)) {
  38              return 0 + $number;
  39          }
  40  
  41          throw new Exception(ExcelError::throwError($number));
  42      }
  43  
  44      /**
  45       * Validate numeric, but allow substitute for null.
  46       *
  47       * @param mixed $number
  48       * @param null|float|int $substitute
  49       *
  50       * @return float|int
  51       */
  52      public static function validateNumericNullSubstitution($number, $substitute)
  53      {
  54          $number = Functions::flattenSingleValue($number);
  55          if ($number === null && $substitute !== null) {
  56              return $substitute;
  57          }
  58          if (is_numeric($number)) {
  59              return 0 + $number;
  60          }
  61  
  62          throw new Exception(ExcelError::throwError($number));
  63      }
  64  
  65      /**
  66       * Confirm number >= 0.
  67       *
  68       * @param float|int $number
  69       */
  70      public static function validateNotNegative($number, ?string $except = null): void
  71      {
  72          if ($number >= 0) {
  73              return;
  74          }
  75  
  76          throw new Exception($except ?? ExcelError::NAN());
  77      }
  78  
  79      /**
  80       * Confirm number > 0.
  81       *
  82       * @param float|int $number
  83       */
  84      public static function validatePositive($number, ?string $except = null): void
  85      {
  86          if ($number > 0) {
  87              return;
  88          }
  89  
  90          throw new Exception($except ?? ExcelError::NAN());
  91      }
  92  
  93      /**
  94       * Confirm number != 0.
  95       *
  96       * @param float|int $number
  97       */
  98      public static function validateNotZero($number): void
  99      {
 100          if ($number) {
 101              return;
 102          }
 103  
 104          throw new Exception(ExcelError::DIV0());
 105      }
 106  
 107      public static function returnSign(float $number): int
 108      {
 109          return $number ? (($number > 0) ? 1 : -1) : 0;
 110      }
 111  
 112      public static function getEven(float $number): float
 113      {
 114          $significance = 2 * self::returnSign($number);
 115  
 116          return $significance ? (ceil($number / $significance) * $significance) : 0;
 117      }
 118  
 119      /**
 120       * Return NAN or value depending on argument.
 121       *
 122       * @param float $result Number
 123       *
 124       * @return float|string
 125       */
 126      public static function numberOrNan($result)
 127      {
 128          return is_nan($result) ? ExcelError::NAN() : $result;
 129      }
 130  }