PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/www/libs/Zend/I18n/View/Helper/NumberFormat.php

https://bitbucket.org/Ppito/kawaiviewmodel2
PHP | 166 lines | 72 code | 17 blank | 77 comment | 7 complexity | 1e3155fddc8fb7ddc5cbd18d7c091694 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_I18n
  9. */
  10. namespace Zend\I18n\View\Helper;
  11. use Locale;
  12. use NumberFormatter;
  13. use Zend\View\Helper\AbstractHelper;
  14. /**
  15. * View helper for formatting dates.
  16. *
  17. * @category Zend
  18. * @package Zend_I18n
  19. * @subpackage View
  20. */
  21. class NumberFormat extends AbstractHelper
  22. {
  23. /**
  24. * Locale to use instead of the default.
  25. *
  26. * @var string
  27. */
  28. protected $locale;
  29. /**
  30. * NumberFormat style to use.
  31. *
  32. * @var integer
  33. */
  34. protected $formatStyle;
  35. /**
  36. * NumberFormat type to use.
  37. *
  38. * @var integer
  39. */
  40. protected $formatType;
  41. /**
  42. * Formatter instances.
  43. *
  44. * @var array
  45. */
  46. protected $formatters = array();
  47. /**
  48. * Set format style to use instead of the default.
  49. *
  50. * @param integer $formatStyle
  51. * @return NumberFormat
  52. */
  53. public function setFormatStyle($formatStyle)
  54. {
  55. $this->formatStyle = (int) $formatStyle;
  56. return $this;
  57. }
  58. /**
  59. * Get the format style to use.
  60. *
  61. * @return integer
  62. */
  63. public function getFormatStyle()
  64. {
  65. if (null === $this->formatStyle) {
  66. $this->formatStyle = NumberFormatter::DECIMAL;
  67. }
  68. return $this->formatStyle;
  69. }
  70. /**
  71. * Set format type to use instead of the default.
  72. *
  73. * @param integer $formatType
  74. * @return NumberFormat
  75. */
  76. public function setFormatType($formatType)
  77. {
  78. $this->formatType = (int) $formatType;
  79. return $this;
  80. }
  81. /**
  82. * Get the format type to use.
  83. *
  84. * @return integer
  85. */
  86. public function getFormatType()
  87. {
  88. if (null === $this->formatType) {
  89. $this->formatType = NumberFormatter::TYPE_DEFAULT;
  90. }
  91. return $this->formatType;
  92. }
  93. /**
  94. * Set locale to use instead of the default.
  95. *
  96. * @param string $locale
  97. * @return NumberFormat
  98. */
  99. public function setLocale($locale)
  100. {
  101. $this->locale = (string) $locale;
  102. return $this;
  103. }
  104. /**
  105. * Get the locale to use.
  106. *
  107. * @return string|null
  108. */
  109. public function getLocale()
  110. {
  111. if ($this->locale === null) {
  112. $this->locale = Locale::getDefault();
  113. }
  114. return $this->locale;
  115. }
  116. /**
  117. * Format a number.
  118. *
  119. * @param integer|float $number
  120. * @param integer $formatStyle
  121. * @param integer $formatType
  122. * @param string $locale
  123. * @return string
  124. */
  125. public function __invoke(
  126. $number,
  127. $formatStyle = null,
  128. $formatType = null,
  129. $locale = null
  130. ) {
  131. if (null === $locale) {
  132. $locale = $this->getLocale();
  133. }
  134. if (null === $formatStyle) {
  135. $formatStyle = $this->getFormatStyle();
  136. }
  137. if (null === $formatType) {
  138. $formatType = $this->getFormatType();
  139. }
  140. $formatterId = md5($formatStyle . "\0" . $locale);
  141. if (!isset($this->formatters[$formatterId])) {
  142. $this->formatters[$formatterId] = new NumberFormatter(
  143. $locale,
  144. $formatStyle
  145. );
  146. }
  147. return $this->formatters[$formatterId]->format($number, $formatType);
  148. }
  149. }