DataType.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Cell;
  3. use PhpOffice\PhpSpreadsheet\RichText\RichText;
  4. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  5. class DataType
  6. {
  7. // Data types
  8. const TYPE_STRING2 = 'str';
  9. const TYPE_STRING = 's';
  10. const TYPE_FORMULA = 'f';
  11. const TYPE_NUMERIC = 'n';
  12. const TYPE_BOOL = 'b';
  13. const TYPE_NULL = 'null';
  14. const TYPE_INLINE = 'inlineStr';
  15. const TYPE_ERROR = 'e';
  16. /**
  17. * List of error codes.
  18. *
  19. * @var array
  20. */
  21. private static $errorCodes = [
  22. '#NULL!' => 0,
  23. '#DIV/0!' => 1,
  24. '#VALUE!' => 2,
  25. '#REF!' => 3,
  26. '#NAME?' => 4,
  27. '#NUM!' => 5,
  28. '#N/A' => 6,
  29. ];
  30. /**
  31. * Get list of error codes.
  32. *
  33. * @return array
  34. */
  35. public static function getErrorCodes()
  36. {
  37. return self::$errorCodes;
  38. }
  39. /**
  40. * Check a string that it satisfies Excel requirements.
  41. *
  42. * @param null|RichText|string $pValue Value to sanitize to an Excel string
  43. *
  44. * @return null|RichText|string Sanitized value
  45. */
  46. public static function checkString($pValue)
  47. {
  48. if ($pValue instanceof RichText) {
  49. // TODO: Sanitize Rich-Text string (max. character count is 32,767)
  50. return $pValue;
  51. }
  52. // string must never be longer than 32,767 characters, truncate if necessary
  53. $pValue = StringHelper::substring($pValue, 0, 32767);
  54. // we require that newline is represented as "\n" in core, not as "\r\n" or "\r"
  55. $pValue = str_replace(["\r\n", "\r"], "\n", $pValue);
  56. return $pValue;
  57. }
  58. /**
  59. * Check a value that it is a valid error code.
  60. *
  61. * @param mixed $pValue Value to sanitize to an Excel error code
  62. *
  63. * @return string Sanitized value
  64. */
  65. public static function checkErrorCode($pValue)
  66. {
  67. $pValue = (string) $pValue;
  68. if (!isset(self::$errorCodes[$pValue])) {
  69. $pValue = '#NULL!';
  70. }
  71. return $pValue;
  72. }
  73. }