/** * @param TimezoneInterface $localeDate * * @deprecated */ public function __construct(TimezoneInterface $localeDate) { $this->_localeDate = $localeDate; $this->_localToNormalFilter = new \Zend_Filter_LocalizedToNormalized( ['date_format' => $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT)] ); $this->_normalToLocalFilter = new \Zend_Filter_NormalizedToLocalized( ['date_format' => \Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT] ); }
/** * @dataProvider getValueDataProvider */ public function testGetValue(array $data, $expect) { if (isset($data['date_format'])) { $data['date_format'] = $this->_localeDate->getDateFormat($data['date_format']); } if (isset($data['time_format'])) { $data['time_format'] = $this->_localeDate->getTimeFormat($data['time_format']); } /** @var $date \Magento\Framework\Data\Form\Element\Date*/ $date = $this->_elementFactory->create('Magento\\Framework\\Data\\Form\\Element\\Date', $data); $this->assertEquals($expect, $date->getValue()); }
/** * Retrieve attribute value * * @param \Magento\Framework\Object $object * @return mixed */ public function getValue(\Magento\Framework\Object $object) { $data = ''; $value = parent::getValue($object); $format = $this->_localeDate->getDateFormat(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::FORMAT_TYPE_MEDIUM); if ($value) { try { $data = $this->_localeDate->date($value, \Zend_Date::ISO_8601, null, false)->toString($format); } catch (\Exception $e) { $data = $this->_localeDate->date($value, null, null, false)->toString($format); } } return $data; }
/** * 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(); }
/** * Get/Set/Reset date filter format * * @param string|null|false $format * @return $this|string */ protected function _dateFilterFormat($format = null) { if (is_null($format)) { // get format if (is_null($this->_dateFilterFormat)) { $this->_dateFilterFormat = \IntlDateFormatter::SHORT; } return $this->_localeDate->getDateFormat($this->_dateFilterFormat); } elseif ($format === false) { // reset value $this->_dateFilterFormat = null; return $this; } $this->_dateFilterFormat = $format; return $this; }
/** * Prepare data for save * * @return $this * @throws LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function beforeSave() { // prevent overriding product data if (isset($this->_data['attribute_code']) && $this->reservedAttributeList->isReservedAttribute($this)) { throw new LocalizedException(__('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', ['max' => self::ATTRIBUTE_CODE_MAX_LENGTH])) { throw new LocalizedException(__('An attribute code must be fewer than %1 characters.', self::ATTRIBUTE_CODE_MAX_LENGTH)); } $defaultValue = $this->getDefaultValue(); $hasDefaultValue = (string) $defaultValue != ''; if ($this->getBackendType() == 'decimal' && $hasDefaultValue) { $numberFormatter = new \NumberFormatter($this->_localeResolver->getLocale(), \NumberFormatter::DECIMAL); $defaultValue = $numberFormatter->parse($defaultValue); if ($defaultValue === false) { throw new LocalizedException(__('Invalid default decimal value')); } $this->setDefaultValue($defaultValue); } 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(\IntlDateFormatter::SHORT); try { $defaultValue = $this->dateTimeFormatter->formatObject(new \DateTime($defaultValue), $format); $this->setDefaultValue($defaultValue); } catch (\Exception $e) { throw new LocalizedException(__('Invalid default date')); } } } if ($this->getBackendType() == 'gallery') { if (!$this->getBackendModel()) { $this->setBackendModel('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\DefaultBackend'); } } return parent::beforeSave(); }
/** * Get/Set/Reset date filter format * * @param string|null|false $format * @return $this|string */ protected function _dateFilterFormat($format = null) { if (is_null($format)) { // get format if (is_null($this->_dateFilterFormat)) { $this->_dateFilterFormat = \Magento\Framework\Stdlib\DateTime\TimezoneInterface::FORMAT_TYPE_SHORT; } return $this->_localeDate->getDateFormat($this->_dateFilterFormat); } else { if ($format === false) { // reset value $this->_dateFilterFormat = null; return $this; } } $this->_dateFilterFormat = $format; return $this; }
/** * Prepare date for save in DB * * string format used from input fields (all date input fields need apply locale settings) * int value can be declared in code (this meen whot we use valid date) * * @param string|int $date * @return string */ public function formatDate($date) { if (empty($date)) { return null; } // unix timestamp given - simply instantiate date object if (preg_match('/^[0-9]+$/', $date)) { $date = new \Magento\Framework\Stdlib\DateTime\Date((int) $date); // international format } elseif (preg_match('#^\\d{4}-\\d{2}-\\d{2}( \\d{2}:\\d{2}:\\d{2})?$#', $date)) { $zendDate = new \Magento\Framework\Stdlib\DateTime\Date(); $date = $zendDate->setIso($date); // parse this date in current locale, do not apply GMT offset } else { $date = $this->_localeDate->date($date, $this->_localeDate->getDateFormat(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::FORMAT_TYPE_SHORT), null, false); } return $date->toString(\Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT); }
/** * Return formatted option value for quote option * * @param string $optionValue Prepared for cart option value * @return string */ public function getFormattedOptionValue($optionValue) { if ($this->_formattedOptionValue === null) { $option = $this->getOption(); if ($this->getOption()->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DATE) { $format = $this->_localeDate->getDateFormat(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::FORMAT_TYPE_MEDIUM); $result = $this->_localeDate->date($optionValue, \Zend_Date::ISO_8601, null, false)->toString($format); } elseif ($this->getOption()->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DATE_TIME) { $format = $this->_localeDate->getDateTimeFormat(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::FORMAT_TYPE_SHORT); $result = $this->_localeDate->date($optionValue, \Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT, null, false)->toString($format); } elseif ($this->getOption()->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_TIME) { $date = new \Magento\Framework\Stdlib\DateTime\Date($optionValue); $result = date($this->is24hTimeFormat() ? 'H:i' : 'h:i a', $date->getTimestamp()); } else { $result = $optionValue; } $this->_formattedOptionValue = $result; } return $this->_formattedOptionValue; }
/** * Get interval for a day * * @param \Magento\Framework\Stdlib\DateTime\DateInterface $dateStart * @return array */ protected function _getDayInterval(\Magento\Framework\Stdlib\DateTime\DateInterface $dateStart) { $interval = array('period' => $dateStart->toString($this->_localeDate->getDateFormat()), 'start' => $dateStart->toString('yyyy-MM-dd HH:mm:ss'), 'end' => $dateStart->toString('yyyy-MM-dd 23:59:59')); return $interval; }