formatter_api.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. /**
  3. * Number formatter class - locale-dependent number formatting/parsing.
  4. *
  5. * This class represents the ICU number formatting functionality. It allows to display
  6. * number according to the localized format or given pattern or set of rules, and to
  7. * parse strings into numbers according to the above patterns.
  8. *
  9. * Example:
  10. * <code>
  11. * $value = 1234567;
  12. * $formatter = new NumberFormatter("de_DE", NumberFormatter::DECIMAL);
  13. * echo $formatter->format($value);
  14. * </code>
  15. *
  16. * @see http://www.icu-project.org/apiref/icu4c/unum_8h.html
  17. * @see http://www.icu-project.org/apiref/icu4c/classNumberFormat.html
  18. *
  19. * The class would also contain all the constants listed in the following enums:
  20. * UNumberFormatStyle, UNumberFormatRoundingMode, UNumberFormatPadPosition,
  21. * UNumberFormatAttribute, UNumberFormatTextAttribute, UNumberFormatSymbol.
  22. */
  23. class NumberFormatter {
  24. #############################################################################
  25. # Common constants.
  26. #############################################################################
  27. /*
  28. * WARNING:
  29. * The values described here are NOT the actual values in PHP code.
  30. * They are references to the ICU C definitions, so the line
  31. * const PATTERN_DECIMAL = 'UNUM_PATTERN_DECIMAL';
  32. * actually means that NumberFormatter::PATTERN_DECIMAL is the same as
  33. * UNUM_PATTERN_DECIMAL constant in the ICU library.
  34. */
  35. /*
  36. * These constants define formatter/parser argument type - integer, floating point or currency.
  37. */
  38. const TYPE_DEFAULT = 'FORMAT_TYPE_DEFAULT';
  39. const TYPE_INT32 = 'FORMAT_TYPE_INT32';
  40. const TYPE_INT64 = 'FORMAT_TYPE_INT64';
  41. const TYPE_DOUBLE = 'FORMAT_TYPE_DOUBLE';
  42. const TYPE_CURRENCY = 'FORMAT_TYPE_CURRENCY';
  43. /*
  44. * UNumberFormatStyle constants
  45. */
  46. const PATTERN_DECIMAL = 'UNUM_PATTERN_DECIMAL';
  47. const DECIMAL = 'UNUM_DECIMAL';
  48. const CURRENCY = 'UNUM_CURRENCY';
  49. const PERCENT = 'UNUM_PERCENT';
  50. const SCIENTIFIC = 'UNUM_SCIENTIFIC';
  51. const SPELLOUT = 'UNUM_SPELLOUT';
  52. const ORDINAL = 'UNUM_ORDINAL';
  53. const DURATION = 'UNUM_DURATION';
  54. const PATTERN_RULEBASED = 'UNUM_PATTERN_RULEBASED';
  55. const DEFAULT = 'UNUM_DEFAULT';
  56. const IGNORE = 'UNUM_IGNORE';
  57. /*
  58. * UNumberFormatRoundingMode
  59. */
  60. const ROUND_CEILING = 'UNUM_ROUND_CEILING';
  61. const ROUND_FLOOR = 'UNUM_ROUND_FLOOR';
  62. const ROUND_DOWN = 'UNUM_ROUND_DOWN';
  63. const ROUND_UP = 'UNUM_ROUND_UP';
  64. const ROUND_HALFEVEN = 'UNUM_ROUND_HALFEVEN';
  65. const ROUND_HALFDOWN = 'UNUM_ROUND_HALFDOWN';
  66. const ROUND_HALFUP = 'UNUM_ROUND_HALFUP';
  67. /*
  68. * UNumberFormatPadPosition
  69. */
  70. const PAD_BEFORE_PREFIX = 'UNUM_PAD_BEFORE_PREFIX';
  71. const PAD_AFTER_PREFIX = 'UNUM_PAD_AFTER_PREFIX';
  72. const PAD_BEFORE_SUFFIX = 'UNUM_PAD_BEFORE_SUFFIX';
  73. const PAD_AFTER_SUFFIX = 'UNUM_PAD_AFTER_SUFFIX';
  74. /*
  75. * UNumberFormatAttribute
  76. */
  77. const PARSE_INT_ONLY = 'UNUM_PARSE_INT_ONLY';
  78. const GROUPING_USED = 'UNUM_GROUPING_USED';
  79. const DECIMAL_ALWAYS_SHOWN = 'UNUM_DECIMAL_ALWAYS_SHOWN';
  80. const MAX_INTEGER_DIGITS = 'UNUM_MAX_INTEGER_DIGITS';
  81. const MIN_INTEGER_DIGITS = 'UNUM_MIN_INTEGER_DIGITS';
  82. const INTEGER_DIGITS = 'UNUM_INTEGER_DIGITS';
  83. const MAX_FRACTION_DIGITS = 'UNUM_MAX_FRACTION_DIGITS';
  84. const MIN_FRACTION_DIGITS = 'UNUM_MIN_FRACTION_DIGITS';
  85. const FRACTION_DIGITS = 'UNUM_FRACTION_DIGITS';
  86. const MULTIPLIER = 'UNUM_MULTIPLIER';
  87. const GROUPING_SIZE = 'UNUM_GROUPING_SIZE';
  88. const ROUNDING_MODE = 'UNUM_ROUNDING_MODE';
  89. const ROUNDING_INCREMENT = 'UNUM_ROUNDING_INCREMENT';
  90. const FORMAT_WIDTH = 'UNUM_FORMAT_WIDTH';
  91. const PADDING_POSITION = 'UNUM_PADDING_POSITION';
  92. const SECONDARY_GROUPING_SIZE = 'UNUM_SECONDARY_GROUPING_SIZE';
  93. const SIGNIFICANT_DIGITS_USED = 'UNUM_SIGNIFICANT_DIGITS_USED';
  94. const MIN_SIGNIFICANT_DIGITS = 'UNUM_MIN_SIGNIFICANT_DIGITS';
  95. const MAX_SIGNIFICANT_DIGITS = 'UNUM_MAX_SIGNIFICANT_DIGITS';
  96. const LENIENT_PARSE = 'UNUM_LENIENT_PARSE';
  97. /*
  98. * UNumberFormatTextAttribute
  99. */
  100. const POSITIVE_PREFIX = 'UNUM_POSITIVE_PREFIX';
  101. const POSITIVE_SUFFIX = 'UNUM_POSITIVE_SUFFIX';
  102. const NEGATIVE_PREFIX = 'UNUM_NEGATIVE_PREFIX';
  103. const NEGATIVE_SUFFIX = 'UNUM_NEGATIVE_SUFFIX';
  104. const PADDING_CHARACTER = 'UNUM_PADDING_CHARACTER';
  105. const CURRENCY_CODE = 'UNUM_CURRENCY_CODE';
  106. const DEFAULT_RULESET = 'UNUM_DEFAULT_RULESET';
  107. const PUBLIC_RULESETS = 'UNUM_PUBLIC_RULESETS';
  108. /*
  109. * UNumberFormatSymbol
  110. */
  111. const DECIMAL_SEPARATOR_SYMBOL = 'UNUM_DECIMAL_SEPARATOR_SYMBOL';
  112. const GROUPING_SEPARATOR_SYMBOL = 'UNUM_GROUPING_SEPARATOR_SYMBOL';
  113. const PATTERN_SEPARATOR_SYMBOL = 'UNUM_PATTERN_SEPARATOR_SYMBOL';
  114. const PERCENT_SYMBOL = 'UNUM_PERCENT_SYMBOL';
  115. const ZERO_DIGIT_SYMBOL = 'UNUM_ZERO_DIGIT_SYMBOL';
  116. const DIGIT_SYMBOL = 'UNUM_DIGIT_SYMBOL';
  117. const MINUS_SIGN_SYMBOL = 'UNUM_MINUS_SIGN_SYMBOL';
  118. const PLUS_SIGN_SYMBOL = 'UNUM_PLUS_SIGN_SYMBOL';
  119. const CURRENCY_SYMBOL = 'UNUM_CURRENCY_SYMBOL';
  120. const INTL_CURRENCY_SYMBOL = 'UNUM_INTL_CURRENCY_SYMBOL';
  121. const MONETARY_SEPARATOR_SYMBOL = 'UNUM_MONETARY_SEPARATOR_SYMBOL';
  122. const EXPONENTIAL_SYMBOL = 'UNUM_EXPONENTIAL_SYMBOL';
  123. const PERMILL_SYMBOL = 'UNUM_PERMILL_SYMBOL';
  124. const PAD_ESCAPE_SYMBOL = 'UNUM_PAD_ESCAPE_SYMBOL';
  125. const INFINITY_SYMBOL = 'UNUM_INFINITY_SYMBOL';
  126. const NAN_SYMBOL = 'UNUM_NAN_SYMBOL';
  127. const SIGNIFICANT_DIGIT_SYMBOL = 'UNUM_SIGNIFICANT_DIGIT_SYMBOL';
  128. const MONETARY_GROUPING_SEPARATOR_SYMBOL = 'UNUM_MONETARY_GROUPING_SEPARATOR_SYMBOL';
  129. /**
  130. * Create a number formatter
  131. *
  132. * Creates a number formatter from locale and pattern. This formatter would be used to
  133. * format or parse numbers.
  134. *
  135. * @param integer $style Style of the formatting, one of the UNumberFormatStyle constants
  136. * @param string $locale Locale in which the number would be formatted
  137. * @param [string] $pattern Pattern string in case chose style requires pattern
  138. * @return NumberFormatter
  139. */
  140. public function __construct($locale, $style, $pattern = null) {}
  141. /**
  142. * Create a number formatter
  143. *
  144. * Creates a number formatter from locale and pattern. This formatter would be used to
  145. * format or parse numbers.
  146. *
  147. * This method is useful when you prefer just to get null on error,
  148. * as if you called numfmt_create().
  149. *
  150. * @param integer $style Style of the formatting, one of the UNumberFormatStyle constants
  151. * @param string $locale Locale in which the number would be formatted
  152. * @param [string] $pattern Pattern string in case chose style requires pattern
  153. * @return NumberFormatter
  154. * @see __construct
  155. * @see numfmt_create
  156. */
  157. public static function create($locale, $style, $pattern = null) {}
  158. /**
  159. * Format a number according to current formatting rules.
  160. *
  161. * If the type is not specified, the type is derived from the $number parameter. I.e., if it's
  162. * integer then INT32 would be chosen on 32-bit, INT64 on 64-bit, if it's double, DOUBLE would be
  163. * chosen. It is possible to format 64-bit number on 32-bit machine by passing it as double and using
  164. * TYPE_INT64.
  165. * When formatting currency, default formatter's currency code is used.
  166. *
  167. * @param integer|double $number Number to format
  168. * @param [integer] $type Type of the formatting - one of TYPE constants. If not specified, default for the type.
  169. * @return string formatted number
  170. */
  171. public function format($number, $type = 0) {}
  172. /**
  173. * Parse a number according to current formatting rules.
  174. *
  175. * @param string $string String to parse
  176. * @param [integer] $type Type of the formatting - one of TYPE constants.
  177. * TYPE_DOUBLE is used by default.
  178. * @param [integer] $position On input, the position to start parsing, default is 0;
  179. * on output, moved to after the last successfully parse character;
  180. * on parse failure, does not change.
  181. * @return integer|double|false Parsed number, false if parsing failed
  182. */
  183. public function parse($string, $type, &$position) {}
  184. /**
  185. * Format number as currency.
  186. *
  187. * Uses user-defined currency string.
  188. *
  189. * @param double $number Number to format
  190. * @param string $currency 3-letter currency code (ISO 4217) to use in format
  191. */
  192. public function formatCurrency($number, $currency) {}
  193. /**
  194. * Parse currency string
  195. *
  196. * This parser would use parseCurrency API string to parse currency string. The format is defined by the
  197. * formatter, returns both number and currency code.
  198. *
  199. * @param string $string String to parse
  200. * @param string $currency Parameter to return parsed currency code
  201. * @param [integer] $position On input, the position within text to match, default is 0;
  202. * on output, the position after the last matched character;
  203. * on parse failure, does not change.
  204. * @return double currency number
  205. */
  206. public function parseCurrency($string, &$currency, &$position) {}
  207. /**
  208. * Set formatter attribute.
  209. *
  210. * This function is used to set any of the formatter attributes. Example:
  211. *
  212. * $formatter->setAttribute(NumberFormat::FORMAT_WIDTH, 10);
  213. *
  214. * @param integer $attr One of UNumberFormatAttribute constants
  215. * @param integer|double $value Value of the attribute
  216. * @return false if attribute is unknown or can not be set, true otherwise
  217. */
  218. public function setAttribute($attr, $value) {}
  219. /**
  220. * Set formatter attribute.
  221. *
  222. * This function is used to set any of the formatter attributes. Example:
  223. *
  224. * $formatter->setTextAttribute(NumberFormat::POSITIVE_PREFIX, "+");
  225. *
  226. * @param integer $attr One of UNumberFormatTextAttribute constants
  227. * @param string $value Value of the attribute
  228. * @return false if attribute is unknown or can not be set, true otherwise
  229. */
  230. public function setTextAttribute($attr, $value) {}
  231. /**
  232. * Set formatting symbol.
  233. *
  234. * Example:
  235. *
  236. * $formatter->setSymbol(NumberFormat::EXPONENTIAL_SYMBOL, "E");
  237. *
  238. * @param integer|array $attr One of UNumberFormatSymbol constants or array of symbols, indexed by
  239. * these constants
  240. * @param string $value Value of the symbol
  241. */
  242. public function setSymbol($attr, $value) {}
  243. /**
  244. * Set pattern used by the formatter
  245. *
  246. * Valid only if the formatter is using pattern and is not rule-based.
  247. * @see http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html
  248. * Localized patterns are not currently supported.
  249. *
  250. * @param string $pattern The pattern to be used.
  251. * @return boolean false if formatter pattern could not be set, true otherwise
  252. */
  253. public function setPattern($pattern) {}
  254. /**
  255. * Get value of the formatter attribute
  256. *
  257. * @param integer $attr One of UNumberFormatAttribute constants
  258. * @return integer|double value of the attribute or false if the value can not be obtained
  259. */
  260. public function getAttribute($attr) {}
  261. /**
  262. * Get value of the formatter attribute
  263. *
  264. * @param integer $attr One of UNumberFormatTextAttribute constants
  265. * @return string value of the attribute or false if the value can not be obtained
  266. */
  267. public function getTextAttribute($attr) {}
  268. /**
  269. * Get value of the formatter symbol
  270. *
  271. * @param integer $attr One of UNumberFormatSymbol constants specifying the symbol
  272. * @return string|false The symbol value, or false if the value can not be obtained
  273. */
  274. public function getSymbol($attr) {}
  275. /**
  276. * Get pattern used by the formatter.
  277. *
  278. * Gets current state of the formatter as a pattern.
  279. * Localized patterns are not currently supported.
  280. *
  281. * Valid only if the formatter is UNUM_PATTERN_DECIMAL
  282. * @return string|false The pattern used by the formatter or false if formatter is of a type
  283. * that does not support patterns.
  284. */
  285. public function getPattern() {}
  286. /**
  287. * Get the locale for which the formatter was created.
  288. *
  289. * @param [integer] $type One of ULocDataLocaleType values
  290. * @return string locale name
  291. */
  292. public function getLocale($type = 0) {}
  293. /**
  294. * Get the error code from last operation
  295. *
  296. * Returns error code from the last number formatting operation.
  297. *
  298. * @return integer the error code, one of UErrorCode values. Initial value is U_ZERO_ERROR.
  299. */
  300. public function getErrorCode() {}
  301. /**
  302. * Get the error text from the last operation.
  303. *
  304. * @return string Description of the last occurred error.
  305. */
  306. public public function getErrorMessage() {}
  307. }
  308. /** Now the same as procedural API */
  309. /**
  310. * Create a number formatter
  311. *
  312. * Creates a number formatter from locale and pattern. This formatter would be used to
  313. * format or parse numbers.
  314. *
  315. * @param string $locale Locale in which the number would be formatted
  316. * @param integer $style Style of the formatting, one of the UNumberFormatStyle constants
  317. * @param [string] $pattern Pattern string in case chose style requires pattern
  318. * @return Numberformatter resource NumberFormatter
  319. */
  320. function numfmt_create($locale, $style, $pattern = null) {}
  321. /**
  322. * Format a number according to current formatting rules.
  323. *
  324. * If the type is not specified, the type is derived from the $number parameter. I.e., if it's
  325. * integer then INT32 would be chosen on 32-bit, INT64 on 64-bit, if it's double, DOUBLE would be
  326. * chosen. It is possible to format 64-bit number on 32-bit machine by passing it as double and using
  327. * TYPE_INT64.
  328. *
  329. * @param NumberFormatter $formatter The formatter resource
  330. * @param integer|double $number Number to format
  331. * @param [integer] $type Type of the formatting - one of TYPE constants. If not specified, default for the type.
  332. * @return string formatted number
  333. */
  334. function numfmt_format($formatter, $number, $type = null) {}
  335. /**
  336. * Parse a number according to current formatting rules.
  337. *
  338. * This parser uses DOUBLE type by default. When parsing currency,
  339. * default currency definitions are used.
  340. *
  341. * @param NumberFormatter $formatter The formatter resource
  342. * @param string $string String to parse
  343. * @param [integer] $type Type of the formatting - one of TYPE constants.
  344. * @param [integer] $position String position after the end of parsed data.
  345. * @return integer|double|false Parsed number, false if parsing failed
  346. */
  347. function numfmt_parse($formatter, $string, $type, &$position) {}
  348. /**
  349. * Format number as currency.
  350. *
  351. * Uses user-defined currency string.
  352. *
  353. * @param NumberFormatter $formatter The formatter resource
  354. * @param double $number Number to format
  355. * @param string $currency 3-letter currency code (ISO 4217) to use in format
  356. */
  357. function numfmt_format_currency($formatter, $number, $currency) {}
  358. /**
  359. * Parse currency string
  360. *
  361. * This parser would use parseCurrency API string to parse currency string. The format is defined by the
  362. * formatter, returns both number and currency code.
  363. *
  364. * @param NumberFormatter $formatter The formatter resource
  365. * @param string $string String to parse
  366. * @param string $currency Parameter to return parsed currency code
  367. * @param [integer] $position String position after the end of parsed data.
  368. * @return double currency number
  369. */
  370. function numfmt_parse_currency($formatter, $string, &$currency, &$position) {}
  371. /**
  372. * Set formatter attribute.
  373. *
  374. * This function is used to set any of the formatter attributes. Example:
  375. *
  376. * numfmt_format_set_attribute($formatter, NumberFormat::FORMAT_WIDTH, 10);
  377. *
  378. * @param NumberFormatter $formatter The formatter resource
  379. * @param integer $attr One of UNumberFormatAttribute constants
  380. * @param integer|double $value Value of the attribute
  381. * @return false if attribute is unknown or can not be set, true otherwise
  382. */
  383. function numfmt_set_attribute($formatter, $attribute, $value) {}
  384. /**
  385. * Set formatter attribute.
  386. *
  387. * This function is used to set any of the formatter attributes. Example:
  388. *
  389. * numfmt_format_set_text_attribute($formatter, NumberFormat::POSITIVE_PREFIX, "+");
  390. *
  391. * @param NumberFormatter $formatter The formatter resource
  392. * @param integer $attr One of UNumberFormatTextAttribute constants
  393. * @param string $value Value of the attribute
  394. * @return false if attribute is unknown or can not be set, true otherwise
  395. */
  396. function numfmt_set_text_attribute($formatter, $attribute, $value) {}
  397. /**
  398. * Set formatting symbol.
  399. *
  400. * Example:
  401. *
  402. * $formatter->setSymbol(NumberFormat::EXPONENTIAL_SYMBOL, "E");
  403. *
  404. * @param NumberFormatter $formatter The formatter resource
  405. * @param integer|array $attr One of UNumberFormatSymbol constants or array of symbols,
  406. * indexed by these constants
  407. * @param string $value Value of the symbol
  408. */
  409. function numfmt_set_symbol($formatter, $attribute, $value) {}
  410. /**
  411. * Set pattern used by the formatter
  412. *
  413. * Valid only if the formatter is using pattern and is not rule-based.
  414. * @see http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html
  415. * Localized patterns are not currently supported.
  416. *
  417. * @param NumberFormatter $formatter The formatter resource
  418. * @param string $pattern The pattern to be used.
  419. * @return boolean false if formatter pattern could not be set, true otherwise
  420. */
  421. function numfmt_set_pattern($formatter, $pattern) {}
  422. /**
  423. * Get value of the formatter attribute
  424. *
  425. * @param NumberFormatter $formatter The formatter resource
  426. * @param integer $attribute One of UNumberFormatAttribute constants
  427. * @return integer|double value of the attribute or false if the value can not be obtained
  428. */
  429. function numfmt_get_attribute($formatter, $attribute) {}
  430. /**
  431. * Get value of the formatter attribute
  432. *
  433. * @param NumberFormatter $formatter The formatter resource
  434. * @param integer $attribute One of UNumberFormatTextAttribute constants
  435. * @return string value of the attribute or false if the value can not be obtained
  436. */
  437. function numfmt_get_text_attribute($formatter, $attribute) {}
  438. /**
  439. * Get value of the formatter symbol
  440. *
  441. * @param NumberFormatter $formatter The formatter resource
  442. * @param integer $attribute One of UNumberFormatSymbol constants specifying the symbol
  443. * @return string|false The symbol value, or false if the value can not be obtained
  444. */
  445. function numfmt_get_symbol($formatter, $attribute) {}
  446. /**
  447. * Get pattern used by the formatter.
  448. *
  449. * Gets current state of the formatter as a pattern.
  450. * Localized patterns are not currently supported.
  451. *
  452. * Valid only if the formatter is UNUM_PATTERN_DECIMAL
  453. * @param NumberFormatter $formatter The formatter resource
  454. * @return string|false The pattern used by the formatter or false if formatter is of a type
  455. * that does not support patterns.
  456. */
  457. function numfmt_get_pattern($formatter) {}
  458. /**
  459. * Get the locale for which the formatter was created.
  460. *
  461. * @param NumberFormatter $formatter The formatter resource
  462. * @param [integer] $type One of ULocDataLocaleType values
  463. * @return string locale name
  464. */
  465. function numfmt_get_locale($formatter, $type = 0) {}
  466. /**
  467. * Get the error code from last operation
  468. *
  469. * Returns error code from the last number formatting operation.
  470. *
  471. * @param NumberFormatter $formatter The formatter resource
  472. * @return integer the error code, one of UErrorCode values. Initial value is U_ZERO_ERROR.
  473. */
  474. function numfmt_get_error_code($formatter) {}
  475. /**
  476. * Get the error text from the last operation.
  477. *
  478. * @param NumberFormatter $formatter The formatter resource
  479. * @return string Description of the last occurred error.
  480. */
  481. function numfmt_get_error_message($formatter) {}