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 401 and 402] [Versions 401 and 403]

   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 StringValueBinder implements IValueBinder
  10  {
  11      /**
  12       * @var bool
  13       */
  14      protected $convertNull = true;
  15  
  16      /**
  17       * @var bool
  18       */
  19      protected $convertBoolean = true;
  20  
  21      /**
  22       * @var bool
  23       */
  24      protected $convertNumeric = true;
  25  
  26      /**
  27       * @var bool
  28       */
  29      protected $convertFormula = true;
  30  
  31      public function setNullConversion(bool $suppressConversion = false): self
  32      {
  33          $this->convertNull = $suppressConversion;
  34  
  35          return $this;
  36      }
  37  
  38      public function setBooleanConversion(bool $suppressConversion = false): self
  39      {
  40          $this->convertBoolean = $suppressConversion;
  41  
  42          return $this;
  43      }
  44  
  45      public function getBooleanConversion(): bool
  46      {
  47          return $this->convertBoolean;
  48      }
  49  
  50      public function setNumericConversion(bool $suppressConversion = false): self
  51      {
  52          $this->convertNumeric = $suppressConversion;
  53  
  54          return $this;
  55      }
  56  
  57      public function setFormulaConversion(bool $suppressConversion = false): self
  58      {
  59          $this->convertFormula = $suppressConversion;
  60  
  61          return $this;
  62      }
  63  
  64      public function setConversionForAllValueTypes(bool $suppressConversion = false): self
  65      {
  66          $this->convertNull = $suppressConversion;
  67          $this->convertBoolean = $suppressConversion;
  68          $this->convertNumeric = $suppressConversion;
  69          $this->convertFormula = $suppressConversion;
  70  
  71          return $this;
  72      }
  73  
  74      /**
  75       * Bind value to a cell.
  76       *
  77       * @param Cell $cell Cell to bind value to
  78       * @param mixed $value Value to bind in cell
  79       */
  80      public function bindValue(Cell $cell, $value)
  81      {
  82          if (is_object($value)) {
  83              return $this->bindObjectValue($cell, $value);
  84          }
  85  
  86          // sanitize UTF-8 strings
  87          if (is_string($value)) {
  88              $value = StringHelper::sanitizeUTF8($value);
  89          }
  90  
  91          if ($value === null && $this->convertNull === false) {
  92              $cell->setValueExplicit($value, DataType::TYPE_NULL);
  93          } elseif (is_bool($value) && $this->convertBoolean === false) {
  94              $cell->setValueExplicit($value, DataType::TYPE_BOOL);
  95          } elseif ((is_int($value) || is_float($value)) && $this->convertNumeric === false) {
  96              $cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
  97          } elseif (is_string($value) && strlen($value) > 1 && $value[0] === '=' && $this->convertFormula === false) {
  98              $cell->setValueExplicit($value, DataType::TYPE_FORMULA);
  99          } else {
 100              if (is_string($value) && strlen($value) > 1 && $value[0] === '=') {
 101                  $cell->getStyle()->setQuotePrefix(true);
 102              }
 103              $cell->setValueExplicit((string) $value, DataType::TYPE_STRING);
 104          }
 105  
 106          return true;
 107      }
 108  
 109      protected function bindObjectValue(Cell $cell, object $value): bool
 110      {
 111          // Handle any objects that might be injected
 112          if ($value instanceof DateTimeInterface) {
 113              $value = $value->format('Y-m-d H:i:s');
 114          } elseif ($value instanceof RichText) {
 115              $cell->setValueExplicit($value, DataType::TYPE_INLINE);
 116  
 117              return true;
 118          }
 119  
 120          $cell->setValueExplicit((string) $value, DataType::TYPE_STRING);
 121  
 122          return true;
 123      }
 124  }