1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Cell; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright (c) 2006 - 2016 PhpSpreadsheet. |
7
|
|
|
* |
8
|
|
|
* This library is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU Lesser General Public |
10
|
|
|
* License as published by the Free Software Foundation; either |
11
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This library is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16
|
|
|
* Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Lesser General Public |
19
|
|
|
* License along with this library; if not, write to the Free Software |
20
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21
|
|
|
* |
22
|
|
|
* @category PhpSpreadsheet |
23
|
|
|
* |
24
|
|
|
* @copyright Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
25
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
26
|
|
|
*/ |
27
|
|
|
class DefaultValueBinder implements IValueBinder |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Bind value to a cell. |
31
|
|
|
* |
32
|
|
|
* @param \PhpOffice\PhpSpreadsheet\Cell $cell Cell to bind value to |
33
|
|
|
* @param mixed $value Value to bind in cell |
34
|
|
|
* |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
68 |
|
public function bindValue(\PhpOffice\PhpSpreadsheet\Cell $cell, $value = null) |
38
|
|
|
{ |
39
|
|
|
// sanitize UTF-8 strings |
40
|
68 |
|
if (is_string($value)) { |
41
|
58 |
|
$value = \PhpOffice\PhpSpreadsheet\Shared\StringHelper::sanitizeUTF8($value); |
42
|
51 |
|
} elseif (is_object($value)) { |
43
|
|
|
// Handle any objects that might be injected |
44
|
17 |
|
if ($value instanceof \DateTime) { |
45
|
1 |
|
$value = $value->format('Y-m-d H:i:s'); |
46
|
|
|
} elseif (!($value instanceof \PhpOffice\PhpSpreadsheet\RichText)) { |
47
|
|
|
$value = (string) $value; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Set value explicit |
52
|
68 |
|
$cell->setValueExplicit($value, self::dataTypeForValue($value)); |
53
|
|
|
|
54
|
|
|
// Done! |
55
|
68 |
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* DataType for value. |
60
|
|
|
* |
61
|
|
|
* @param mixed $pValue |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
95 |
|
public static function dataTypeForValue($pValue = null) |
66
|
|
|
{ |
67
|
|
|
// Match the value against a few data types |
68
|
95 |
|
if ($pValue === null) { |
69
|
18 |
|
return DataType::TYPE_NULL; |
70
|
93 |
|
} elseif ($pValue === '') { |
71
|
14 |
|
return DataType::TYPE_STRING; |
72
|
|
|
} elseif ($pValue instanceof \PhpOffice\PhpSpreadsheet\RichText) { |
73
|
17 |
|
return DataType::TYPE_INLINE; |
74
|
89 |
|
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) { |
75
|
24 |
|
return DataType::TYPE_FORMULA; |
76
|
87 |
|
} elseif (is_bool($pValue)) { |
77
|
10 |
|
return DataType::TYPE_BOOL; |
78
|
83 |
|
} elseif (is_float($pValue) || is_int($pValue)) { |
79
|
46 |
|
return DataType::TYPE_NUMERIC; |
80
|
76 |
|
} elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) { |
81
|
29 |
|
$tValue = ltrim($pValue, '+-'); |
82
|
29 |
|
if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') { |
83
|
1 |
|
return DataType::TYPE_STRING; |
84
|
28 |
|
} elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) { |
85
|
|
|
return DataType::TYPE_STRING; |
86
|
|
|
} |
87
|
|
|
|
88
|
28 |
|
return DataType::TYPE_NUMERIC; |
89
|
67 |
|
} elseif (is_string($pValue)) { |
90
|
67 |
|
$errorCodes = DataType::getErrorCodes(); |
91
|
67 |
|
if (isset($errorCodes[$pValue])) { |
92
|
3 |
|
return DataType::TYPE_ERROR; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
64 |
|
return DataType::TYPE_STRING; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|