3.4.0
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace Kirby\Toolkit;
|
||||
|
||||
use Exception;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Http\Idn;
|
||||
use ReflectionFunction;
|
||||
use Throwable;
|
||||
@@ -239,13 +240,47 @@ V::$validators = [
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks for a valid date
|
||||
* Checks for a valid date or compares two
|
||||
* dates with each other.
|
||||
*
|
||||
* Pass only the first argument to check for a valid date.
|
||||
* Pass an operator as second argument and another date as
|
||||
* third argument to compare them.
|
||||
*/
|
||||
'date' => function ($value): bool {
|
||||
$date = date_parse($value);
|
||||
return $date !== false &&
|
||||
$date['error_count'] === 0 &&
|
||||
$date['warning_count'] === 0;
|
||||
'date' => function (?string $value, string $operator = null, string $test = null): bool {
|
||||
$args = func_get_args();
|
||||
|
||||
// simple date validation
|
||||
if (count($args) === 1) {
|
||||
$date = date_parse($value);
|
||||
return $date !== false &&
|
||||
$date['error_count'] === 0 &&
|
||||
$date['warning_count'] === 0;
|
||||
}
|
||||
|
||||
$value = strtotime($value);
|
||||
$test = strtotime($test);
|
||||
|
||||
if (is_int($value) !== true || is_int($test) !== true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($operator) {
|
||||
case '!=':
|
||||
return $value !== $test;
|
||||
case '<':
|
||||
return $value < $test;
|
||||
case '>':
|
||||
return $value > $test;
|
||||
case '<=':
|
||||
return $value <= $test;
|
||||
case '>=':
|
||||
return $value >= $test;
|
||||
case '==':
|
||||
return $value === $test;
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Invalid date comparison operator: "' . $operator . '". Allowed operators: "==", "!=", "<", "<=", ">", ">="');
|
||||
},
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user