Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Throws an InvalidArgumentException when one attempts to create a DateTime element with a string that does not conform to the expected \DateTime format.
  • Loading branch information
jackdpeterson authored Apr 27, 2017
1 parent 083f794 commit 6dac363
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions src/Element/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DateInterval;
use DateTime as PhpDateTime;
use Zend\Form\Element;
use Zend\Form\Exception\InvalidArgumentException;
use Zend\InputFilter\InputProviderInterface;
use Zend\Validator\Date as DateValidator;
use Zend\Validator\DateStep as DateStepValidator;
Expand Down Expand Up @@ -77,10 +78,11 @@ public function setOptions($options)
public function getValue($returnFormattedValue = true)
{
$value = parent::getValue();
if (! $value instanceof PhpDateTime || ! $returnFormattedValue) {
if (!$value instanceof PhpDateTime || !$returnFormattedValue) {
return $value;
}
$format = $this->getFormat();

return $value->format($format);
}

Expand All @@ -92,7 +94,8 @@ public function getValue($returnFormattedValue = true)
*/
public function setFormat($format)
{
$this->format = (string) $format;
$this->format = (string)$format;

return $this;
}

Expand Down Expand Up @@ -120,25 +123,47 @@ protected function getValidators()
$validators = [];
$validators[] = $this->getDateValidator();

if (isset($this->attributes['min'])) {
if (isset($this->attributes['min']) && \DateTime::createFromFormat(self::DATETIME_FORMAT,
$this->attributes['min']) instanceof \DateTimeInterface
) {
$validators[] = new GreaterThanValidator([
'min' => $this->attributes['min'],
'min' => $this->attributes['min'],
'inclusive' => true,
]);
} elseif (isset($this->attributes['min']) && !\DateTime::createFromFormat(self::DATETIME_FORMAT,
$this->attributes['min']) instanceof \DateTimeInterface
) {
throw new InvalidArgumentException(sprintf(
'%1$s expects "min" to conform to %2$s; received "%3$s"', __METHOD__, self::DATETIME_FORMAT,
$this->attributes['min']
));
}
if (isset($this->attributes['max'])) {

if (isset($this->attributes['max']) && \DateTime::createFromFormat(self::DATETIME_FORMAT,
$this->attributes['max']) instanceof \DateTimeInterface
) {
$validators[] = new LessThanValidator([
'max' => $this->attributes['max'],
'max' => $this->attributes['max'],
'inclusive' => true,
]);
} elseif (isset($this->attributes['max']) && !\DateTime::createFromFormat(self::DATETIME_FORMAT,
$this->attributes['max']) instanceof \DateTimeInterface
) {
throw new InvalidArgumentException(sprintf(
'%1$s expects "max" to conform to %2$s; received "%3$s"', __METHOD__, self::DATETIME_FORMAT,
$this->attributes['max']
));
}
if (! isset($this->attributes['step'])


if (!isset($this->attributes['step'])
|| 'any' !== $this->attributes['step']
) {
$validators[] = $this->getStepValidator();
}

$this->validators = $validators;

return $this->validators;
}

Expand All @@ -159,17 +184,18 @@ protected function getDateValidator()
*/
protected function getStepValidator()
{
$format = $this->getFormat();
$format = $this->getFormat();
$stepValue = (isset($this->attributes['step']))
? $this->attributes['step'] : 1; // Minutes
? $this->attributes['step'] : 1; // Minutes

$baseValue = (isset($this->attributes['min']))
? $this->attributes['min'] : date($format, 0);
$baseValue = (isset($this->attributes['min']) && \DateTime::createFromFormat(self::DATETIME_FORMAT,
$this->attributes['min']) instanceof \DateTimeInterface)
? $this->attributes['min'] : date($format, 0);

return new DateStepValidator([
'format' => $format,
'format' => $format,
'baseValue' => $baseValue,
'step' => new DateInterval("PT{$stepValue}M"),
'step' => new DateInterval("PT{$stepValue}M"),
]);
}

Expand Down

0 comments on commit 6dac363

Please sign in to comment.