PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Validate/Float.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 134 lines | 64 code | 15 blank | 55 comment | 11 complexity | b16d73ff31bb115635fbce7b04aca088 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Float.php 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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 INVALID = 'floatInvalid';
  38. const NOT_FLOAT = 'notFloat';
  39. /**
  40. * @var array
  41. */
  42. protected $_messageTemplates = array(
  43. self::INVALID => "Invalid type given. String, integer or float expected",
  44. self::NOT_FLOAT => "'%value%' does not appear to be a float",
  45. );
  46. protected $_locale;
  47. /**
  48. * Constructor for the float validator
  49. *
  50. * @param string|Zend_Config|Zend_Locale $locale
  51. */
  52. public function __construct($locale = null)
  53. {
  54. if ($locale instanceof Zend_Config) {
  55. $locale = $locale->toArray();
  56. }
  57. if (is_array($locale)) {
  58. if (array_key_exists('locale', $locale)) {
  59. $locale = $locale['locale'];
  60. } else {
  61. $locale = null;
  62. }
  63. }
  64. if (empty($locale)) {
  65. require_once 'Zend/Registry.php';
  66. if (Zend_Registry::isRegistered('Zend_Locale')) {
  67. $locale = Zend_Registry::get('Zend_Locale');
  68. }
  69. }
  70. $this->setLocale($locale);
  71. }
  72. /**
  73. * Returns the set locale
  74. */
  75. public function getLocale()
  76. {
  77. return $this->_locale;
  78. }
  79. /**
  80. * Sets the locale to use
  81. *
  82. * @param string|Zend_Locale $locale
  83. */
  84. public function setLocale($locale = null)
  85. {
  86. require_once 'Zend/Locale.php';
  87. $this->_locale = Zend_Locale::findLocale($locale);
  88. return $this;
  89. }
  90. /**
  91. * Defined by Zend_Validate_Interface
  92. *
  93. * Returns true if and only if $value is a floating-point value
  94. *
  95. * @param string $value
  96. * @return boolean
  97. */
  98. public function isValid($value)
  99. {
  100. if (!is_string($value) && !is_int($value) && !is_float($value)) {
  101. $this->_error(self::INVALID);
  102. return false;
  103. }
  104. if (is_float($value)) {
  105. return true;
  106. }
  107. $this->_setValue($value);
  108. try {
  109. if (!Zend_Locale_Format::isFloat($value, array('locale' => $this->_locale))) {
  110. $this->_error(self::NOT_FLOAT);
  111. return false;
  112. }
  113. } catch (Zend_Locale_Exception $e) {
  114. $this->_error(self::NOT_FLOAT);
  115. return false;
  116. }
  117. return true;
  118. }
  119. }