DataType.Class.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/8/11
  6. * Time: 5:58 PM
  7. */
  8. namespace Util\PHPExcel\Cell;
  9. use Util\PHPExcel\RichText;
  10. use Util\PHPExcel\Shared\PHPExcelString;
  11. class DataType
  12. {
  13. /* Data types */
  14. const TYPE_STRING2 = 'str';
  15. const TYPE_STRING = 's';
  16. const TYPE_FORMULA = 'f';
  17. const TYPE_NUMERIC = 'n';
  18. const TYPE_BOOL = 'b';
  19. const TYPE_NULL = 'null';
  20. const TYPE_INLINE = 'inlineStr';
  21. const TYPE_ERROR = 'e';
  22. /**
  23. * List of error codes
  24. *
  25. * @var array
  26. */
  27. private static $errorCodes = array(
  28. '#NULL!' => 0,
  29. '#DIV/0!' => 1,
  30. '#VALUE!' => 2,
  31. '#REF!' => 3,
  32. '#NAME?' => 4,
  33. '#NUM!' => 5,
  34. '#N/A' => 6
  35. );
  36. /**
  37. * Get list of error codes
  38. *
  39. * @return array
  40. */
  41. public static function getErrorCodes()
  42. {
  43. return self::$errorCodes;
  44. }
  45. /**
  46. * DataType for value
  47. *
  48. * @deprecated Replaced by PHPExcel_Cell_IValueBinder infrastructure, will be removed in version 1.8.0
  49. * @param mixed $pValue
  50. * @return string
  51. */
  52. public static function dataTypeForValue($pValue = null)
  53. {
  54. return PHPExcel_Cell_DefaultValueBinder::dataTypeForValue($pValue);
  55. }
  56. /**
  57. * Check a string that it satisfies Excel requirements
  58. *
  59. * @param mixed Value to sanitize to an Excel string
  60. * @return mixed Sanitized value
  61. */
  62. public static function checkString($pValue = null)
  63. {
  64. if ($pValue instanceof RichText) {
  65. // TODO: Sanitize Rich-Text string (max. character count is 32,767)
  66. return $pValue;
  67. }
  68. // string must never be longer than 32,767 characters, truncate if necessary
  69. $pValue = PHPExcelString::Substring($pValue, 0, 32767);
  70. // we require that newline is represented as "\n" in core, not as "\r\n" or "\r"
  71. $pValue = str_replace(array("\r\n", "\r"), "\n", $pValue);
  72. return $pValue;
  73. }
  74. /**
  75. * Check a value that it is a valid error code
  76. *
  77. * @param mixed Value to sanitize to an Excel error code
  78. * @return string Sanitized value
  79. */
  80. public static function checkErrorCode($pValue = null)
  81. {
  82. $pValue = (string) $pValue;
  83. if (!array_key_exists($pValue, self::$errorCodes)) {
  84. $pValue = '#NULL!';
  85. }
  86. return $pValue;
  87. }
  88. }