| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Cell; |
| 4 |
|
| 5 |
use DateTimeInterface; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\RichText\RichText; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
| 9 |
use Stringable; |
| 10 |
|
| 11 |
class StringValueBinder extends DefaultValueBinder implements IValueBinder |
| 12 |
{ |
| 13 |
protected bool $convertNull = true; |
| 14 |
|
| 15 |
protected bool $convertBoolean = true; |
| 16 |
|
| 17 |
protected bool $convertNumeric = true; |
| 18 |
|
| 19 |
protected bool $convertFormula = true; |
| 20 |
|
| 21 |
protected bool $setIgnoredErrors = false; |
| 22 |
|
| 23 |
public function setSetIgnoredErrors(bool $setIgnoredErrors = false): self |
| 24 |
{ |
| 25 |
$this->setIgnoredErrors = $setIgnoredErrors; |
| 26 |
|
| 27 |
return $this; |
| 28 |
} |
| 29 |
|
| 30 |
public function setNullConversion(bool $suppressConversion = false): self |
| 31 |
{ |
| 32 |
$this->convertNull = $suppressConversion; |
| 33 |
|
| 34 |
return $this; |
| 35 |
} |
| 36 |
|
| 37 |
public function setBooleanConversion(bool $suppressConversion = false): self |
| 38 |
{ |
| 39 |
$this->convertBoolean = $suppressConversion; |
| 40 |
|
| 41 |
return $this; |
| 42 |
} |
| 43 |
|
| 44 |
public function getBooleanConversion(): bool |
| 45 |
{ |
| 46 |
return $this->convertBoolean; |
| 47 |
} |
| 48 |
|
| 49 |
public function setNumericConversion(bool $suppressConversion = false): self |
| 50 |
{ |
| 51 |
$this->convertNumeric = $suppressConversion; |
| 52 |
|
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
public function setFormulaConversion(bool $suppressConversion = false): self |
| 57 |
{ |
| 58 |
$this->convertFormula = $suppressConversion; |
| 59 |
|
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
public function setConversionForAllValueTypes(bool $suppressConversion = false): self |
| 64 |
{ |
| 65 |
$this->convertNull = $suppressConversion; |
| 66 |
$this->convertBoolean = $suppressConversion; |
| 67 |
$this->convertNumeric = $suppressConversion; |
| 68 |
$this->convertFormula = $suppressConversion; |
| 69 |
|
| 70 |
return $this; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Bind value to a cell. |
| 75 |
* |
| 76 |
* @param Cell $cell Cell to bind value to |
| 77 |
* @param mixed $value Value to bind in cell |
| 78 |
*/ |
| 79 |
public function bindValue(Cell $cell, $value): bool |
| 80 |
{ |
| 81 |
if (is_object($value)) { |
| 82 |
return $this->bindObjectValue($cell, $value); |
| 83 |
} |
| 84 |
if ($value !== null && !is_scalar($value)) { |
| 85 |
throw new SpreadsheetException('Unable to bind unstringable ' . gettype($value)); |
| 86 |
} |
| 87 |
|
| 88 |
// sanitize UTF-8 strings |
| 89 |
if (is_string($value)) { |
| 90 |
$value = StringHelper::sanitizeUTF8($value); |
| 91 |
} |
| 92 |
|
| 93 |
$ignoredErrors = false; |
| 94 |
if ($value === null && $this->convertNull === false) { |
| 95 |
$cell->setValueExplicit($value, DataType::TYPE_NULL); |
| 96 |
} elseif (is_bool($value) && $this->convertBoolean === false) { |
| 97 |
$cell->setValueExplicit($value, DataType::TYPE_BOOL); |
| 98 |
} elseif ((is_int($value) || is_float($value)) && $this->convertNumeric === false) { |
| 99 |
$cell->setValueExplicit($value, DataType::TYPE_NUMERIC); |
| 100 |
} elseif (is_string($value) && strlen($value) > 1 && $value[0] === '=' && $this->convertFormula === false && parent::dataTypeForValue($value) === DataType::TYPE_FORMULA) { |
| 101 |
$cell->setValueExplicit($value, DataType::TYPE_FORMULA); |
| 102 |
} else { |
| 103 |
$ignoredErrors = is_numeric($value); |
| 104 |
$cell->setValueExplicit((string) $value, DataType::TYPE_STRING); |
| 105 |
} |
| 106 |
if ($this->setIgnoredErrors) { |
| 107 |
$cell->getIgnoredErrors()->setNumberStoredAsText($ignoredErrors); |
| 108 |
} |
| 109 |
|
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
protected function bindObjectValue(Cell $cell, object $value): bool |
| 114 |
{ |
| 115 |
// Handle any objects that might be injected |
| 116 |
$ignoredErrors = false; |
| 117 |
if ($value instanceof DateTimeInterface) { |
| 118 |
$value = $value->format('Y-m-d H:i:s'); |
| 119 |
$cell->setValueExplicit($value, DataType::TYPE_STRING); |
| 120 |
} elseif ($value instanceof RichText) { |
| 121 |
$cell->setValueExplicit($value, DataType::TYPE_INLINE); |
| 122 |
$ignoredErrors = is_numeric($value->getPlainText()); |
| 123 |
} elseif (method_exists($value, '__toString')) { |
| 124 |
$cell->setValueExplicit((string) $value, DataType::TYPE_STRING); |
| 125 |
$ignoredErrors = is_numeric((string) $value); |
| 126 |
} else { |
| 127 |
throw new SpreadsheetException('Unable to bind unstringable object of type ' . get_class($value)); |
| 128 |
} |
| 129 |
if ($this->setIgnoredErrors) { |
| 130 |
$cell->getIgnoredErrors()->setNumberStoredAsText($ignoredErrors); |
| 131 |
} |
| 132 |
|
| 133 |
return true; |
| 134 |
} |
| 135 |
} |
| 136 |
|