diff --git a/src/CliProgram/Validation/Validators/DateTimeValidator.php b/src/CliProgram/Validation/Validators/DateTimeValidator.php index d1ffce9..c69d04c 100644 --- a/src/CliProgram/Validation/Validators/DateTimeValidator.php +++ b/src/CliProgram/Validation/Validators/DateTimeValidator.php @@ -15,14 +15,20 @@ class DateTimeValidator extends AbstractDateTimeValidator { * @var bool Is the time part mandatory ? */ private bool $timeMandatory; + /** + * @var DateTimeInterface The time part (if not mandatory) + */ + private DateTimeInterface $timePart; /** * Create a validator * - * @param bool $mandatoryDate Is the time part mandatory ? + * @param bool $timeMandatory Is the time part mandatory ? + * @param DateTimeInterface|null $timePart The time part (if not mandatory) */ - public function __construct (bool $mandatoryDate = false) { - $this->setTimeMandatory($mandatoryDate); + public function __construct (bool $timeMandatory = false, ?DateTimeInterface $timePart = null) { + $this->setTimeMandatory($timeMandatory); + $this->setTimePart($timePart ?? (new DateTimeImmutable())->setTimestamp(0)); parent::__construct(); } @@ -56,9 +62,9 @@ class DateTimeValidator extends AbstractDateTimeValidator { (int)$this->getInternalValidator()->getMatch('day'), ); return $value->setTime( - (int)($this->getInternalValidator()->getMatch('hour') ?? 0), - (int)($this->getInternalValidator()->getMatch('minute') ?? 0), - (int)($this->getInternalValidator()->getMatch('second') ?? 0), + (int)($this->getInternalValidator()->getMatch('hour') ?? (int)$this->getTimePart()->format('%H')), + (int)($this->getInternalValidator()->getMatch('minute') ?? (int)$this->getTimePart()->format('%i')), + (int)($this->getInternalValidator()->getMatch('second') ?? (int)$this->getTimePart()->format('%s')), ); } @@ -81,4 +87,24 @@ class DateTimeValidator extends AbstractDateTimeValidator { $this->timeMandatory = $timeMandatory; return $this; } + + /** + * The time part (if not mandatory) + * + * @return DateTimeInterface The time part (if not mandatory) + */ + public function getTimePart (): DateTimeInterface { + return $this->timePart; + } + /** + * Set the time part (if not mandatory) + * + * @param DateTimeInterface $timePart The time part + * + * @return $this + */ + public function setTimePart (DateTimeInterface $timePart): self { + $this->timePart = $timePart; + return $this; + } } \ No newline at end of file