DefaultValueBinder.php 3.8 KB

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