PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/I18n/Filter/NumberFormat.php

https://bitbucket.org/pcelta/zf2
PHP | 168 lines | 95 code | 19 blank | 54 comment | 10 complexity | 9ecbbb0ff78a119a32e222adef0b2e66 MD5 | raw file
  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\Filter;
  11. use NumberFormatter;
  12. use Traversable;
  13. use Zend\I18n\Exception;
  14. use Zend\Stdlib\ErrorHandler;
  15. /**
  16. * @category Zend
  17. * @package Zend_I18n
  18. * @subpackage Filter
  19. */
  20. class NumberFormat extends AbstractLocale
  21. {
  22. protected $options = array(
  23. 'locale' => null,
  24. 'style' => NumberFormatter::DEFAULT_STYLE,
  25. 'type' => NumberFormatter::TYPE_DOUBLE
  26. );
  27. /**
  28. * @var NumberFormatter
  29. */
  30. protected $formatter = null;
  31. /**
  32. * @param array|Traversable|string|null $localeOrOptions
  33. * @param int $style
  34. * @param int $type
  35. */
  36. public function __construct(
  37. $localeOrOptions = null,
  38. $style = NumberFormatter::DEFAULT_STYLE,
  39. $type = NumberFormatter::TYPE_DOUBLE)
  40. {
  41. if ($localeOrOptions !== null) {
  42. if ($localeOrOptions instanceof Traversable) {
  43. $localeOrOptions = iterator_to_array($localeOrOptions);
  44. }
  45. if (!is_array($localeOrOptions)) {
  46. $this->setLocale($localeOrOptions);
  47. $this->setStyle($style);
  48. $this->setType($type);
  49. } else {
  50. $this->setOptions($localeOrOptions);
  51. }
  52. }
  53. }
  54. /**
  55. * @param string|null $locale
  56. * @return NumberFormat
  57. */
  58. public function setLocale($locale = null)
  59. {
  60. $this->options['locale'] = $locale;
  61. $this->formatter = null;
  62. return $this;
  63. }
  64. /**
  65. * @param int $style
  66. * @return NumberFormat
  67. */
  68. public function setStyle($style)
  69. {
  70. $this->options['style'] = (int) $style;
  71. $this->formatter = null;
  72. return $this;
  73. }
  74. /**
  75. * @return int
  76. */
  77. public function getStyle()
  78. {
  79. return $this->options['style'];
  80. }
  81. /**
  82. * @param int $type
  83. * @return NumberFormat
  84. */
  85. public function setType($type)
  86. {
  87. $this->options['type'] = (int) $type;
  88. return $this;
  89. }
  90. /**
  91. * @return int
  92. */
  93. public function getType()
  94. {
  95. return $this->options['type'];
  96. }
  97. /**
  98. * @param NumberFormatter $formatter
  99. * @return NumberFormat
  100. */
  101. public function setFormatter(NumberFormatter $formatter)
  102. {
  103. $this->formatter = $formatter;
  104. return $this;
  105. }
  106. /**
  107. * @return NumberFormatter
  108. * @throws Exception\RuntimeException
  109. */
  110. public function getFormatter()
  111. {
  112. if ($this->formatter === null) {
  113. $formatter = NumberFormatter::create($this->getLocale(), $this->getStyle());
  114. if (!$formatter) {
  115. throw new Exception\RuntimeException(
  116. 'Can not create NumberFormatter instance; ' . intl_get_error_message()
  117. );
  118. }
  119. $this->formatter = $formatter;
  120. }
  121. return $this->formatter;
  122. }
  123. /**
  124. * Defined by Zend\Filter\FilterInterface
  125. *
  126. * @see Zend\Filter\FilterInterface::filter()
  127. * @param mixed $value
  128. * @return mixed
  129. */
  130. public function filter($value)
  131. {
  132. $formatter = $this->getFormatter();
  133. $type = $this->getType();
  134. if (is_int($value) || is_float($value)) {
  135. ErrorHandler::start();
  136. $result = $formatter->format($value, $type);
  137. ErrorHandler::stop();
  138. } else {
  139. $value = str_replace(array("\xC2\xA0", ' '), '', $value);
  140. ErrorHandler::start();
  141. $result = $formatter->parse($value, $type);
  142. ErrorHandler::stop();
  143. }
  144. if ($result === false) {
  145. return $value;
  146. }
  147. return str_replace("\xC2\xA0", ' ', $result);
  148. }
  149. }