IntlGlobals.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Intl\Globals;
  11. /**
  12. * Provides fake static versions of the global functions in the intl extension.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. *
  16. * @internal
  17. */
  18. abstract class IntlGlobals
  19. {
  20. /**
  21. * Indicates that no error occurred.
  22. *
  23. * @var int
  24. */
  25. const U_ZERO_ERROR = 0;
  26. /**
  27. * Indicates that an invalid argument was passed.
  28. *
  29. * @var int
  30. */
  31. const U_ILLEGAL_ARGUMENT_ERROR = 1;
  32. /**
  33. * Indicates that the parse() operation failed.
  34. *
  35. * @var int
  36. */
  37. const U_PARSE_ERROR = 9;
  38. /**
  39. * All known error codes.
  40. *
  41. * @var array
  42. */
  43. private static $errorCodes = array(
  44. self::U_ZERO_ERROR => 'U_ZERO_ERROR',
  45. self::U_ILLEGAL_ARGUMENT_ERROR => 'U_ILLEGAL_ARGUMENT_ERROR',
  46. self::U_PARSE_ERROR => 'U_PARSE_ERROR',
  47. );
  48. /**
  49. * The error code of the last operation.
  50. *
  51. * @var int
  52. */
  53. private static $errorCode = self::U_ZERO_ERROR;
  54. /**
  55. * The error code of the last operation.
  56. *
  57. * @var int
  58. */
  59. private static $errorMessage = 'U_ZERO_ERROR';
  60. /**
  61. * Returns whether the error code indicates a failure.
  62. *
  63. * @param int $errorCode The error code returned by IntlGlobals::getErrorCode()
  64. *
  65. * @return bool
  66. */
  67. public static function isFailure($errorCode)
  68. {
  69. return isset(self::$errorCodes[$errorCode])
  70. && $errorCode > self::U_ZERO_ERROR;
  71. }
  72. /**
  73. * Returns the error code of the last operation.
  74. *
  75. * Returns IntlGlobals::U_ZERO_ERROR if no error occurred.
  76. *
  77. * @return int
  78. */
  79. public static function getErrorCode()
  80. {
  81. return self::$errorCode;
  82. }
  83. /**
  84. * Returns the error message of the last operation.
  85. *
  86. * Returns "U_ZERO_ERROR" if no error occurred.
  87. *
  88. * @return string
  89. */
  90. public static function getErrorMessage()
  91. {
  92. return self::$errorMessage;
  93. }
  94. /**
  95. * Returns the symbolic name for a given error code.
  96. *
  97. * @param int $code The error code returned by IntlGlobals::getErrorCode()
  98. *
  99. * @return string
  100. */
  101. public static function getErrorName($code)
  102. {
  103. if (isset(self::$errorCodes[$code])) {
  104. return self::$errorCodes[$code];
  105. }
  106. return '[BOGUS UErrorCode]';
  107. }
  108. /**
  109. * Sets the current error.
  110. *
  111. * @param int $code One of the error constants in this class
  112. * @param string $message The ICU class error message
  113. *
  114. * @throws \InvalidArgumentException If the code is not one of the error constants in this class
  115. */
  116. public static function setError($code, $message = '')
  117. {
  118. if (!isset(self::$errorCodes[$code])) {
  119. throw new \InvalidArgumentException(sprintf('No such error code: "%s"', $code));
  120. }
  121. self::$errorMessage = $message ? sprintf('%s: %s', $message, self::$errorCodes[$code]) : self::$errorCodes[$code];
  122. self::$errorCode = $code;
  123. }
  124. }