Float.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_Abstract
  23. */
  24. require_once 'Zend/Validate/Abstract.php';
  25. /**
  26. * @see Zend_Locale_Format
  27. */
  28. require_once 'Zend/Locale/Format.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Validate
  32. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Validate_Float extends Zend_Validate_Abstract
  36. {
  37. const NOT_FLOAT = 'notFloat';
  38. /**
  39. * @var array
  40. */
  41. protected $_messageTemplates = array(
  42. self::NOT_FLOAT => "'%value%' does not appear to be a float"
  43. );
  44. protected $_locale;
  45. /**
  46. * Constructor for the float validator
  47. *
  48. * @param string|Zend_Locale $locale
  49. */
  50. public function __construct($locale = null)
  51. {
  52. $this->setLocale($locale);
  53. }
  54. /**
  55. * Returns the set locale
  56. */
  57. public function getLocale()
  58. {
  59. return $this->_locale;
  60. }
  61. /**
  62. * Sets the locale to use
  63. *
  64. * @param string|Zend_Locale $locale
  65. */
  66. public function setLocale($locale = null)
  67. {
  68. require_once 'Zend/Locale.php';
  69. $this->_locale = Zend_Locale::findLocale($locale);
  70. return $this;
  71. }
  72. /**
  73. * Defined by Zend_Validate_Interface
  74. *
  75. * Returns true if and only if $value is a floating-point value
  76. *
  77. * @param string $value
  78. * @return boolean
  79. */
  80. public function isValid($value)
  81. {
  82. $valueString = (string) $value;
  83. $this->_setValue($valueString);
  84. try {
  85. if (!Zend_Locale_Format::isFloat($value, array('locale' => $this->_locale))) {
  86. $this->_error();
  87. return false;
  88. }
  89. } catch (Zend_Locale_Exception $e) {
  90. $this->_error();
  91. return false;
  92. }
  93. return true;
  94. }
  95. }