vendor/pimcore/pimcore/models/Document/Editable/Date.php line 23

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Model\Document\Editable;
  16. use Carbon\Carbon;
  17. use Pimcore\Model;
  18. /**
  19.  * @method \Pimcore\Model\Document\Editable\Dao getDao()
  20.  */
  21. class Date extends Model\Document\Editable implements EditmodeDataInterface
  22. {
  23.     /**
  24.      * Contains the date
  25.      *
  26.      * @internal
  27.      *
  28.      */
  29.     protected ?\Carbon\Carbon $date null;
  30.     public function getType(): string
  31.     {
  32.         return 'date';
  33.     }
  34.     public function getData(): mixed
  35.     {
  36.         return $this->date;
  37.     }
  38.     public function getDate(): ?\Carbon\Carbon
  39.     {
  40.         return $this->getData();
  41.     }
  42.     public function getDataEditmode(): ?int
  43.     {
  44.         if ($this->date) {
  45.             return $this->date->getTimestamp();
  46.         }
  47.         return null;
  48.     }
  49.     public function frontend()
  50.     {
  51.         if ($this->date instanceof Carbon) {
  52.             if (isset($this->config['outputFormat']) && $this->config['outputFormat']) {
  53.                 return $this->date->formatLocalized($this->config['outputFormat']);
  54.             } else {
  55.                 if (isset($this->config['format']) && $this->config['format']) {
  56.                     $format $this->config['format'];
  57.                 } else {
  58.                     $format \DateTimeInterface::ATOM;
  59.                 }
  60.                 return $this->date->format($format);
  61.             }
  62.         }
  63.     }
  64.     public function getDataForResource(): mixed
  65.     {
  66.         if ($this->date) {
  67.             return $this->date->getTimestamp();
  68.         }
  69.         return null;
  70.     }
  71.     public function setDataFromResource(mixed $data): static
  72.     {
  73.         if ($data) {
  74.             $this->setDateFromTimestamp((int)$data);
  75.         }
  76.         return $this;
  77.     }
  78.     public function setDataFromEditmode(mixed $data): static
  79.     {
  80.         if (strlen((string) $data) > 5) {
  81.             $timestamp strtotime($data);
  82.             $this->setDateFromTimestamp($timestamp);
  83.         }
  84.         return $this;
  85.     }
  86.     public function isEmpty(): bool
  87.     {
  88.         if ($this->date) {
  89.             return false;
  90.         }
  91.         return true;
  92.     }
  93.     private function setDateFromTimestamp(int $timestamp): void
  94.     {
  95.         $this->date = new Carbon();
  96.         $this->date->setTimestamp($timestamp);
  97.     }
  98. }