This commit is contained in:
Bastian Allgeier
2020-07-07 12:40:13 +02:00
parent 5f025ac2c2
commit f79d2e960c
176 changed files with 10532 additions and 5343 deletions

View File

@@ -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: "==", "!=", "<", "<=", ">", ">="');
},
/**