DefaultValueBinder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 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 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  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 - 2013 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. }
  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. // Match the value against a few data types
  70. if (is_null($pValue)) {
  71. return PHPExcel_Cell_DataType::TYPE_NULL;
  72. } elseif ($pValue === '') {
  73. return PHPExcel_Cell_DataType::TYPE_STRING;
  74. } elseif ($pValue instanceof PHPExcel_RichText) {
  75. return PHPExcel_Cell_DataType::TYPE_INLINE;
  76. } elseif ($pValue{0} === '=' && strlen($pValue) > 1) {
  77. return PHPExcel_Cell_DataType::TYPE_FORMULA;
  78. } elseif (is_bool($pValue)) {
  79. return PHPExcel_Cell_DataType::TYPE_BOOL;
  80. } elseif (is_float($pValue) || is_int($pValue)) {
  81. return PHPExcel_Cell_DataType::TYPE_NUMERIC;
  82. } elseif (preg_match('/^\-?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)$/', $pValue)) {
  83. return PHPExcel_Cell_DataType::TYPE_NUMERIC;
  84. } elseif (is_string($pValue) && array_key_exists($pValue, PHPExcel_Cell_DataType::getErrorCodes())) {
  85. return PHPExcel_Cell_DataType::TYPE_ERROR;
  86. } else {
  87. return PHPExcel_Cell_DataType::TYPE_STRING;
  88. }
  89. }
  90. }