vendor/pimcore/pimcore/models/DataObject/ClassDefinition/Data/Time.php line 74

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\DataObject\ClassDefinition\Data;
  15. use Pimcore\Model;
  16. class Time extends Model\DataObject\ClassDefinition\Data\Input
  17. {
  18.     /**
  19.      * Static type of this element
  20.      *
  21.      * @internal
  22.      *
  23.      * @var string
  24.      */
  25.     public $fieldtype 'time';
  26.     /**
  27.      * Column length
  28.      *
  29.      * @internal
  30.      *
  31.      * @var int
  32.      */
  33.     public $columnLength 5;
  34.     /**
  35.      * @internal
  36.      *
  37.      * @var string|null
  38.      */
  39.     public $minValue;
  40.     /**
  41.      * @internal
  42.      *
  43.      * @var string|null
  44.      */
  45.     public $maxValue;
  46.     /**
  47.      * @internal
  48.      *
  49.      * @var int
  50.      */
  51.     public $increment 15 ;
  52.     /**
  53.      * @return string|null
  54.      */
  55.     public function getMinValue()
  56.     {
  57.         return $this->minValue;
  58.     }
  59.     /**
  60.      * @param string|null $minValue
  61.      */
  62.     public function setMinValue($minValue)
  63.     {
  64.         if (strlen($minValue)) {
  65.             $this->minValue $this->toTime($minValue);
  66.         } else {
  67.             $this->minValue null;
  68.         }
  69.     }
  70.     /**
  71.      * @return string|null
  72.      */
  73.     public function getMaxValue()
  74.     {
  75.         return $this->maxValue;
  76.     }
  77.     /**
  78.      * @param string|null $maxValue
  79.      */
  80.     public function setMaxValue($maxValue)
  81.     {
  82.         if (strlen($maxValue)) {
  83.             $this->maxValue $this->toTime($maxValue);
  84.         } else {
  85.             $this->maxValue null;
  86.         }
  87.     }
  88.     /**
  89.      * {@inheritdoc}
  90.      */
  91.     public function checkValidity($data$omitMandatoryCheck false$params = [])
  92.     {
  93.         parent::checkValidity($data$omitMandatoryCheck);
  94.         if ((is_string($data) && strlen($data) != && !empty($data)) || (!empty($data) && !is_string($data))) {
  95.             throw new Model\Element\ValidationException('Wrong time format given must be a 5 digit string (eg: 06:49) [ '.$this->getName().' ]');
  96.         }
  97.         if (!$omitMandatoryCheck && strlen($data)) {
  98.             if (!$this->toTime($data)) {
  99.                 throw new \Exception('Wrong time format given must be a 5 digit string (eg: 06:49) [ '.$this->getName().' ]');
  100.             }
  101.             if (strlen($this->getMinValue()) && $this->isEarlier($this->getMinValue(), $data)) {
  102.                 throw new \Exception('Value in field [ '.$this->getName().' ] is not at least ' $this->getMinValue());
  103.             }
  104.             if (strlen($this->getMaxValue()) && $this->isLater($this->getMaxValue(), $data)) {
  105.                 throw new \Exception('Value in field [ ' $this->getName() . ' ] is bigger than ' $this->getMaxValue());
  106.             }
  107.         }
  108.     }
  109.     /**
  110.      * {@inheritdoc}
  111.      */
  112.     public function isDiffChangeAllowed($object$params = [])
  113.     {
  114.         return true;
  115.     }
  116.     /**
  117.      * @param string|null $data
  118.      *
  119.      * @return bool
  120.      */
  121.     public function isEmpty($data)
  122.     {
  123.         return strlen($data) !== 5;
  124.     }
  125.     /**
  126.      * Returns a 5 digit time string of a given time
  127.      *
  128.      * @param string $timestamp
  129.      *
  130.      * @return null|string
  131.      */
  132.     private function toTime($timestamp)
  133.     {
  134.         $timestamp strtotime($timestamp);
  135.         if (!$timestamp) {
  136.             return null;
  137.         }
  138.         return date('H:i'$timestamp);
  139.     }
  140.     /**
  141.      * Returns a timestamp representation of a given time
  142.      *
  143.      * @param string $string
  144.      * @param int|null $baseTimestamp
  145.      *
  146.      * @return int
  147.      */
  148.     private function toTimestamp($string$baseTimestamp null)
  149.     {
  150.         if ($baseTimestamp === null) {
  151.             $baseTimestamp time();
  152.         }
  153.         return strtotime($string$baseTimestamp);
  154.     }
  155.     /**
  156.      * Returns whether or not a time is earlier than the subject
  157.      *
  158.      * @param string $subject
  159.      * @param string $comparison
  160.      *
  161.      * @return bool
  162.      */
  163.     private function isEarlier($subject$comparison)
  164.     {
  165.         $baseTs time();
  166.         return $this->toTimestamp($subject$baseTs) > $this->toTimestamp($comparison$baseTs);
  167.     }
  168.     /**
  169.      * Returns whether or not a time is later than the subject
  170.      *
  171.      * @param string $subject
  172.      * @param string $comparison
  173.      *
  174.      * @return bool
  175.      */
  176.     private function isLater($subject$comparison)
  177.     {
  178.         $baseTs time();
  179.         return $this->toTimestamp($subject$baseTs) < $this->toTimestamp($comparison$baseTs);
  180.     }
  181.     /**
  182.      * {@inheritdoc}
  183.      */
  184.     public function getDataForSearchIndex($object$params = [])
  185.     {
  186.         return '';
  187.     }
  188.     /**
  189.      * @return int
  190.      */
  191.     public function getIncrement()
  192.     {
  193.         return $this->increment;
  194.     }
  195.     /**
  196.      * @param int $increment
  197.      */
  198.     public function setIncrement($increment)
  199.     {
  200.         $this->increment = (int) $increment;
  201.     }
  202. }