123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Eav\Model\Entity\Attribute\Backend;
- /**
- * @api
- * @since 100.0.2
- */
- class Datetime extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
- {
- /**
- * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
- */
- protected $_localeDate;
- /**
- * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
- * @codeCoverageIgnore
- */
- public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate)
- {
- $this->_localeDate = $localeDate;
- }
- /**
- * Formatting date value before save
- *
- * Should set (bool, string) correct type for empty value from html form,
- * necessary for further process, else date string
- *
- * @param \Magento\Framework\DataObject $object
- * @throws \Magento\Framework\Exception\LocalizedException
- * @return $this
- */
- public function beforeSave($object)
- {
- $attributeName = $this->getAttribute()->getName();
- $_formated = $object->getData($attributeName . '_is_formated');
- if (!$_formated && $object->hasData($attributeName)) {
- try {
- $value = $this->formatDate($object->getData($attributeName));
- } catch (\Exception $e) {
- throw new \Magento\Framework\Exception\LocalizedException(__('Invalid date'));
- }
- if ($value === null) {
- $value = $object->getData($attributeName);
- }
- $object->setData($attributeName, $value);
- $object->setData($attributeName . '_is_formated', true);
- }
- 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|\DateTimeInterface $date
- * @return string
- */
- public function formatDate($date)
- {
- if (empty($date)) {
- return null;
- }
- // unix timestamp given - simply instantiate date object
- if (is_scalar($date) && preg_match('/^[0-9]+$/', $date)) {
- $date = (new \DateTime())->setTimestamp($date);
- } elseif (!($date instanceof \DateTimeInterface)) {
- // normalized format expecting Y-m-d[ H:i:s] - time is optional
- $date = new \DateTime($date);
- }
- return $date->format('Y-m-d H:i:s');
- }
- }
|