From 7f427c99ac1aa87cac2188f1a67383c47f52c51e Mon Sep 17 00:00:00 2001 From: MartkCz Date: Tue, 30 Jan 2024 12:10:21 +0100 Subject: [PATCH] DateInput: supports DateTimeImmutable --- src/DateInput.php | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/DateInput.php b/src/DateInput.php index fc545a2..a2f0612 100755 --- a/src/DateInput.php +++ b/src/DateInput.php @@ -3,6 +3,9 @@ namespace WebChemistry\Controls; use DateTime; +use DateTimeImmutable; +use DateTimeInterface; +use LogicException; use Nette; use Nette\Forms\Controls\TextInput; use Nette\Forms\Validator; @@ -28,12 +31,20 @@ final class DateInput extends TextInput { /** @var int */ private $type = self::DATE; + private $immutable = false; + public function __construct($label = null, int $maxLength = null) { parent::__construct($label, $maxLength); $this->setHtmlType('date'); } + public function setImmutable($immutable = true) { + $this->immutable = $immutable; + + return $this; + } + public function setDateTime() { $this->setHtmlType('datetime-local'); $this->type = self::DATETIME; @@ -62,9 +73,13 @@ protected function getHttpData($type, ?string $htmlTail = null) { try { $date = null; if ($str) { - $date = new \DateTime($str); + if (!$this->immutable) { + $date = new DateTime($str); + } else { + $date = new DateTimeImmutable($str); + } } - } catch (\Throwable $e) { + } catch (Throwable $e) { $msg = Validator::$messages[self::DATE_VALID] ?? 'Date is not correct.'; $this->addError($msg); @@ -99,16 +114,20 @@ public function getControl(): Nette\Utils\Html { public function setValue($value) { if (is_string($value)) { try { - $value = new DateTime($value); + if (!$this->immutable) { + $value = new DateTime($value); + } else { + $value = new DateTimeImmutable($value); + } } catch (Throwable $exception) { - throw new \LogicException( + throw new LogicException( sprintf('Cannot create datetime from string, %s given.', $value), 0, $exception ); } - } else if ($value !== null && !$value instanceof DateTime) { - throw new \LogicException("Must be a string or DateTime or null."); + } else if ($value !== null && !$value instanceof DateTimeInterface) { + throw new LogicException("Must be a string or DateTime or DateTimeImmutable or null."); } $this->value = $value; @@ -116,7 +135,10 @@ public function setValue($value) { return $this; } - public function getValue(): ?DateTime { + /** + * @return DateTime|DateTimeImmutable|null + */ + public function getValue() { return $this->value; }