Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

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