5 * Copyright (c) 2006 - 2014 PHPExcel
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * @package PHPExcel_Cell
23 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25 * @version ##VERSION##, ##DATE##
29 /** PHPExcel root directory */
30 if (!defined('PHPEXCEL_ROOT')) {
34 define('PHPEXCEL_ROOT', dirname(__FILE__
) . '/../../');
35 require(PHPEXCEL_ROOT
. 'PHPExcel/Autoloader.php');
40 * PHPExcel_Cell_DefaultValueBinder
43 * @package PHPExcel_Cell
44 * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
46 class PHPExcel_Cell_DefaultValueBinder
implements PHPExcel_Cell_IValueBinder
49 * Bind value to a cell
51 * @param PHPExcel_Cell $cell Cell to bind value to
52 * @param mixed $value Value to bind in cell
55 public function bindValue(PHPExcel_Cell
$cell, $value = null)
57 // sanitize UTF-8 strings
58 if (is_string($value)) {
59 $value = PHPExcel_Shared_String
::SanitizeUTF8($value);
60 } elseif (is_object($value)) {
61 // Handle any objects that might be injected
62 if ($value instanceof DateTime
) {
63 $value = $value->format('Y-m-d H:i:s');
64 } elseif (!($value instanceof PHPExcel_RichText
)) {
65 $value = (string) $value;
70 $cell->setValueExplicit( $value, self
::dataTypeForValue($value) );
79 * @param mixed $pValue
82 public static function dataTypeForValue($pValue = null) {
83 // Match the value against a few data types
84 if ($pValue === null) {
85 return PHPExcel_Cell_DataType
::TYPE_NULL
;
86 } elseif ($pValue === '') {
87 return PHPExcel_Cell_DataType
::TYPE_STRING
;
88 } elseif ($pValue instanceof PHPExcel_RichText
) {
89 return PHPExcel_Cell_DataType
::TYPE_INLINE
;
90 } elseif ($pValue{0} === '=' && strlen($pValue) > 1) {
91 return PHPExcel_Cell_DataType
::TYPE_FORMULA
;
92 } elseif (is_bool($pValue)) {
93 return PHPExcel_Cell_DataType
::TYPE_BOOL
;
94 } elseif (is_float($pValue) ||
is_int($pValue)) {
95 return PHPExcel_Cell_DataType
::TYPE_NUMERIC
;
96 } elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
97 $tValue = ltrim($pValue, '+-');
98 if (is_string($pValue) && $tValue{0} === '0' && strlen($tValue) > 1 && $tValue{1} !== '.' ) {
99 return PHPExcel_Cell_DataType
::TYPE_STRING
;
100 } elseif((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX
)) {
101 return PHPExcel_Cell_DataType
::TYPE_STRING
;
103 return PHPExcel_Cell_DataType
::TYPE_NUMERIC
;
104 } elseif (is_string($pValue) && array_key_exists($pValue, PHPExcel_Cell_DataType
::getErrorCodes())) {
105 return PHPExcel_Cell_DataType
::TYPE_ERROR
;
108 return PHPExcel_Cell_DataType
::TYPE_STRING
;