Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
27 / 27 |
DateTime | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
11 | |
100.00% |
27 / 27 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
validate | |
100.00% |
1 / 1 |
8 | |
100.00% |
15 / 15 |
|||
getInverseError | |
100.00% |
1 / 1 |
2 | |
100.00% |
9 / 9 |
<?php | |
namespace Verja\Validator; | |
use Verja\Error; | |
use Verja\Validator; | |
class DateTime extends Validator | |
{ | |
/** @var string */ | |
protected $format; | |
/** @var bool */ | |
protected $strict; | |
/** | |
* DateTime constructor. | |
* | |
* When strict is set to true the resulting date will be formatted back using the given format and the value | |
* has to equal this value. This is helpful to prevent month and day interchanges but will fail for some natural | |
* date formats like `'dS of F Y' => '21st of January 2016`. | |
* | |
* @param string $format | |
* @param bool $strict | |
*/ | |
public function __construct(string $format = null, bool $strict = false) | |
{ | |
$this->format = $format; | |
$this->strict = $strict; | |
} | |
/** | |
* Validate $value | |
* | |
* @param mixed $value | |
* @param array $context | |
* @return bool | |
*/ | |
public function validate($value, array $context = []): bool | |
{ | |
if ($value instanceof \DateTime) { | |
return true; | |
} | |
if (empty($this->format) && strtotime($value) === false) { | |
$this->error = new Error('NO_DATE', $value, 'value should be a valid date'); | |
return false; | |
} | |
if (!empty($this->format)) { | |
$date = date_create_from_format($this->format, $value); | |
if ($date === false || $this->strict && $date->format($this->format) !== $value) { | |
$this->error = new Error( | |
'NO_FORMATTED_DATE', | |
$value, | |
sprintf('value should be a valid date in format %s', $this->format), | |
[ 'format' => $this->format, 'strict' => $this->strict ] | |
); | |
return false; | |
} | |
} | |
return true; | |
} | |
public function getInverseError($value) | |
{ | |
return is_null($this->format) ? | |
new Error('IS_DATE', $value, 'value should not be a valid date') : | |
new Error( | |
'IS_FORMATTED_DATE', | |
$value, | |
sprintf( | |
'value should not be valid date in format %s', | |
$this->format | |
), | |
[ 'format' => $this->format, 'strict' => $this->strict ] | |
); | |
} | |
} |