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