1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Cell;
|
|
|
4 |
|
|
|
5 |
use DateTimeInterface;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
|
|
|
9 |
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
|
|
10 |
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
|
|
11 |
use Stringable;
|
|
|
12 |
|
|
|
13 |
class DefaultValueBinder implements IValueBinder
|
|
|
14 |
{
|
|
|
15 |
/**
|
|
|
16 |
* Bind value to a cell.
|
|
|
17 |
*
|
|
|
18 |
* @param Cell $cell Cell to bind value to
|
|
|
19 |
* @param mixed $value Value to bind in cell
|
|
|
20 |
*/
|
|
|
21 |
public function bindValue(Cell $cell, mixed $value): bool
|
|
|
22 |
{
|
|
|
23 |
// sanitize UTF-8 strings
|
|
|
24 |
if (is_string($value)) {
|
|
|
25 |
$value = StringHelper::sanitizeUTF8($value);
|
|
|
26 |
} elseif ($value === null || is_scalar($value) || $value instanceof RichText) {
|
|
|
27 |
// No need to do anything
|
|
|
28 |
} elseif ($value instanceof DateTimeInterface) {
|
|
|
29 |
$value = $value->format('Y-m-d H:i:s');
|
|
|
30 |
} elseif ($value instanceof Stringable) {
|
|
|
31 |
$value = (string) $value;
|
|
|
32 |
} else {
|
|
|
33 |
throw new SpreadsheetException('Unable to bind unstringable ' . gettype($value));
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
// Set value explicit
|
|
|
37 |
$cell->setValueExplicit($value, static::dataTypeForValue($value));
|
|
|
38 |
|
|
|
39 |
// Done!
|
|
|
40 |
return true;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* DataType for value.
|
|
|
45 |
*/
|
|
|
46 |
public static function dataTypeForValue(mixed $value): string
|
|
|
47 |
{
|
|
|
48 |
// Match the value against a few data types
|
|
|
49 |
if ($value === null) {
|
|
|
50 |
return DataType::TYPE_NULL;
|
|
|
51 |
}
|
|
|
52 |
if (is_float($value) || is_int($value)) {
|
|
|
53 |
return DataType::TYPE_NUMERIC;
|
|
|
54 |
}
|
|
|
55 |
if (is_bool($value)) {
|
|
|
56 |
return DataType::TYPE_BOOL;
|
|
|
57 |
}
|
|
|
58 |
if ($value === '') {
|
|
|
59 |
return DataType::TYPE_STRING;
|
|
|
60 |
}
|
|
|
61 |
if ($value instanceof RichText) {
|
|
|
62 |
return DataType::TYPE_INLINE;
|
|
|
63 |
}
|
|
|
64 |
if ($value instanceof Stringable) {
|
|
|
65 |
$value = (string) $value;
|
|
|
66 |
}
|
|
|
67 |
if (!is_string($value)) {
|
|
|
68 |
$gettype = is_object($value) ? get_class($value) : gettype($value);
|
|
|
69 |
|
|
|
70 |
throw new SpreadsheetException("unusable type $gettype");
|
|
|
71 |
}
|
|
|
72 |
if (strlen($value) > 1 && $value[0] === '=') {
|
|
|
73 |
$calculation = new Calculation();
|
|
|
74 |
$calculation->disableBranchPruning();
|
|
|
75 |
|
|
|
76 |
try {
|
|
|
77 |
if (empty($calculation->parseFormula($value))) {
|
|
|
78 |
return DataType::TYPE_STRING;
|
|
|
79 |
}
|
|
|
80 |
} catch (CalculationException $e) {
|
|
|
81 |
$message = $e->getMessage();
|
|
|
82 |
if (
|
|
|
83 |
$message === 'Formula Error: An unexpected error occurred'
|
|
|
84 |
|| str_contains($message, 'has no operands')
|
|
|
85 |
) {
|
|
|
86 |
return DataType::TYPE_STRING;
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
return DataType::TYPE_FORMULA;
|
|
|
91 |
}
|
|
|
92 |
if (preg_match('/^[\+\-]?(\d+\.?\d*|\d*\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $value)) {
|
|
|
93 |
$tValue = ltrim($value, '+-');
|
|
|
94 |
if (strlen($tValue) > 1 && $tValue[0] === '0' && $tValue[1] !== '.') {
|
|
|
95 |
return DataType::TYPE_STRING;
|
|
|
96 |
} elseif ((!str_contains($value, '.')) && ($value > PHP_INT_MAX)) {
|
|
|
97 |
return DataType::TYPE_STRING;
|
|
|
98 |
} elseif (!is_numeric($value)) {
|
|
|
99 |
return DataType::TYPE_STRING;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
return DataType::TYPE_NUMERIC;
|
|
|
103 |
}
|
|
|
104 |
$errorCodes = DataType::getErrorCodes();
|
|
|
105 |
if (isset($errorCodes[$value])) {
|
|
|
106 |
return DataType::TYPE_ERROR;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
return DataType::TYPE_STRING;
|
|
|
110 |
}
|
|
|
111 |
}
|