DataType.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * PHPExcel_Cell_DataType
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 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. class PHPExcel_Cell_DataType
  28. {
  29. /* Data types */
  30. const TYPE_STRING2 = 'str';
  31. const TYPE_STRING = 's';
  32. const TYPE_FORMULA = 'f';
  33. const TYPE_NUMERIC = 'n';
  34. const TYPE_BOOL = 'b';
  35. const TYPE_NULL = 'null';
  36. const TYPE_INLINE = 'inlineStr';
  37. const TYPE_ERROR = 'e';
  38. /**
  39. * List of error codes
  40. *
  41. * @var array
  42. */
  43. private static $errorCodes = array(
  44. '#NULL!' => 0,
  45. '#DIV/0!' => 1,
  46. '#VALUE!' => 2,
  47. '#REF!' => 3,
  48. '#NAME?' => 4,
  49. '#NUM!' => 5,
  50. '#N/A' => 6
  51. );
  52. /**
  53. * Get list of error codes
  54. *
  55. * @return array
  56. */
  57. public static function getErrorCodes()
  58. {
  59. return self::$errorCodes;
  60. }
  61. /**
  62. * DataType for value
  63. *
  64. * @deprecated Replaced by PHPExcel_Cell_IValueBinder infrastructure, will be removed in version 1.8.0
  65. * @param mixed $pValue
  66. * @return string
  67. */
  68. public static function dataTypeForValue($pValue = null)
  69. {
  70. return PHPExcel_Cell_DefaultValueBinder::dataTypeForValue($pValue);
  71. }
  72. /**
  73. * Check a string that it satisfies Excel requirements
  74. *
  75. * @param mixed Value to sanitize to an Excel string
  76. * @return mixed Sanitized value
  77. */
  78. public static function checkString($pValue = null)
  79. {
  80. if ($pValue instanceof PHPExcel_RichText) {
  81. // TODO: Sanitize Rich-Text string (max. character count is 32,767)
  82. return $pValue;
  83. }
  84. // string must never be longer than 32,767 characters, truncate if necessary
  85. $pValue = PHPExcel_Shared_String::Substring($pValue, 0, 32767);
  86. // we require that newline is represented as "\n" in core, not as "\r\n" or "\r"
  87. $pValue = str_replace(array("\r\n", "\r"), "\n", $pValue);
  88. return $pValue;
  89. }
  90. /**
  91. * Check a value that it is a valid error code
  92. *
  93. * @param mixed Value to sanitize to an Excel error code
  94. * @return string Sanitized value
  95. */
  96. public static function checkErrorCode($pValue = null)
  97. {
  98. $pValue = (string) $pValue;
  99. if (!array_key_exists($pValue, self::$errorCodes)) {
  100. $pValue = '#NULL!';
  101. }
  102. return $pValue;
  103. }
  104. }