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

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 (is_string($minValue) && 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 (is_string($maxValue) && 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)) {
  95.             if (!preg_match('/^(2[0-3]|[01][0-9]):[0-5][0-9]$/'$data) && $data !== '') {
  96.                 throw new Model\Element\ValidationException('Wrong time format given must be a 5 digit string (eg: 06:49) [ '.$this->getName().' ]');
  97.             }
  98.         } elseif (!empty($data)) {
  99.             throw new Model\Element\ValidationException('Wrong time format given must be a 5 digit string (eg: 06:49) [ '.$this->getName().' ]');
  100.         }
  101.         if (!$omitMandatoryCheck && $data) {
  102.             if (!$this->toTime($data)) {
  103.                 throw new Model\Element\ValidationException('Wrong time format given must be a 5 digit string (eg: 06:49) [ '.$this->getName().' ]');
  104.             }
  105.             if ($this->getMinValue() && $this->isEarlier($this->getMinValue(), $data)) {
  106.                 throw new Model\Element\ValidationException('Value in field [ '.$this->getName().' ] is not at least ' $this->getMinValue());
  107.             }
  108.             if ($this->getMaxValue() && $this->isLater($this->getMaxValue(), $data)) {
  109.                 throw new Model\Element\ValidationException('Value in field [ ' $this->getName() . ' ] is bigger than ' $this->getMaxValue());
  110.             }
  111.         }
  112.     }
  113.     /**
  114.      * {@inheritdoc}
  115.      */
  116.     public function isDiffChangeAllowed($object$params = [])
  117.     {
  118.         return true;
  119.     }
  120.     /**
  121.      * @param string|null $data
  122.      *
  123.      * @return bool
  124.      */
  125.     public function isEmpty($data)
  126.     {
  127.         return !is_string($data) || !preg_match('/^(2[0-3]|[01][0-9]):[0-5][0-9]$/'$data);
  128.     }
  129.     /**
  130.      * Returns a 5 digit time string of a given time
  131.      *
  132.      * @param string $timestamp
  133.      *
  134.      * @return null|string
  135.      */
  136.     private function toTime($timestamp)
  137.     {
  138.         $timestamp strtotime($timestamp);
  139.         if (!$timestamp) {
  140.             return null;
  141.         }
  142.         return date('H:i'$timestamp);
  143.     }
  144.     /**
  145.      * Returns a timestamp representation of a given time
  146.      *
  147.      * @param string $string
  148.      * @param int|null $baseTimestamp
  149.      *
  150.      * @return int
  151.      */
  152.     private function toTimestamp($string$baseTimestamp null)
  153.     {
  154.         if ($baseTimestamp === null) {
  155.             $baseTimestamp time();
  156.         }
  157.         return strtotime($string$baseTimestamp);
  158.     }
  159.     /**
  160.      * Returns whether or not a time is earlier than the subject
  161.      *
  162.      * @param string $subject
  163.      * @param string $comparison
  164.      *
  165.      * @return bool
  166.      */
  167.     private function isEarlier($subject$comparison)
  168.     {
  169.         $baseTs time();
  170.         return $this->toTimestamp($subject$baseTs) > $this->toTimestamp($comparison$baseTs);
  171.     }
  172.     /**
  173.      * Returns whether or not a time is later than the subject
  174.      *
  175.      * @param string $subject
  176.      * @param string $comparison
  177.      *
  178.      * @return bool
  179.      */
  180.     private function isLater($subject$comparison)
  181.     {
  182.         $baseTs time();
  183.         return $this->toTimestamp($subject$baseTs) < $this->toTimestamp($comparison$baseTs);
  184.     }
  185.     /**
  186.      * {@inheritdoc}
  187.      */
  188.     public function getDataForSearchIndex($object$params = [])
  189.     {
  190.         return '';
  191.     }
  192.     /**
  193.      * @return int
  194.      */
  195.     public function getIncrement()
  196.     {
  197.         return $this->increment;
  198.     }
  199.     /**
  200.      * @param int $increment
  201.      */
  202.     public function setIncrement($increment)
  203.     {
  204.         $this->increment = (int) $increment;
  205.     }
  206. }