Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Int.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 = 'intInvalid';
41  const NOT_INT = 'notInt';
42 
46  protected $_messageTemplates = array(
47  self::INVALID => "Invalid type given. String or integer expected",
48  self::NOT_INT => "'%value%' does not appear to be an integer",
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  if ($locale !== null) {
80  $this->setLocale($locale);
81  }
82  }
83 
87  public function getLocale()
88  {
89  return $this->_locale;
90  }
91 
98  public function setLocale($locale = null)
99  {
100  #require_once 'Zend/Locale.php';
101  $this->_locale = Zend_Locale::findLocale($locale);
102  return $this;
103  }
104 
113  public function isValid($value)
114  {
115  if (!is_string($value) && !is_int($value) && !is_float($value)) {
116  $this->_error(self::INVALID);
117  return false;
118  }
119 
120  if (is_int($value)) {
121  return true;
122  }
123 
124  $this->_setValue($value);
125  if ($this->_locale === null) {
126  $locale = localeconv();
127  $valueFiltered = str_replace($locale['decimal_point'], '.', $value);
128  $valueFiltered = str_replace($locale['thousands_sep'], '', $valueFiltered);
129 
130  if (strval(intval($valueFiltered)) != $valueFiltered) {
131  $this->_error(self::NOT_INT);
132  return false;
133  }
134 
135  } else {
136  try {
137  if (!Zend_Locale_Format::isInteger($value, array('locale' => $this->_locale))) {
138  $this->_error(self::NOT_INT);
139  return false;
140  }
141  } catch (Zend_Locale_Exception $e) {
142  $this->_error(self::NOT_INT);
143  return false;
144  }
145  }
146 
147  return true;
148  }
149 }
setLocale($locale=null)
Definition: Int.php:98
isValid($value)
Definition: Int.php:113
_error($messageKey, $value=null)
Definition: Abstract.php:284
static isRegistered($index)
Definition: Registry.php:178
__construct($locale=null)
Definition: Int.php:58
$value
Definition: gender.phtml:16
static isInteger($value, array $options=array())
Definition: Format.php:721
const NOT_INT
Definition: Int.php:41
static findLocale($locale=null)
Definition: Locale.php:1740
const INVALID
Definition: Int.php:40
static get($index)
Definition: Registry.php:141