diff --git a/composer.json b/composer.json index 600e4049d..b33767a84 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,7 @@ "ext-intl": "for script transliteration in Strings::webalize() and toAscii()", "ext-mbstring": "to use Strings::lower() etc...", "ext-xml": "to use Strings::length() etc. when mbstring is not available", - "ext-gd": "to use Image", - "https://nette.org/donate": "\u001b[1;37;42m Please consider supporting Nette via a donation \u001b[0m" + "ext-gd": "to use Image" }, "conflict": { "nette/nette": "<2.2" diff --git a/src/Utils/Validators.php b/src/Utils/Validators.php index e133ab097..92d7759ea 100644 --- a/src/Utils/Validators.php +++ b/src/Utils/Validators.php @@ -261,9 +261,22 @@ public static function isList($value) */ public static function isInRange($value, $range) { - return $value !== NULL - && (!isset($range[0]) || (is_string($range[0]) ? (string) $value >= $range[0] : is_numeric($value) && $value * 1 >= $range[0])) - && (!isset($range[1]) || (is_string($range[1]) ? (string) $value <= $range[1] : is_numeric($value) && $value * 1 <= $range[1])); + if ($value === NULL || !(isset($range[0]) || isset($range[1]))) { + return FALSE; + } + $limit = isset($range[0]) ? $range[0] : $range[1]; + if (is_string($limit)) { + $value = (string) $value; + } elseif ($limit instanceof \DateTimeInterface) { + if (!$value instanceof \DateTimeInterface) { + return FALSE; + } + } elseif (is_numeric($value)) { + $value *= 1; + } else { + return FALSE; + } + return (!isset($range[0]) || ($value >= $range[0])) && (!isset($range[1]) || ($value <= $range[1])); } diff --git a/tests/Utils/Validators.isInRange().phpt b/tests/Utils/Validators.isInRange().phpt index a5885debe..662a4b7c8 100644 --- a/tests/Utils/Validators.isInRange().phpt +++ b/tests/Utils/Validators.isInRange().phpt @@ -28,6 +28,10 @@ Assert::true(Validators::isInRange(-1, ['', 2])); Assert::true(Validators::isInRange(1, [-1, NULL])); Assert::false(Validators::isInRange(1, [-1, ''])); +Assert::false(Validators::isInRange(0, [NULL, NULL])); +Assert::false(Validators::isInRange('', [NULL, NULL])); +Assert::false(Validators::isInRange(10, [NULL, NULL])); + Assert::false(Validators::isInRange(NULL, [0, 1])); Assert::false(Validators::isInRange(NULL, ['0', 'b'])); @@ -42,3 +46,8 @@ Assert::false(Validators::isInRange('a', [NULL, 9])); Assert::true(Validators::isInRange('1', [NULL, 9])); Assert::false(Validators::isInRange(10, ['a', NULL])); Assert::true(Validators::isInRange(10, [NULL, 'a'])); + +Assert::false(Validators::isInRange(new DateTimeImmutable('2017-04-26'), [new DateTime('2017-04-20'), new DateTime('2017-04-23')])); +Assert::true(Validators::isInRange(new DateTimeImmutable('2017-04-26'), [new DateTime('2017-04-20'), new DateTime('2017-04-30')])); +Assert::false(Validators::isInRange(new DateTimeImmutable('2017-04-26'), [10, NULL])); +Assert::false(Validators::isInRange(new DateTimeImmutable('2017-04-26'), [NULL, 10]));