Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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  
  20      /**
  21       * List of error codes.
  22       *
  23       * @var array
  24       */
  25      private static $errorCodes = [
  26          '#NULL!' => 0,
  27          '#DIV/0!' => 1,
  28          '#VALUE!' => 2,
  29          '#REF!' => 3,
  30          '#NAME?' => 4,
  31          '#NUM!' => 5,
  32          '#N/A' => 6,
  33      ];
  34  
  35      /**
  36       * Get list of error codes.
  37       *
  38       * @return array
  39       */
  40      public static function getErrorCodes()
  41      {
  42          return self::$errorCodes;
  43      }
  44  
  45      /**
  46       * Check a string that it satisfies Excel requirements.
  47       *
  48       * @param null|RichText|string $pValue Value to sanitize to an Excel string
  49       *
  50       * @return null|RichText|string Sanitized value
  51       */
  52      public static function checkString($pValue)
  53      {
  54          if ($pValue instanceof RichText) {
  55              // TODO: Sanitize Rich-Text string (max. character count is 32,767)
  56              return $pValue;
  57          }
  58  
  59          // string must never be longer than 32,767 characters, truncate if necessary
  60          $pValue = StringHelper::substring($pValue, 0, 32767);
  61  
  62          // we require that newline is represented as "\n" in core, not as "\r\n" or "\r"
  63          $pValue = str_replace(["\r\n", "\r"], "\n", $pValue);
  64  
  65          return $pValue;
  66      }
  67  
  68      /**
  69       * Check a value that it is a valid error code.
  70       *
  71       * @param mixed $pValue Value to sanitize to an Excel error code
  72       *
  73       * @return string Sanitized value
  74       */
  75      public static function checkErrorCode($pValue)
  76      {
  77          $pValue = (string) $pValue;
  78  
  79          if (!isset(self::$errorCodes[$pValue])) {
  80              $pValue = '#NULL!';
  81          }
  82  
  83          return $pValue;
  84      }
  85  }