-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Icinga/add-class-DateTimeValidator
Add class DateTimeValidator
- Loading branch information
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |