visualizer
/
vendor
/
phpoffice
/
phpspreadsheet
/
src
/
PhpSpreadsheet
/
Cell
/
DefaultValueBinder.php
DefaultValueBinder.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.5.0, at vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/DefaultValueBinder.php
| 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 DefaultValueBinder implements IValueBinder |
| 10 | { |
| 11 | /** |
| 12 | * Bind value to a cell. |
| 13 | * |
| 14 | * @param Cell $cell Cell to bind value to |
| 15 | * @param mixed $value Value to bind in cell |
| 16 | * |
| 17 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 18 | * |
| 19 | * @return bool |
| 20 | */ |
| 21 | public function bindValue(Cell $cell, $value) |
| 22 | { |
| 23 | // sanitize UTF-8 strings |
| 24 | if (is_string($value)) { |
| 25 | $value = StringHelper::sanitizeUTF8($value); |
| 26 | } elseif (is_object($value)) { |
| 27 | // Handle any objects that might be injected |
| 28 | if ($value instanceof DateTimeInterface) { |
| 29 | $value = $value->format('Y-m-d H:i:s'); |
| 30 | } elseif (!($value instanceof RichText)) { |
| 31 | $value = (string) $value; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Set value explicit |
| 36 | $cell->setValueExplicit($value, static::dataTypeForValue($value)); |
| 37 | |
| 38 | // Done! |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * DataType for value. |
| 44 | * |
| 45 | * @param mixed $pValue |
| 46 | * |
| 47 | * @return string |
| 48 | */ |
| 49 | public static function dataTypeForValue($pValue) |
| 50 | { |
| 51 | // Match the value against a few data types |
| 52 | if ($pValue === null) { |
| 53 | return DataType::TYPE_NULL; |
| 54 | } elseif ($pValue === '') { |
| 55 | return DataType::TYPE_STRING; |
| 56 | } elseif ($pValue instanceof RichText) { |
| 57 | return DataType::TYPE_INLINE; |
| 58 | } elseif ($pValue[0] === '=' && strlen($pValue) > 1) { |
| 59 | return DataType::TYPE_FORMULA; |
| 60 | } elseif (is_bool($pValue)) { |
| 61 | return DataType::TYPE_BOOL; |
| 62 | } elseif (is_float($pValue) || is_int($pValue)) { |
| 63 | return DataType::TYPE_NUMERIC; |
| 64 | } elseif (preg_match('/^[\+\-]?(\d+\\.?\d*|\d*\\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) { |
| 65 | $tValue = ltrim($pValue, '+-'); |
| 66 | if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') { |
| 67 | return DataType::TYPE_STRING; |
| 68 | } elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) { |
| 69 | return DataType::TYPE_STRING; |
| 70 | } |
| 71 | |
| 72 | return DataType::TYPE_NUMERIC; |
| 73 | } elseif (is_string($pValue)) { |
| 74 | $errorCodes = DataType::getErrorCodes(); |
| 75 | if (isset($errorCodes[$pValue])) { |
| 76 | return DataType::TYPE_ERROR; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | return DataType::TYPE_STRING; |
| 81 | } |
| 82 | } |
| 83 |