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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Cell;
   4  
   5  use PhpOffice\PhpSpreadsheet\RichText\RichText;
   6  use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
   7  
   8  class DataType
   9  {
  10      // Data types
  11      const TYPE_STRING2 = 'str';
  12      const TYPE_STRING = 's';
  13      const TYPE_FORMULA = 'f';
  14      const TYPE_NUMERIC = 'n';
  15      const TYPE_BOOL = 'b';
  16      const TYPE_NULL = 'null';
  17      const TYPE_INLINE = 'inlineStr';
  18      const TYPE_ERROR = 'e';
  19      const TYPE_ISO_DATE = 'd';
  20  
  21      /**
  22       * List of error codes.
  23       *
  24       * @var array<string, int>
  25       */
  26      private static $errorCodes = [
  27          '#NULL!' => 0,
  28          '#DIV/0!' => 1,
  29          '#VALUE!' => 2,
  30          '#REF!' => 3,
  31          '#NAME?' => 4,
  32          '#NUM!' => 5,
  33          '#N/A' => 6,
  34          '#CALC!' => 7,
  35      ];
  36  
  37      public const MAX_STRING_LENGTH = 32767;
  38  
  39      /**
  40       * Get list of error codes.
  41       *
  42       * @return array<string, int>
  43       */
  44      public static function getErrorCodes()
  45      {
  46          return self::$errorCodes;
  47      }
  48  
  49      /**
  50       * Check a string that it satisfies Excel requirements.
  51       *
  52       * @param null|RichText|string $textValue Value to sanitize to an Excel string
  53       *
  54       * @return RichText|string Sanitized value
  55       */
  56      public static function checkString($textValue)
  57      {
  58          if ($textValue instanceof RichText) {
  59              // TODO: Sanitize Rich-Text string (max. character count is 32,767)
  60              return $textValue;
  61          }
  62  
  63          // string must never be longer than 32,767 characters, truncate if necessary
  64          $textValue = StringHelper::substring((string) $textValue, 0, self::MAX_STRING_LENGTH);
  65  
  66          // we require that newline is represented as "\n" in core, not as "\r\n" or "\r"
  67          $textValue = str_replace(["\r\n", "\r"], "\n", $textValue);
  68  
  69          return $textValue;
  70      }
  71  
  72      /**
  73       * Check a value that it is a valid error code.
  74       *
  75       * @param mixed $value Value to sanitize to an Excel error code
  76       *
  77       * @return string Sanitized value
  78       */
  79      public static function checkErrorCode($value)
  80      {
  81          $value = (string) $value;
  82  
  83          if (!isset(self::$errorCodes[$value])) {
  84              $value = '#NULL!';
  85          }
  86  
  87          return $value;
  88      }
  89  }