| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Vendors\StellarWP\Validation\Rules; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use DateTimeImmutable; |
| 7 |
use DateTimeInterface; |
| 8 |
use Exception; |
| 9 |
use Give\Vendors\StellarWP\Validation\Contracts\Sanitizer; |
| 10 |
use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd; |
| 11 |
use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; |
| 12 |
|
| 13 |
/** |
| 14 |
* This rule validates that the given value is a valid date. |
| 15 |
* |
| 16 |
* @since 1.2.0 |
| 17 |
*/ |
| 18 |
class DateTime implements ValidationRule, ValidatesOnFrontEnd, Sanitizer |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @var string|null |
| 22 |
*/ |
| 23 |
protected $format; |
| 24 |
|
| 25 |
/** |
| 26 |
* @since 1.2.0 |
| 27 |
*/ |
| 28 |
public static function id(): string |
| 29 |
{ |
| 30 |
return 'dateTime'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @since 1.2.0 |
| 35 |
*/ |
| 36 |
public static function fromString(?string $options = null): ValidationRule |
| 37 |
{ |
| 38 |
// @phpstan-ignore-next-line |
| 39 |
return new static($options); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @since 1.2.0 |
| 44 |
*/ |
| 45 |
public function __construct(?string $format = null) |
| 46 |
{ |
| 47 |
$this->format = $format; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @since 1.2.0 |
| 52 |
*/ |
| 53 |
public function __invoke($value, Closure $fail, string $key, array $values) |
| 54 |
{ |
| 55 |
if ($value instanceof DateTimeInterface) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$failedValidation = function () use ($fail) { |
| 60 |
$fail(sprintf(__('%s must be a valid date', 'give'), '{field}')); |
| 61 |
}; |
| 62 |
|
| 63 |
try { |
| 64 |
if (!is_string($value) && !is_numeric($value)) { |
| 65 |
$failedValidation(); |
| 66 |
|
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
if ($this->format !== null) { |
| 71 |
$date = \DateTime::createFromFormat($this->format, $value); |
| 72 |
if ($date === false || $date->format($this->format) !== $value) { |
| 73 |
$failedValidation(); |
| 74 |
|
| 75 |
return; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (strtotime($value) === false) { |
| 80 |
$failedValidation(); |
| 81 |
|
| 82 |
return; |
| 83 |
} |
| 84 |
} catch (Exception $exception) { |
| 85 |
$failedValidation(); |
| 86 |
|
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
$date = date_parse($value); |
| 91 |
|
| 92 |
if (!checkdate($date['month'], $date['day'], $date['year'])) { |
| 93 |
$failedValidation(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @since 1.2.0 |
| 99 |
*/ |
| 100 |
public function sanitize($value) |
| 101 |
{ |
| 102 |
if ($value instanceof DateTimeInterface) { |
| 103 |
return $value; |
| 104 |
} |
| 105 |
|
| 106 |
if ($this->format !== null) { |
| 107 |
return DateTimeImmutable::createFromFormat($this->format, $value); |
| 108 |
} |
| 109 |
|
| 110 |
return new DateTimeImmutable($value); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @since 1.2.0 |
| 115 |
*/ |
| 116 |
public function serializeOption() |
| 117 |
{ |
| 118 |
return $this->format; |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|