Skip to content

Commit

Permalink
Merge pull request #3 from Icinga/add-class-DateTimeValidator
Browse files Browse the repository at this point in the history
Add class DateTimeValidator
  • Loading branch information
nilmerg authored Oct 7, 2021
2 parents c36286a + 05230ab commit e32eeec
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/DateTimeValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace ipl\Validator;

use DateTime;
use ipl\I18n\Translation;

/**
* Validator for date-and-time input controls
*/
class DateTimeValidator extends BaseValidator
{
use Translation;

/** @var string Default date time format */
const FORMAT = 'Y-m-d\TH:i:s';

/** @var bool Whether to use the default date time format */
protected $local;

/**
* Create a new date-and-time input control validator
*
* @param bool $local
*/
public function __construct($local = true)
{
$this->local = (bool) $local;
}

/**
* Check whether the given date time is valid
*
* @param string|DateTime $value
*
* @return bool
*/
public function isValid($value)
{
// Multiple isValid() calls must not stack validation messages
$this->clearMessages();

if (! $value instanceof DateTime && ! is_string($value)) {
$this->addMessage($this->translate('Invalid date/time given.'));

return false;
}

if (! $value instanceof DateTime) {
$format = $this->local === true ? static::FORMAT : DateTime::RFC3339;
$dateTime = DateTime::createFromFormat($format, $value);

if ($dateTime === false || $dateTime->format($format) !== $value) {
$this->addMessage(sprintf(
$this->translate("Date/time string not in the expected format: %s"),
$format
));

return false;
}
}

return true;
}
}
36 changes: 36 additions & 0 deletions tests/DateTimeValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace ipl\Tests\Validator;

use DateTime;
use ipl\I18n\NoopTranslator;
use ipl\I18n\StaticTranslator;
use ipl\Validator\DateTimeValidator;

class DateTimeValidatorTest extends TestCase
{
public function testDateTimeValidatorWithValidDateTime()
{
StaticTranslator::$instance = new NoopTranslator();
$this->assertTrue((new DateTimeValidator())->isValid(new DateTime()), 'current date is a valid date');
}

public function testDateTimeValidatorWithFalseAsDateTimeValue()
{
StaticTranslator::$instance = new NoopTranslator();
$validator = new DateTimeValidator();

$this->assertFalse($validator->isValid(false), 'false is not a valid date');
}

public function testDateTimeValidatorWithStringAsDateTimeValue()
{
StaticTranslator::$instance = new NoopTranslator();
$validator = new DateTimeValidator();

$this->assertTrue($validator->isValid('2021-02-15T15:03:01'), '15th Feb is a valid date');
$this->assertFalse($validator->isValid('2021-02-31T15:03:01'), '31st Feb is not a valid date');
$this->assertFalse($validator->isValid('2021-02-03T26:03:01'), "26 o'clock is not a valid time");
$this->assertFalse($validator->isValid(''), 'Empty value is not a valid date');
}
}

0 comments on commit e32eeec

Please sign in to comment.