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