AdvancedValueBinder.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  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_AdvancedValueBinder
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Cell
  40. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Cell_AdvancedValueBinder extends 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. // Find out data type
  58. $dataType = parent::dataTypeForValue($value);
  59. // Style logic - strings
  60. if ($dataType === PHPExcel_Cell_DataType::TYPE_STRING && !$value instanceof PHPExcel_RichText) {
  61. // Test for booleans using locale-setting
  62. if ($value == PHPExcel_Calculation::getTRUE()) {
  63. $cell->setValueExplicit( True, PHPExcel_Cell_DataType::TYPE_BOOL);
  64. return true;
  65. } elseif($value == PHPExcel_Calculation::getFALSE()) {
  66. $cell->setValueExplicit( False, PHPExcel_Cell_DataType::TYPE_BOOL);
  67. return true;
  68. }
  69. // Check for number in scientific format
  70. if (preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NUMBER.'$/', $value)) {
  71. $cell->setValueExplicit( (float) $value, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  72. return true;
  73. }
  74. // Check for percentage
  75. if (preg_match('/^\-?[0-9]*\.?[0-9]*\s?\%$/', $value)) {
  76. // Convert value to number
  77. $cell->setValueExplicit( (float)str_replace('%', '', $value) / 100, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  78. // Set style
  79. $cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE );
  80. return true;
  81. }
  82. // Check for time without seconds e.g. '9:45', '09:45'
  83. if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d$/', $value)) {
  84. list($h, $m) = explode(':', $value);
  85. $days = $h / 24 + $m / 1440;
  86. // Convert value to number
  87. $cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  88. // Set style
  89. $cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME3 );
  90. return true;
  91. }
  92. // Check for time with seconds '9:45:59', '09:45:59'
  93. if (preg_match('/^(\d|[0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$/', $value)) {
  94. list($h, $m, $s) = explode(':', $value);
  95. $days = $h / 24 + $m / 1440 + $s / 86400;
  96. // Convert value to number
  97. $cell->setValueExplicit($days, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  98. // Set style
  99. $cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat()->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4 );
  100. return true;
  101. }
  102. // Check for datetime, e.g. '2008-12-31', '2008-12-31 15:59', '2008-12-31 15:59:10'
  103. if (($d = PHPExcel_Shared_Date::stringToExcel($value)) !== false) {
  104. // Convert value to number
  105. $cell->setValueExplicit($d, PHPExcel_Cell_DataType::TYPE_NUMERIC);
  106. // Determine style. Either there is a time part or not. Look for ':'
  107. if (strpos($value, ':') !== false) {
  108. $formatCode = 'yyyy-mm-dd h:mm';
  109. } else {
  110. $formatCode = 'yyyy-mm-dd';
  111. }
  112. $cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat()->setFormatCode($formatCode);
  113. return true;
  114. }
  115. // Check for newline character "\n"
  116. if (strpos($value, "\n") !== false) {
  117. $value = PHPExcel_Shared_String::SanitizeUTF8($value);
  118. $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
  119. // Set style
  120. $cell->getParent()->getStyle( $cell->getCoordinate() )->getAlignment()->setWrapText(true);
  121. return true;
  122. }
  123. }
  124. // Not bound yet? Use parent...
  125. return parent::bindValue($cell, $value);
  126. }
  127. }