sfNumberFormatInfo.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <?php
  2. /**
  3. * sfNumberFormatInfo class file.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the BSD License.
  7. *
  8. * Copyright(c) 2004 by Qiang Xue. All rights reserved.
  9. *
  10. * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
  11. * The latest version of PRADO can be obtained from:
  12. * {@link http://prado.sourceforge.net/}
  13. *
  14. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  15. * @version $Id: sfNumberFormatInfo.class.php 11700 2008-09-21 10:53:44Z fabien $
  16. * @package symfony
  17. * @subpackage i18n
  18. */
  19. /**
  20. * sfNumberFormatInfo class
  21. *
  22. * Defines how numeric values are formatted and displayed,
  23. * depending on the culture. Numeric values are formatted using
  24. * standard or custom patterns stored in the properties of a
  25. * sfNumberFormatInfo.
  26. *
  27. * This class contains information, such as currency, decimal
  28. * separators, and other numeric symbols.
  29. *
  30. * To create a sfNumberFormatInfo for a specific culture,
  31. * create a sfCultureInfo for that culture and retrieve the
  32. * sfCultureInfo->NumberFormat property. Or use
  33. * sfNumberFormatInfo::getInstance($culture).
  34. * To create a sfNumberFormatInfo for the invariant culture, use the
  35. * InvariantInfo::getInvariantInfo().
  36. *
  37. *
  38. * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
  39. * @version v1.0, last update on Sun Dec 05 14:48:26 EST 2004
  40. * @package symfony
  41. * @subpackage i18n
  42. */
  43. class sfNumberFormatInfo
  44. {
  45. /**
  46. * ICU number formatting data.
  47. * @var array
  48. */
  49. protected $data = array();
  50. /**
  51. * A list of properties that are accessable/writable.
  52. * @var array
  53. */
  54. protected $properties = array();
  55. /**
  56. * The number pattern.
  57. * @var array
  58. */
  59. protected $pattern = array();
  60. const DECIMAL = 0;
  61. const CURRENCY = 1;
  62. const PERCENTAGE = 2;
  63. const SCIENTIFIC = 3;
  64. /**
  65. * Allows functions that begins with 'set' to be called directly
  66. * as an attribute/property to retrieve the value.
  67. *
  68. * @return mixed
  69. */
  70. function __get($name)
  71. {
  72. $getProperty = 'get'.$name;
  73. if (in_array($getProperty, $this->properties))
  74. {
  75. return $this->$getProperty();
  76. }
  77. else
  78. {
  79. throw new sfException(sprintf('Property %s does not exists.', $name));
  80. }
  81. }
  82. /**
  83. * Allows functions that begins with 'set' to be called directly
  84. * as an attribute/property to set the value.
  85. */
  86. function __set($name, $value)
  87. {
  88. $setProperty = 'set'.$name;
  89. if (in_array($setProperty, $this->properties))
  90. {
  91. $this->$setProperty($value);
  92. }
  93. else
  94. {
  95. throw new sfException(sprintf('Property %s can not be set.', $name));
  96. }
  97. }
  98. /**
  99. * Initializes a new writable instance of the sfNumberFormatInfo class
  100. * that is dependent on the ICU data for number, decimal, and currency
  101. * formatting information. <b>N.B.</b>You should not initialize this
  102. * class directly unless you know what you are doing. Please use use
  103. * sfNumberFormatInfo::getInstance() to create an instance.
  104. *
  105. * @param array $data ICU data for date time formatting.
  106. * @param int $type The sfNumberFormatInfo type
  107. * @see getInstance()
  108. */
  109. function __construct($data = array(), $type = sfNumberFormatInfo::DECIMAL)
  110. {
  111. $this->properties = get_class_methods($this);
  112. if (empty($data))
  113. {
  114. throw new sfException('Please provide the ICU data to initialize.');
  115. }
  116. $this->data = $data;
  117. $this->setPattern($type);
  118. }
  119. /**
  120. * Sets the pattern for a specific number pattern. The validate patterns
  121. * sfNumberFormatInfo::DECIMAL, sfNumberFormatInfo::CURRENCY,
  122. * sfNumberFormatInfo::PERCENTAGE, or sfNumberFormatInfo::SCIENTIFIC
  123. *
  124. * @param int $type pattern type.
  125. */
  126. function setPattern($type = sfNumberFormatInfo::DECIMAL)
  127. {
  128. if (is_int($type))
  129. {
  130. $this->pattern = $this->parsePattern($this->data['NumberPatterns'][$type]);
  131. }
  132. else
  133. {
  134. $this->pattern = $this->parsePattern($type);
  135. }
  136. $this->pattern['negInfty'] = $this->data['NumberElements'][6].$this->data['NumberElements'][9];
  137. $this->pattern['posInfty'] = $this->data['NumberElements'][11].$this->data['NumberElements'][9];
  138. }
  139. function getPattern()
  140. {
  141. return $this->pattern;
  142. }
  143. /**
  144. * Gets the default sfNumberFormatInfo that is culture-independent (invariant).
  145. *
  146. * @return sfNumberFormatInfo default sfNumberFormatInfo.
  147. */
  148. static public function getInvariantInfo($type = sfNumberFormatInfo::DECIMAL)
  149. {
  150. static $invariant;
  151. if (is_null($invariant))
  152. {
  153. $culture = sfCultureInfo::getInvariantCulture();
  154. $invariant = $culture->NumberFormat;
  155. $invariant->setPattern($type);
  156. }
  157. return $invariant;
  158. }
  159. /**
  160. * Returns the sfNumberFormatInfo associated with the specified culture.
  161. *
  162. * @param sfCultureInfo $culture the culture that gets the sfNumberFormat property.
  163. * @param int $type the number formatting type, it should be
  164. * sfNumberFormatInfo::DECIMAL, sfNumberFormatInfo::CURRENCY,
  165. * sfNumberFormatInfo::PERCENTAGE, or sfNumberFormatInfo::SCIENTIFIC
  166. * @return sfNumberFormatInfo sfNumberFormatInfo for the specified culture.
  167. * @see getCurrencyInstance();
  168. * @see getPercentageInstance();
  169. * @see getScientificInstance();
  170. */
  171. public static function getInstance($culture = null, $type = sfNumberFormatInfo::DECIMAL)
  172. {
  173. if ($culture instanceof sfCultureInfo)
  174. {
  175. $formatInfo = $culture->getNumberFormat();
  176. $formatInfo->setPattern($type);
  177. return $formatInfo;
  178. }
  179. else if (is_string($culture))
  180. {
  181. $sfCultureInfo = sfCultureInfo::getInstance($culture);
  182. $formatInfo = $sfCultureInfo->getNumberFormat();
  183. $formatInfo->setPattern($type);
  184. return $formatInfo;
  185. }
  186. else
  187. {
  188. $sfCultureInfo = sfCultureInfo::getInstance();
  189. $formatInfo = $sfCultureInfo->getNumberFormat();
  190. $formatInfo->setPattern($type);
  191. return $formatInfo;
  192. }
  193. }
  194. /**
  195. * Returns the currency format info associated with the specified culture.
  196. *
  197. * @param sfCultureInfo $culture the culture that gets the NumberFormat property.
  198. * @return sfNumberFormatInfo sfNumberFormatInfo for the specified culture.
  199. */
  200. public static function getCurrencyInstance($culture = null)
  201. {
  202. return self::getInstance($culture, self::CURRENCY);
  203. }
  204. /**
  205. * Returns the percentage format info associated with the specified culture.
  206. *
  207. * @param sfCultureInfo $culture the culture that gets the NumberFormat property.
  208. * @return sfNumberFormatInfo sfNumberFormatInfo for the specified culture.
  209. */
  210. public static function getPercentageInstance($culture = null)
  211. {
  212. return self::getInstance($culture, self::PERCENTAGE);
  213. }
  214. /**
  215. * Returns the scientific format info associated with the specified culture.
  216. *
  217. * @param sfCultureInfo $culture the culture that gets the NumberFormat property.
  218. * @return sfNumberFormatInfo sfNumberFormatInfo for the specified culture.
  219. */
  220. public static function getScientificInstance($culture = null)
  221. {
  222. return self::getInstance($culture, self::SCIENTIFIC);
  223. }
  224. /**
  225. * Parses the given pattern and return a list of known properties.
  226. *
  227. * @param string $pattern a number pattern.
  228. * @return array list of pattern properties.
  229. */
  230. protected function parsePattern($pattern)
  231. {
  232. $pattern = explode(';', $pattern);
  233. $negative = null;
  234. if (count($pattern) > 1)
  235. {
  236. $negative = $pattern[1];
  237. }
  238. $pattern = $pattern[0];
  239. $comma = ',';
  240. $dot = '.';
  241. $digit = '0';
  242. $hash = '#';
  243. // find the first group point, and decimal point
  244. $groupPos1 = strrpos($pattern, $comma);
  245. $decimalPos = strrpos($pattern, $dot);
  246. $groupPos2 = false;
  247. $groupSize1 = false;
  248. $groupSize2 = false;
  249. $decimalPoints = is_int($decimalPos) ? -1 : false;
  250. $info['negPref'] = $this->data['NumberElements'][6];
  251. $info['negPost'] = '';
  252. $info['negative'] = $negative;
  253. $info['positive'] = $pattern;
  254. // find the negative prefix and postfix
  255. if ($negative)
  256. {
  257. $prefixPostfix = $this->getPrePostfix($negative);
  258. $info['negPref'] = $prefixPostfix[0];
  259. $info['negPost'] = $prefixPostfix[1];
  260. }
  261. $posfix = $this->getPrePostfix($pattern);
  262. $info['posPref'] = $posfix[0];
  263. $info['posPost'] = $posfix[1];
  264. if (is_int($groupPos1))
  265. {
  266. // get the second group
  267. $groupPos2 = strrpos(substr($pattern, 0, $groupPos1), $comma);
  268. // get the number of decimal digits
  269. if (is_int($decimalPos))
  270. {
  271. $groupSize1 = $decimalPos - $groupPos1 - 1;
  272. }
  273. else
  274. {
  275. // no decimal point, so traverse from the back
  276. // to find the groupsize 1.
  277. for ($i = strlen($pattern) - 1; $i >= 0; $i--)
  278. {
  279. if ($pattern{$i} == $digit || $pattern{$i} == $hash)
  280. {
  281. $groupSize1 = $i - $groupPos1;
  282. break;
  283. }
  284. }
  285. }
  286. // get the second group size
  287. if (is_int($groupPos2))
  288. {
  289. $groupSize2 = $groupPos1 - $groupPos2 - 1;
  290. }
  291. }
  292. if (is_int($decimalPos))
  293. {
  294. for ($i = strlen($pattern) - 1; $i >= 0; $i--)
  295. {
  296. if ($pattern{$i} == $dot)
  297. {
  298. break;
  299. }
  300. if ($pattern{$i} == $digit)
  301. {
  302. $decimalPoints = $i - $decimalPos;
  303. break;
  304. }
  305. }
  306. }
  307. $digitPattern = is_int($decimalPos) ? substr($pattern, 0, $decimalPos) : $pattern;
  308. $digitPattern = preg_replace('/[^0]/', '', $digitPattern);
  309. $info['groupPos1'] = $groupPos1;
  310. $info['groupSize1'] = $groupSize1;
  311. $info['groupPos2'] = $groupPos2;
  312. $info['groupSize2'] = $groupSize2;
  313. $info['decimalPos'] = $decimalPos;
  314. $info['decimalPoints'] = $decimalPoints;
  315. $info['digitSize'] = strlen($digitPattern);
  316. return $info;
  317. }
  318. /**
  319. * Gets the prefix and postfix of a pattern.
  320. *
  321. * @param string $pattern pattern
  322. * @return array of prefix and postfix, array(prefix,postfix).
  323. */
  324. protected function getPrePostfix($pattern)
  325. {
  326. $regexp = '/[#,\.0]+/';
  327. $result = preg_split($regexp, $pattern);
  328. return array($result[0], $result[1]);
  329. }
  330. /**
  331. * Indicates the number of decimal places.
  332. *
  333. * @return int number of decimal places.
  334. */
  335. function getDecimalDigits()
  336. {
  337. return $this->pattern['decimalPoints'];
  338. }
  339. /**
  340. * Sets the number of decimal places.
  341. *
  342. * @param int $value number of decimal places.
  343. */
  344. function setDecimalDigits($value)
  345. {
  346. return $this->pattern['decimalPoints'] = $value;
  347. }
  348. /**
  349. * Indicates the digit size.
  350. *
  351. * @return int digit size.
  352. */
  353. function getDigitSize()
  354. {
  355. return $this->pattern['digitSize'];
  356. }
  357. /**
  358. * Sets the digit size.
  359. *
  360. * @param int $value digit size.
  361. */
  362. function setDigitSize($value)
  363. {
  364. $this->pattern['digitSize'] = $value;
  365. }
  366. /**
  367. * Gets the string to use as the decimal separator.
  368. *
  369. * @return string decimal separator.
  370. */
  371. function getDecimalSeparator()
  372. {
  373. return $this->data['NumberElements'][0];
  374. }
  375. /**
  376. * Sets the string to use as the decimal separator.
  377. *
  378. * @param string $value the decimal point
  379. */
  380. function setDecimalSeparator($value)
  381. {
  382. return $this->data['NumberElements'][0] = $value;
  383. }
  384. /**
  385. * Gets the string that separates groups of digits to the left
  386. * of the decimal in currency values.
  387. *
  388. * @return string currency group separator.
  389. */
  390. function getGroupSeparator()
  391. {
  392. return $this->data['NumberElements'][1];
  393. }
  394. /**
  395. * Sets the string to use as the group separator.
  396. *
  397. * @param string $value the group separator.
  398. */
  399. function setGroupSeparator($value)
  400. {
  401. return $this->data['NumberElements'][1] = $value;
  402. }
  403. /**
  404. * Gets the number of digits in each group to the left of the decimal
  405. * There can be two grouping sizes, this fucntion
  406. * returns <b>array(group1, group2)</b>, if there is only 1 grouping size,
  407. * group2 will be false.
  408. *
  409. * @return array grouping size(s).
  410. */
  411. function getGroupSizes()
  412. {
  413. $group1 = $this->pattern['groupSize1'];
  414. $group2 = $this->pattern['groupSize2'];
  415. return array($group1, $group2);
  416. }
  417. /**
  418. * Sets the number of digits in each group to the left of the decimal.
  419. * There can be two grouping sizes, the value should
  420. * be an <b>array(group1, group2)</b>, if there is only 1 grouping size,
  421. * group2 should be false.
  422. *
  423. * @param array $groupSize grouping size(s).
  424. */
  425. function setGroupSizes($groupSize)
  426. {
  427. $this->pattern['groupSize1'] = $groupSize[0];
  428. $this->pattern['groupSize2'] = $groupSize[1];
  429. }
  430. /**
  431. * Gets the format pattern for negative values.
  432. * The negative pattern is composed of a prefix, and postfix.
  433. * This function returns <b>array(prefix, postfix)</b>.
  434. *
  435. * @return arary negative pattern.
  436. */
  437. function getNegativePattern()
  438. {
  439. $prefix = $this->pattern['negPref'];
  440. $postfix = $this->pattern['negPost'];
  441. return array($prefix, $postfix);
  442. }
  443. /**
  444. * Sets the format pattern for negative values.
  445. * The negative pattern is composed of a prefix, and postfix in the form
  446. * <b>array(prefix, postfix)</b>.
  447. *
  448. * @param arary $pattern negative pattern.
  449. */
  450. function setNegativePattern($pattern)
  451. {
  452. $this->pattern['negPref'] = $pattern[0];
  453. $this->pattern['negPost'] = $pattern[1];
  454. }
  455. /**
  456. * Gets the format pattern for positive values.
  457. * The positive pattern is composed of a prefix, and postfix.
  458. * This function returns <b>array(prefix, postfix)</b>.
  459. *
  460. * @return arary positive pattern.
  461. */
  462. function getPositivePattern()
  463. {
  464. $prefix = $this->pattern['posPref'];
  465. $postfix = $this->pattern['posPost'];
  466. return array($prefix, $postfix);
  467. }
  468. /**
  469. * Sets the format pattern for positive values.
  470. * The positive pattern is composed of a prefix, and postfix in the form
  471. * <b>array(prefix, postfix)</b>.
  472. *
  473. * @param arary $pattern positive pattern.
  474. */
  475. function setPositivePattern($pattern)
  476. {
  477. $this->pattern['posPref'] = $pattern[0];
  478. $this->pattern['posPost'] = $pattern[1];
  479. }
  480. /**
  481. * Gets the string to use as the currency symbol.
  482. *
  483. * @return string $currency currency symbol.
  484. */
  485. function getCurrencySymbol($currency = 'USD')
  486. {
  487. if (isset($this->pattern['symbol']))
  488. {
  489. return $this->pattern['symbol'];
  490. }
  491. else
  492. {
  493. return $this->data['Currencies'][$currency][0];
  494. }
  495. }
  496. /**
  497. * Sets the string to use as the currency symbol.
  498. *
  499. * @param string $symbol currency symbol.
  500. */
  501. function setCurrencySymbol($symbol)
  502. {
  503. $this->pattern['symbol'] = $symbol;
  504. }
  505. /**
  506. * Gets the string that represents negative infinity.
  507. *
  508. * @return string negative infinity.
  509. */
  510. function getNegativeInfinitySymbol()
  511. {
  512. return $this->pattern['negInfty'];
  513. }
  514. /**
  515. * Sets the string that represents negative infinity.
  516. *
  517. * @param string $value negative infinity.
  518. */
  519. function setNegativeInfinitySymbol($value)
  520. {
  521. $this->pattern['negInfty'] = $value;
  522. }
  523. /**
  524. * Gets the string that represents positive infinity.
  525. *
  526. * @return string positive infinity.
  527. */
  528. function getPositiveInfinitySymbol()
  529. {
  530. return $this->pattern['posInfty'];
  531. }
  532. /**
  533. * Sets the string that represents positive infinity.
  534. *
  535. * @param string $value positive infinity.
  536. */
  537. function setPositiveInfinitySymbol($value)
  538. {
  539. $this->pattern['posInfty'] = $value;
  540. }
  541. /**
  542. * Gets the string that denotes that the associated number is negative.
  543. *
  544. * @return string negative sign.
  545. */
  546. function getNegativeSign()
  547. {
  548. return $this->data['NumberElements'][6];
  549. }
  550. /**
  551. * Sets the string that denotes that the associated number is negative.
  552. *
  553. * @param string $value negative sign.
  554. */
  555. function setNegativeSign($value)
  556. {
  557. $this->data['NumberElements'][6] = $value;
  558. }
  559. /**
  560. * Gets the string that denotes that the associated number is positive.
  561. *
  562. * @return string positive sign.
  563. */
  564. function getPositiveSign()
  565. {
  566. return $this->data['NumberElements'][11];
  567. }
  568. /**
  569. * Sets the string that denotes that the associated number is positive.
  570. *
  571. * @param string $value positive sign.
  572. */
  573. function setPositiveSign($value)
  574. {
  575. $this->data['NumberElements'][11] = $value;
  576. }
  577. /**
  578. * Gets the string that represents the IEEE NaN (not a number) value.
  579. *
  580. * @return string NaN symbol.
  581. */
  582. function getNaNSymbol()
  583. {
  584. return $this->data['NumberElements'][10];
  585. }
  586. /**
  587. * Sets the string that represents the IEEE NaN (not a number) value.
  588. *
  589. * @param string $value NaN symbol.
  590. */
  591. function setNaNSymbol($value)
  592. {
  593. $this->data['NumberElements'][10] = $value;
  594. }
  595. /**
  596. * Gets the string to use as the percent symbol.
  597. *
  598. * @return string percent symbol.
  599. */
  600. function getPercentSymbol()
  601. {
  602. return $this->data['NumberElements'][3];
  603. }
  604. /**
  605. * Sets the string to use as the percent symbol.
  606. *
  607. * @param string $value percent symbol.
  608. */
  609. function setPercentSymbol($value)
  610. {
  611. $this->data['NumberElements'][3] = $value;
  612. }
  613. /**
  614. * Gets the string to use as the per mille symbol.
  615. *
  616. * @return string percent symbol.
  617. */
  618. function getPerMilleSymbol()
  619. {
  620. return $this->data['NumberElements'][8];
  621. }
  622. /**
  623. * Sets the string to use as the per mille symbol.
  624. *
  625. * @param string $value percent symbol.
  626. */
  627. function setPerMilleSymbol($value)
  628. {
  629. $this->data['NumberElements'][8] = $value;
  630. }
  631. }