No Description

DefaultValueBinder.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 PHPExcel
  6. *
  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.
  11. *
  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.
  16. *
  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
  20. *
  21. * @category PHPExcel
  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##
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  34. }
  35. /**
  36. * PHPExcel_Cell_DefaultValueBinder
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Cell
  40. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
  43. {
  44. /**
  45. * Bind value to a cell
  46. *
  47. * @param PHPExcel_Cell $cell Cell to bind value to
  48. * @param mixed $value Value to bind in cell
  49. * @return boolean
  50. */
  51. public function bindValue(PHPExcel_Cell $cell, $value = null)
  52. {
  53. // sanitize UTF-8 strings
  54. if (is_string($value)) {
  55. $value = PHPExcel_Shared_String::SanitizeUTF8($value);
  56. } elseif (is_object($value)) {
  57. // Handle any objects that might be injected
  58. if ($value instanceof DateTime) {
  59. $value = $value->format('Y-m-d H:i:s');
  60. } elseif (!($value instanceof PHPExcel_RichText)) {
  61. $value = (string) $value;
  62. }
  63. }
  64. // Set value explicit
  65. $cell->setValueExplicit( $value, self::dataTypeForValue($value) );
  66. // Done!
  67. return true;
  68. }
  69. /**
  70. * DataType for value
  71. *
  72. * @param mixed $pValue
  73. * @return string
  74. */
  75. public static function dataTypeForValue($pValue = null) {
  76. // Match the value against a few data types
  77. if ($pValue === null) {
  78. return PHPExcel_Cell_DataType::TYPE_NULL;
  79. } elseif ($pValue === '') {
  80. return PHPExcel_Cell_DataType::TYPE_STRING;
  81. } elseif ($pValue instanceof PHPExcel_RichText) {
  82. return PHPExcel_Cell_DataType::TYPE_INLINE;
  83. } elseif ($pValue{0} === '=' && strlen($pValue) > 1) {
  84. return PHPExcel_Cell_DataType::TYPE_FORMULA;
  85. } elseif (is_bool($pValue)) {
  86. return PHPExcel_Cell_DataType::TYPE_BOOL;
  87. } elseif (is_float($pValue) || is_int($pValue)) {
  88. return PHPExcel_Cell_DataType::TYPE_NUMERIC;
  89. } elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
  90. $tValue = ltrim($pValue, '+-');
  91. if (is_string($pValue) && $tValue{0} === '0' && strlen($tValue) > 1 && $tValue{1} !== '.' ) {
  92. return PHPExcel_Cell_DataType::TYPE_STRING;
  93. } elseif((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) {
  94. return PHPExcel_Cell_DataType::TYPE_STRING;
  95. }
  96. return PHPExcel_Cell_DataType::TYPE_NUMERIC;
  97. } elseif (is_string($pValue) && array_key_exists($pValue, PHPExcel_Cell_DataType::getErrorCodes())) {
  98. return PHPExcel_Cell_DataType::TYPE_ERROR;
  99. }
  100. return PHPExcel_Cell_DataType::TYPE_STRING;
  101. }
  102. }