DefaultValueBinder.php 4.0 KB

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