/** * Defined by Zend_Filter_Interface * * Normalizes the given input * * @param string $value Value to normalized * @return string|array The normalized value */ public function filter($value) { if (Zend_Locale_Format::isNumber($value, $this->_options)) { return Zend_Locale_Format::getNumber($value, $this->_options); // if (($this->_options['precision'] === 0) && Zend_Locale_Format::isInteger($value, $this->_options)) { // Detect integer // return Zend_Locale_Format::getInteger($value, $this->_options); // } else if (($this->_options['precision'] === null) && Zend_Locale_Format::isFloat($value, $this->_options)) { // Detect float // return Zend_Locale_Format::getFloat($value, $this->_options); // } else if (Zend_Locale_Format::isNumber($value, $this->_options)) { // Detect all other numbers // return Zend_Locale_Format::getNumber($value, $this->_options); } else { if ($this->_options['date_format'] === null && strpos($value, ':') !== false) { // Special case, no date format specified, detect time input return Zend_Locale_Format::getTime($value, $this->_options); } else { if (Zend_Locale_Format::checkDateFormat($value, $this->_options)) { // Detect date or time input return Zend_Locale_Format::getDate($value, $this->_options); } } } return $value; }
public function validate($validator) { if (!$this->value && !$validator->fieldIsRequired($this->name)) { return true; } require_once THIRDPARTY_PATH . "/Zend/Locale/Format.php"; $valid = Zend_Locale_Format::isNumber(trim($this->value), array('locale' => i18n::get_locale())); if (!$valid) { $validator->validationError($this->name, _t('NumericField.VALIDATION', "'{value}' is not a number, only numbers can be accepted for this field", array('value' => $this->value)), "validation"); return false; } return true; }
/** * displays the value in its current locality format */ public function setValue($value, $data = array()) { require_once THIRDPARTY_PATH . "/Zend/Locale/Format.php"; // If passing in a non-string number, or a value // directly from a dataobject then localise this number if (is_numeric($value) && !is_string($value) || $value && $data instanceof DataObject) { $locale = new Zend_Locale($this->getLocale()); $this->value = Zend_Locale_Format::toNumber($value, array('locale' => $locale)); } else { if (Zend_Locale_Format::isNumber($this->clean($value), array('locale' => i18n::get_locale()))) { // If an invalid number, store it anyway, but validate() will fail $this->value = $this->clean($value); } } return $this; }
/** * Defined by Zend_Filter_Interface * * Normalizes the given input * * @param string $value Value to normalized * @return string|array The normalized value */ public function filter($value) { if (Zend_Locale_Format::isNumber($value, $this->_options)) { return Zend_Locale_Format::getNumber($value, $this->_options); } else { if ($this->_options['date_format'] === null && strpos($value, ':') !== false) { // Special case, no date format specified, detect time input return Zend_Locale_Format::getTime($value, $this->_options); } else { if (Zend_Locale_Format::checkDateFormat($value, $this->_options)) { // Detect date or time input return Zend_Locale_Format::getDate($value, $this->_options); } } } return $value; }
/** * Determine if the current value is a valid number in the current locale * * @return bool */ protected function isNumeric() { require_once "Zend/Locale/Format.php"; $locale = new Zend_Locale($this->getLocale()); return Zend_Locale_Format::isNumber($this->clean($this->value), array('locale' => $locale)); }
/** * Get the unit from the form. If it's defined and different than the * display unit, convert the value and set it back into this control's value. * Then, format with proper number of digits. * * Do not call this method more than once! */ protected function convert() { $user = Transaction::getInstance()->getUser(); if ($this->getValue() !== null && Zend_Locale_Format::isNumber($this->getValue(), array("locale" => $user->getLocale()))) { $form = $this->getForm(); // Get the 'hidden' value, which indicates the unit of the current value. $unit = $form->getValue($this->getUnitFieldName()); // If the unit is different, convert if ($unit && $unit != $this->displayUnit) { $measure = MeasureUtils::newMeasure($unit, $this->getValue(), $user->getLocale()); $unitInfo = MeasureUtils::getUnitInfo($this->displayUnit); $measure->setType($unitInfo['constantName']); // Sets the new value without rounding and without formatting $this->setValue($measure->getValue(-1, $user->getLocale())); } $format = Formatter::getInstance(); // Format and round the value $this->setValue($format->number($format->getNumber($this->getValue()), $this->decimalDigits)); } }
/** * Prepare data for save * * @return Mage_Eav_Model_Entity_Attribute * @throws Mage_Eav_Exception */ protected function _beforeSave() { /** * Check for maximum attribute_code length */ if (isset($this->_data['attribute_code']) && !Zend_Validate::is($this->_data['attribute_code'], 'StringLength', array('max' => self::ATTRIBUTE_CODE_MAX_LENGTH))) { throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Maximum length of attribute code must be less then %s symbols', self::ATTRIBUTE_CODE_MAX_LENGTH)); } $defaultValue = $this->getDefaultValue(); $hasDefaultValue = (string) $defaultValue != ''; if ($this->getBackendType() == 'decimal' && $hasDefaultValue) { $locale = Mage::app()->getLocale()->getLocaleCode(); if (!Zend_Locale_Format::isNumber($defaultValue, array('locale' => $locale))) { throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default decimal value')); } try { $filter = new Zend_Filter_LocalizedToNormalized(array('locale' => Mage::app()->getLocale()->getLocaleCode())); $this->setDefaultValue($filter->filter($defaultValue)); } catch (Exception $e) { throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default decimal value')); } } if ($this->getBackendType() == 'datetime') { if (!$this->getBackendModel()) { $this->setBackendModel('eav/entity_attribute_backend_datetime'); } if (!$this->getFrontendModel()) { $this->setFrontendModel('eav/entity_attribute_frontend_datetime'); } // save default date value as timestamp if ($hasDefaultValue) { $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT); try { $defaultValue = Mage::app()->getLocale()->date($defaultValue, $format, null, false)->toValue(); $this->setDefaultValue($defaultValue); } catch (Exception $e) { throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid default date')); } } } if ($this->getBackendType() == 'gallery') { if (!$this->getBackendModel()) { $this->setBackendModel('eav/entity_attribute_backend_media'); } } return parent::_beforeSave(); }
/** * test isNumber * expected boolean */ public function testIsNumber() { $this->assertTrue( Zend_Locale_Format::isNumber('-1.234.567,12345', array('locale' => 'de_AT'))); $this->assertFalse(Zend_Locale_Format::isNumber('textwithoutnumber', array('locale' => 'de_AT'))); }
public function testShortNotation() { $this->assertEquals(0.12345, Zend_Locale_Format::getNumber(0.12345)); $options = array('locale' => 'de'); $this->assertEquals(0.12345, Zend_Locale_Format::getNumber(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals(0.12345, Zend_Locale_Format::getNumber(',12345', $options)); $this->assertEquals('0,75', Zend_Locale_Format::toNumber(0.75, array('locale' => 'de_DE', 'precision' => 2))); $this->assertTrue(Zend_Locale_Format::isNumber(',12345', array('locale' => 'de_AT'))); $this->assertEquals(0.12345, Zend_Locale_Format::getFloat(0.12345)); $options = array('locale' => 'de'); $this->assertEquals(0.12345, Zend_Locale_Format::getFloat(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals(0.12345, Zend_Locale_Format::getFloat(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals('0,12345', Zend_Locale_Format::toFloat(0.12345, $options)); $options = array('locale' => 'ar_QA'); $this->assertEquals('0,12345', Zend_Locale_Format::toFloat(0.12345, $options)); $this->assertTrue(Zend_Locale_Format::isFloat(',12345', array('locale' => 'de_AT'))); $this->assertEquals(0, Zend_Locale_Format::getInteger(0.1234567)); $options = array('locale' => 'de'); $this->assertEquals(0, Zend_Locale_Format::getInteger(',12345', $options)); $options = array('locale' => 'de_AT'); $this->assertEquals(0, Zend_Locale_Format::getInteger(',12345', $options)); $this->assertEquals('0', Zend_Locale_Format::toInteger(0.123, array('locale' => 'de'))); $options = array('locale' => 'de_AT'); $this->assertEquals('0', Zend_Locale_Format::toInteger(0.12345, $options)); $this->assertFalse(Zend_Locale_Format::isInteger(',12345', array('locale' => 'de_AT'))); $options = array('locale' => 'de_AT'); $this->assertEquals('0,567', Zend_Locale_Format::toNumber(0.5669999999999999, $options)); }
/** * test isNumber * expected boolean */ public function testIsNumber() { $this->assertEquals(Zend_Locale_Format::isNumber('-1.234.567,12345', array('locale' => 'de_AT')), true, "true expected"); $this->assertEquals(Zend_Locale_Format::isNumber('textwithoutnumber', array('locale' => 'de_AT')), false, "false expected"); }
/** * Prepare data for save * * @return Mage_Eav_Model_Entity_Attribute */ protected function _beforeSave() { // prevent overriding product data if (isset($this->_data['attribute_code']) && Mage::getModel('Mage_Catalog_Model_Product')->isReservedAttribute($this)) { throw Mage::exception('Mage_Eav', Mage::helper('Mage_Eav_Helper_Data')->__('The attribute code \'%s\' is reserved by system. Please try another attribute code', $this->_data['attribute_code'])); } /** * Check for maximum attribute_code length */ if (isset($this->_data['attribute_code']) && !Zend_Validate::is($this->_data['attribute_code'], 'StringLength', array('max' => self::ATTRIBUTE_CODE_MAX_LENGTH))) { throw Mage::exception('Mage_Eav', Mage::helper('Mage_Eav_Helper_Data')->__('Maximum length of attribute code must be less then %s symbols', self::ATTRIBUTE_CODE_MAX_LENGTH)); } $defaultValue = $this->getDefaultValue(); $hasDefaultValue = (string) $defaultValue != ''; if ($this->getBackendType() == 'decimal' && $hasDefaultValue) { if (!Zend_Locale_Format::isNumber($defaultValue, array('locale' => Mage::app()->getLocale()->getLocaleCode()))) { throw Mage::exception('Mage_Eav', Mage::helper('Mage_Eav_Helper_Data')->__('Invalid default decimal value')); } try { $filter = new Zend_Filter_LocalizedToNormalized(array('locale' => Mage::app()->getLocale()->getLocaleCode())); $this->setDefaultValue($filter->filter($defaultValue)); } catch (Exception $e) { throw Mage::exception('Mage_Eav', Mage::helper('Mage_Eav_Helper_Data')->__('Invalid default decimal value')); } } if ($this->getBackendType() == 'datetime') { if (!$this->getBackendModel()) { $this->setBackendModel('Mage_Eav_Model_Entity_Attribute_Backend_Datetime'); } if (!$this->getFrontendModel()) { $this->setFrontendModel('Mage_Eav_Model_Entity_Attribute_Frontend_Datetime'); } // save default date value as timestamp if ($hasDefaultValue) { $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT); try { $defaultValue = Mage::app()->getLocale()->date($defaultValue, $format, null, false)->toValue(); $this->setDefaultValue($defaultValue); } catch (Exception $e) { throw Mage::exception('Mage_Eav', Mage::helper('Mage_Eav_Helper_Data')->__('Invalid default date')); } } } if ($this->getBackendType() == 'gallery') { if (!$this->getBackendModel()) { $this->setBackendModel('Mage_Eav_Model_Entity_Attribute_Backend_Default'); } } return parent::_beforeSave(); }
/** * Prepare data for save * * @return $this * @throws Exception */ protected function _beforeSave() { // prevent overriding product data if (isset($this->_data['attribute_code']) && $this->reservedAttributeList->isReservedAttribute($this)) { throw new Exception(__('The attribute code \'%1\' is reserved by system. Please try another attribute code', $this->_data['attribute_code'])); } /** * Check for maximum attribute_code length */ if (isset($this->_data['attribute_code']) && !\Zend_Validate::is($this->_data['attribute_code'], 'StringLength', array('max' => self::ATTRIBUTE_CODE_MAX_LENGTH))) { throw new Exception(__('Maximum length of attribute code must be less than %1 symbols', self::ATTRIBUTE_CODE_MAX_LENGTH)); } $defaultValue = $this->getDefaultValue(); $hasDefaultValue = (string) $defaultValue != ''; if ($this->getBackendType() == 'decimal' && $hasDefaultValue) { if (!\Zend_Locale_Format::isNumber($defaultValue, array('locale' => $this->_localeResolver->getLocaleCode()))) { throw new Exception(__('Invalid default decimal value')); } try { $filter = new \Zend_Filter_LocalizedToNormalized(array('locale' => $this->_localeResolver->getLocaleCode())); $this->setDefaultValue($filter->filter($defaultValue)); } catch (\Exception $e) { throw new Exception(__('Invalid default decimal value')); } } if ($this->getBackendType() == 'datetime') { if (!$this->getBackendModel()) { $this->setBackendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\Datetime'); } if (!$this->getFrontendModel()) { $this->setFrontendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Frontend\\Datetime'); } // save default date value as timestamp if ($hasDefaultValue) { $format = $this->_localeDate->getDateFormat(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::FORMAT_TYPE_SHORT); try { $defaultValue = $this->_localeDate->date($defaultValue, $format, null, false)->toValue(); $this->setDefaultValue($defaultValue); } catch (\Exception $e) { throw new Exception(__('Invalid default date')); } } } if ($this->getBackendType() == 'gallery') { if (!$this->getBackendModel()) { $this->setBackendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\DefaultBackend'); } } return parent::_beforeSave(); }
/** * test if isNumberFailed * expected boolean */ public function testIsNumberFailed() { $value = Zend_Locale_Format::isNumber('textwithoutnumber', 'de_AT'); $this->assertEquals($value, FALSE, "FALSE expected"); }
/** Checks if a given string is valid representation of a number in the current locale. * @return bool * @example http://www.concrete5.org/documentation/how-tos/developers/formatting-numbers/ See the Formatting numbers how-to for more details */ public function isNumber($string) { return Zend_Locale_Format::isNumber($string, array('locale' => $this->getZendLocale())); }
protected function _beforeSave() { // prevent overriding product data if (isset($this->_data['attribute_code']) && Mage::getModel('catalog/product')->isReservedAttribute($this)) { Mage::throwException(Mage::helper('eav')->__('The attribute code \'%s\' is reserved by system. Please try another attribute code.', $this->_data['attribute_code'])); } $defaultValue = $this->getDefaultValue(); $hasDefaultValue = (string) $defaultValue != ''; if ($this->getBackendType() == 'decimal' && $hasDefaultValue) { if (!Zend_Locale_Format::isNumber($defaultValue, array('locale' => Mage::app()->getLocale()->getLocaleCode()))) { throw new Exception('Invalid default decimal value.'); } try { $filter = new Zend_Filter_LocalizedToNormalized(array('locale' => Mage::app()->getLocale()->getLocaleCode())); $this->setDefaultValue($filter->filter($defaultValue)); } catch (Exception $e) { throw new Exception('Invalid default decimal value.'); } } if ($this->getBackendType() == 'datetime') { if (!$this->getBackendModel()) { $this->setBackendModel('eav/entity_attribute_backend_datetime'); } if (!$this->getFrontendModel()) { $this->setFrontendModel('eav/entity_attribute_frontend_datetime'); } // save default date value as timestamp if ($hasDefaultValue) { $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT); try { $defaultValue = Mage::app()->getLocale()->date($defaultValue, $format, null, false)->toValue(); $this->setDefaultValue($defaultValue); } catch (Exception $e) { throw new Exception('Invalid default date.'); } } } if ($this->getBackendType() == 'gallery') { if (!$this->getBackendModel()) { $this->setBackendModel('eav/entity_attribute_backend_media'); } } return parent::_beforeSave(); }