StringValueBinder.php 3.4 KB

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