Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Float.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
30 #require_once 'Zend/Locale/Format.php';
31 
39 {
40  const INVALID = 'floatInvalid';
41  const NOT_FLOAT = 'notFloat';
42 
46  protected $_messageTemplates = array(
47  self::INVALID => "Invalid type given. String, integer or float expected",
48  self::NOT_FLOAT => "'%value%' does not appear to be a float",
49  );
50 
51  protected $_locale;
52 
58  public function __construct($locale = null)
59  {
60  if ($locale instanceof Zend_Config) {
61  $locale = $locale->toArray();
62  }
63 
64  if (is_array($locale)) {
65  if (array_key_exists('locale', $locale)) {
66  $locale = $locale['locale'];
67  } else {
68  $locale = null;
69  }
70  }
71 
72  if (empty($locale)) {
73  #require_once 'Zend/Registry.php';
74  if (Zend_Registry::isRegistered('Zend_Locale')) {
75  $locale = Zend_Registry::get('Zend_Locale');
76  }
77  }
78 
79  $this->setLocale($locale);
80  }
81 
85  public function getLocale()
86  {
87  return $this->_locale;
88  }
89 
96  public function setLocale($locale = null)
97  {
98  #require_once 'Zend/Locale.php';
99  $this->_locale = Zend_Locale::findLocale($locale);
100  return $this;
101  }
102 
111  public function isValid($value)
112  {
113  if (!is_string($value) && !is_int($value) && !is_float($value)) {
114  $this->_error(self::INVALID);
115  return false;
116  }
117 
118  if (is_float($value)) {
119  return true;
120  }
121 
122  $this->_setValue($value);
123  try {
124  if (!Zend_Locale_Format::isFloat($value, array('locale' => $this->_locale))) {
125  $this->_error(self::NOT_FLOAT);
126  return false;
127  }
128  } catch (Zend_Locale_Exception $e) {
129  $this->_error(self::NOT_FLOAT);
130  return false;
131  }
132 
133  return true;
134  }
135 }
static isFloat($value, array $options=array())
Definition: Format.php:673
__construct($locale=null)
Definition: Float.php:58
setLocale($locale=null)
Definition: Float.php:96
_error($messageKey, $value=null)
Definition: Abstract.php:284
static isRegistered($index)
Definition: Registry.php:178
$value
Definition: gender.phtml:16
static findLocale($locale=null)
Definition: Locale.php:1740
isValid($value)
Definition: Float.php:111
static get($index)
Definition: Registry.php:141