PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Cell / DefaultValueBinder.php

DefaultValueBinder.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Cell/DefaultValueBinder.php

147 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Cell;
4
5 use TablePress\Composer\Pcre\Preg;
6 use DateTimeInterface;
7 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\CalculationParserOnly;
8 use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
9 use TablePress\PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
10 use TablePress\PhpOffice\PhpSpreadsheet\RichText\RichText;
11 use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper;
12 use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\BaseDrawing;
13 use Stringable;
14
15 class DefaultValueBinder implements IValueBinder
16 {
17 // 123 456 789 012 345
18 private const FIFTEEN_NINES = 999_999_999_999_999;
19
20 /**
21 * Bind value to a cell.
22 *
23 * @param Cell $cell Cell to bind value to
24 * @param mixed $value Value to bind in cell
25 */
26 public function bindValue(Cell $cell, $value): bool
27 {
28 // sanitize UTF-8 strings
29 if (is_string($value)) {
30 $value = StringHelper::sanitizeUTF8($value);
31 } elseif ($value === null || is_scalar($value) || $value instanceof RichText) {
32 // No need to do anything
33 } elseif ($value instanceof DateTimeInterface) {
34 $value = $value->format('Y-m-d H:i:s');
35 } elseif (is_object($value) && method_exists($value, '__toString')) {
36 $value = (string) $value;
37 } elseif ($value instanceof BaseDrawing) {
38 $value->setCoordinates($cell->getCoordinate());
39 $value->setResizeProportional(false);
40 $value->setInCell(true);
41 $value->setWorksheet($cell->getWorksheet(), true);
42 } else {
43 throw new SpreadsheetException('Unable to bind unstringable ' . gettype($value));
44 }
45
46 // Set value explicit
47 $cell->setValueExplicit($value, static::dataTypeForValue($value));
48
49 // Done!
50 return true;
51 }
52
53 /**
54 * DataType for value.
55 * @param mixed $value
56 */
57 public static function dataTypeForValue($value): string
58 {
59 // Match the value against a few data types
60 if ($value === null) {
61 return DataType::TYPE_NULL;
62 }
63 if (is_int($value) && abs($value) > self::FIFTEEN_NINES) {
64 return DataType::TYPE_STRING;
65 }
66 if (is_float($value) || is_int($value)) {
67 return DataType::TYPE_NUMERIC;
68 }
69 if (is_bool($value)) {
70 return DataType::TYPE_BOOL;
71 }
72 if ($value === '') {
73 return DataType::TYPE_STRING;
74 }
75 if ($value instanceof RichText) {
76 return DataType::TYPE_INLINE;
77 }
78 if ($value instanceof BaseDrawing) {
79 return DataType::TYPE_DRAWING_IN_CELL;
80 }
81 if (is_object($value) && method_exists($value, '__toString')) {
82 $value = (string) $value;
83 }
84 if (!is_string($value)) {
85 $gettype = is_object($value) ? get_class($value) : gettype($value);
86
87 throw new SpreadsheetException("unusable type $gettype");
88 }
89 if (strlen($value) > 1 && $value[0] === '=') {
90 $calculation = CalculationParserOnly::getParserInstance();
91
92 try {
93 if (empty($calculation->parseFormula($value))) {
94 return DataType::TYPE_STRING;
95 }
96 } catch (CalculationException $e) {
97 $message = $e->getMessage();
98 if (
99 $message === 'Formula Error: An unexpected error occurred'
100 || str_contains($message, 'has no operands')
101 ) {
102 return DataType::TYPE_STRING;
103 }
104 }
105
106 return DataType::TYPE_FORMULA;
107 }
108 if (Preg::isMatch('/^[\+\-]?(\d+\.?\d*|\d*\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $value)) {
109 $tValue = ltrim($value, '+-');
110 if (strlen($tValue) > 1 && $tValue[0] === '0' && $tValue[1] !== '.') {
111 return DataType::TYPE_STRING;
112 }
113 if (!Preg::isMatch('/[eE.]/', $value)) {
114 $aValue = abs((float) $value);
115 if ($aValue > self::FIFTEEN_NINES) {
116 return DataType::TYPE_STRING;
117 }
118 }
119 if (!is_numeric($value) || !is_finite((float) $value)) {
120 return DataType::TYPE_STRING;
121 }
122
123 return DataType::TYPE_NUMERIC;
124 }
125 $errorCodes = DataType::getErrorCodes();
126 if (isset($errorCodes[$value])) {
127 return DataType::TYPE_ERROR;
128 }
129
130 return DataType::TYPE_STRING;
131 }
132
133 protected bool $preserveCr = false;
134
135 public function getPreserveCr(): bool
136 {
137 return $this->preserveCr;
138 }
139
140 public function setPreserveCr(bool $preserveCr): self
141 {
142 $this->preserveCr = $preserveCr;
143
144 return $this;
145 }
146 }
147