PageRenderTime 67ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/I18n/Validator/Float.php

https://github.com/Ocramius/zf2
PHP | 251 lines | 148 code | 35 blank | 68 comment | 25 complexity | 5ae180f8f6d570517222f47db2224eb2 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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\I18n\Validator;
  10. use Locale;
  11. use NumberFormatter;
  12. use Traversable;
  13. use IntlException;
  14. use Zend\I18n\Exception as I18nException;
  15. use Zend\Stdlib\ArrayUtils;
  16. use Zend\Stdlib\StringUtils;
  17. use Zend\Stdlib\StringWrapper\StringWrapperInterface;
  18. use Zend\Validator\AbstractValidator;
  19. use Zend\Validator\Exception;
  20. class Float extends AbstractValidator
  21. {
  22. const INVALID = 'floatInvalid';
  23. const NOT_FLOAT = 'notFloat';
  24. /**
  25. * @var array
  26. */
  27. protected $messageTemplates = array(
  28. self::INVALID => "Invalid type given. String, integer or float expected",
  29. self::NOT_FLOAT => "The input does not appear to be a float",
  30. );
  31. /**
  32. * Optional locale
  33. *
  34. * @var string|null
  35. */
  36. protected $locale;
  37. /**
  38. * UTF-8 compatable wrapper for string functions
  39. *
  40. * @var StringWrapperInterface
  41. */
  42. protected $wrapper;
  43. /**
  44. * Constructor for the integer validator
  45. *
  46. * @param array|Traversable $options
  47. * @throws Exception\ExtensionNotLoadedException if ext/intl is not present
  48. */
  49. public function __construct($options = array())
  50. {
  51. if (!extension_loaded('intl')) {
  52. throw new I18nException\ExtensionNotLoadedException(
  53. sprintf('%s component requires the intl PHP extension', __NAMESPACE__)
  54. );
  55. }
  56. $this->wrapper = StringUtils::getWrapper();
  57. if ($options instanceof Traversable) {
  58. $options = ArrayUtils::iteratorToArray($options);
  59. }
  60. if (array_key_exists('locale', $options)) {
  61. $this->setLocale($options['locale']);
  62. }
  63. parent::__construct($options);
  64. }
  65. /**
  66. * Returns the set locale
  67. *
  68. * @return string
  69. */
  70. public function getLocale()
  71. {
  72. if (null === $this->locale) {
  73. $this->locale = Locale::getDefault();
  74. }
  75. return $this->locale;
  76. }
  77. /**
  78. * Sets the locale to use
  79. *
  80. * @param string|null $locale
  81. * @return Float
  82. */
  83. public function setLocale($locale)
  84. {
  85. $this->locale = $locale;
  86. return $this;
  87. }
  88. /**
  89. * Returns true if and only if $value is a floating-point value. Uses the formal definition of a float as described
  90. * in the PHP manual: {@link http://www.php.net/float}
  91. *
  92. * @param string $value
  93. * @return bool
  94. * @throws Exception\InvalidArgumentException
  95. */
  96. public function isValid($value)
  97. {
  98. if (!is_scalar($value) || is_bool($value)) {
  99. $this->error(self::INVALID);
  100. return false;
  101. }
  102. $this->setValue($value);
  103. if (is_float($value) || is_int($value)) {
  104. return true;
  105. }
  106. // Need to check if this is scientific formatted string. If not, switch to decimal.
  107. $formatter = new NumberFormatter($this->getLocale(), NumberFormatter::SCIENTIFIC);
  108. try {
  109. if (intl_is_failure($formatter->getErrorCode())) {
  110. throw new Exception\InvalidArgumentException($formatter->getErrorMessage());
  111. }
  112. } catch (IntlException $intlException) {
  113. throw new Exception\InvalidArgumentException($e->getMessage(), 0, $intlException);
  114. }
  115. if (StringUtils::hasPcreUnicodeSupport()) {
  116. $exponentialSymbols = '[Ee' . $formatter->getSymbol(NumberFormatter::EXPONENTIAL_SYMBOL) . ']+';
  117. $search = '/' . $exponentialSymbols . '/u';
  118. } else {
  119. $exponentialSymbols = '[Ee]';
  120. $search = '/' . $exponentialSymbols . '/';
  121. }
  122. if (!preg_match($search, $value)) {
  123. $formatter = new NumberFormatter($this->getLocale(), NumberFormatter::DECIMAL);
  124. }
  125. /**
  126. * @desc There are seperator "look-alikes" for decimal and group seperators that are more commonly used than the
  127. * official unicode chracter. We need to replace those with the real thing - or remove it.
  128. */
  129. $groupSeparator = $formatter->getSymbol(NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
  130. $decSeparator = $formatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
  131. //NO-BREAK SPACE and ARABIC THOUSANDS SEPARATOR
  132. if ($groupSeparator == "\xC2\xA0") {
  133. $value = str_replace(' ', $groupSeparator, $value);
  134. } elseif ($groupSeparator == "\xD9\xAC") { //NumberFormatter doesn't have grouping at all for Arabic-Indic
  135. $value = str_replace(array('\'', $groupSeparator), '', $value);
  136. }
  137. //ARABIC DECIMAL SEPARATOR
  138. if ($decSeparator == "\xD9\xAB") {
  139. $value = str_replace(',', $decSeparator, $value);
  140. }
  141. $groupSeparatorPosition = $this->wrapper->strpos($value, $groupSeparator);
  142. $decSeparatorPosition = $this->wrapper->strpos($value, $decSeparator);
  143. //We have seperators, and they are flipped. i.e. 2.000,000 for en-US
  144. if ($groupSeparatorPosition && $decSeparatorPosition && $groupSeparatorPosition > $decSeparatorPosition) {
  145. $this->error(self::NOT_FLOAT);
  146. return false;
  147. }
  148. //If we have Unicode support, we can use the real graphemes, otherwise, just the ASCII characters
  149. $decimal = '['. preg_quote($decSeparator, '/') . ']';
  150. $prefix = '[+-]';
  151. $exp = $exponentialSymbols;
  152. $numberRange = '0-9';
  153. $useUnicode = '';
  154. $suffix = '';
  155. if (StringUtils::hasPcreUnicodeSupport()) {
  156. $prefix = '['
  157. . preg_quote(
  158. $formatter->getTextAttribute(NumberFormatter::POSITIVE_PREFIX)
  159. . $formatter->getTextAttribute(NumberFormatter::NEGATIVE_PREFIX)
  160. . $formatter->getSymbol(NumberFormatter::PLUS_SIGN_SYMBOL)
  161. . $formatter->getSymbol(NumberFormatter::MINUS_SIGN_SYMBOL),
  162. '/'
  163. )
  164. . ']{0,3}';
  165. $suffix = ($formatter->getTextAttribute(NumberFormatter::NEGATIVE_SUFFIX))
  166. ? '['
  167. . preg_quote(
  168. $formatter->getTextAttribute(NumberFormatter::POSITIVE_SUFFIX)
  169. . $formatter->getTextAttribute(NumberFormatter::NEGATIVE_SUFFIX)
  170. . $formatter->getSymbol(NumberFormatter::PLUS_SIGN_SYMBOL)
  171. . $formatter->getSymbol(NumberFormatter::MINUS_SIGN_SYMBOL),
  172. '/'
  173. )
  174. . ']{0,3}'
  175. : '';
  176. $numberRange = '\p{N}';
  177. $useUnicode = 'u';
  178. }
  179. /**
  180. * @desc Match against the formal definition of a float. The
  181. * exponential number check is modified for RTL non-Latin number
  182. * systems (Arabic-Indic numbering). I'm also switching out the period
  183. * for the decimal separator. The formal definition leaves out +- from
  184. * the integer and decimal notations so add that. This also checks
  185. * that a grouping sperator is not in the last GROUPING_SIZE graphemes
  186. * of the string - i.e. 10,6 is not valid for en-US.
  187. * @see http://www.php.net/float
  188. */
  189. $lnum = '[' . $numberRange . ']+';
  190. $dnum = '(([' . $numberRange . ']*' . $decimal . $lnum . ')|(' . $lnum . $decimal . '[' . $numberRange . ']*))';
  191. $expDnum = '((' . $prefix . '((' . $lnum . '|' . $dnum . ')' . $exp . $prefix . $lnum . ')' . $suffix . ')|'
  192. . '(' . $suffix . '(' . $lnum . $prefix . $exp . '(' . $dnum . '|' . $lnum . '))' . $prefix . '))';
  193. // LEFT-TO-RIGHT MARK (U+200E) is messing up everything for the handful
  194. // of locales that have it
  195. $lnumSearch = str_replace("\xE2\x80\x8E", '', '/^' .$prefix . $lnum . $suffix . '$/' . $useUnicode);
  196. $dnumSearch = str_replace("\xE2\x80\x8E", '', '/^' .$prefix . $dnum . $suffix . '$/' . $useUnicode);
  197. $expDnumSearch = str_replace("\xE2\x80\x8E", '', '/^' . $expDnum . '$/' . $useUnicode);
  198. $value = str_replace("\xE2\x80\x8E", '', $value);
  199. $unGroupedValue = str_replace($groupSeparator, '', $value);
  200. // No strrpos() in wrappers yet. ICU 4.x doesn't have grouping size for
  201. // everything. ICU 52 has 3 for ALL locales.
  202. $groupSize = ($formatter->getAttribute(NumberFormatter::GROUPING_SIZE))
  203. ? $formatter->getAttribute(NumberFormatter::GROUPING_SIZE)
  204. : 3;
  205. $lastStringGroup = $this->wrapper->substr($value, -$groupSize);
  206. if ((preg_match($lnumSearch, $unGroupedValue)
  207. || preg_match($dnumSearch, $unGroupedValue)
  208. || preg_match($expDnumSearch, $unGroupedValue))
  209. && false === $this->wrapper->strpos($lastStringGroup, $groupSeparator)
  210. ) {
  211. return true;
  212. }
  213. $this->error(self::NOT_FLOAT);
  214. return false;
  215. }
  216. }