Improve DateTimeValidator: add time part if not mandatory

master 3.9.0
Julien Rosset 2 months ago
parent fefb3bf3c1
commit 8152f9a673

@ -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;
}
}
Loading…
Cancel
Save