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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Cell;
   4  
   5  use DateTimeInterface;
   6  use PhpOffice\PhpSpreadsheet\RichText\RichText;
   7  use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
   8  
   9  class DefaultValueBinder implements IValueBinder
  10  {
  11      /**
  12       * Bind value to a cell.
  13       *
  14       * @param Cell $cell Cell to bind value to
  15       * @param mixed $value Value to bind in cell
  16       *
  17       * @return bool
  18       */
  19      public function bindValue(Cell $cell, $value)
  20      {
  21          // sanitize UTF-8 strings
  22          if (is_string($value)) {
  23              $value = StringHelper::sanitizeUTF8($value);
  24          } elseif (is_object($value)) {
  25              // Handle any objects that might be injected
  26              if ($value instanceof DateTimeInterface) {
  27                  $value = $value->format('Y-m-d H:i:s');
  28              } elseif (!($value instanceof RichText)) {
  29                  $value = (string) $value;
  30              }
  31          }
  32  
  33          // Set value explicit
  34          $cell->setValueExplicit($value, static::dataTypeForValue($value));
  35  
  36          // Done!
  37          return true;
  38      }
  39  
  40      /**
  41       * DataType for value.
  42       *
  43       * @param mixed $pValue
  44       *
  45       * @return string
  46       */
  47      public static function dataTypeForValue($pValue)
  48      {
  49          // Match the value against a few data types
  50          if ($pValue === null) {
  51              return DataType::TYPE_NULL;
  52          } elseif (is_float($pValue) || is_int($pValue)) {
  53              return DataType::TYPE_NUMERIC;
  54          } elseif (is_bool($pValue)) {
  55              return DataType::TYPE_BOOL;
  56          } elseif ($pValue === '') {
  57              return DataType::TYPE_STRING;
  58          } elseif ($pValue instanceof RichText) {
  59              return DataType::TYPE_INLINE;
  60          } elseif (is_string($pValue) && $pValue[0] === '=' && strlen($pValue) > 1) {
  61              return DataType::TYPE_FORMULA;
  62          } elseif (preg_match('/^[\+\-]?(\d+\\.?\d*|\d*\\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
  63              $tValue = ltrim($pValue, '+-');
  64              if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') {
  65                  return DataType::TYPE_STRING;
  66              } elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) {
  67                  return DataType::TYPE_STRING;
  68              } elseif (!is_numeric($pValue)) {
  69                  return DataType::TYPE_STRING;
  70              }
  71  
  72              return DataType::TYPE_NUMERIC;
  73          } elseif (is_string($pValue)) {
  74              $errorCodes = DataType::getErrorCodes();
  75              if (isset($errorCodes[$pValue])) {
  76                  return DataType::TYPE_ERROR;
  77              }
  78          }
  79  
  80          return DataType::TYPE_STRING;
  81      }
  82  }