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