Skip to content

Commit d311c5e

Browse files
committed
Merge pull request #292 from henriquemoody/date_time
Improve date and time handling on "Min" and "Max" rules
2 parents 3e46726 + d6855c0 commit d311c5e

File tree

8 files changed

+74
-34
lines changed

8 files changed

+74
-34
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,13 @@ Also accepts dates:
14061406
v::date()->max('2012-01-01')->validate('2010-01-01'); //true
14071407
```
14081408

1409+
Also date intervals:
1410+
1411+
```php
1412+
// Same of minimum age validation
1413+
v::date()->max('-18 years')->validate('1988-09-09'); //true
1414+
```
1415+
14091416
`true` may be passed as a parameter to indicate that inclusive
14101417
values must be used.
14111418

library/Exceptions/MaxException.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class MaxException extends ValidationException
77

88
public static $defaultTemplates = array(
99
self::MODE_DEFAULT => array(
10-
self::STANDARD => '{{name}} must be lower than {{maxValue}}',
11-
self::INCLUSIVE => '{{name}} must be lower than or equals {{maxValue}}',
10+
self::STANDARD => '{{name}} must be lower than {{interval}}',
11+
self::INCLUSIVE => '{{name}} must be lower than or equals {{interval}}',
1212
),
1313
self::MODE_NEGATIVE => array(
14-
self::STANDARD => '{{name}} must not be lower than {{maxValue}}',
15-
self::INCLUSIVE => '{{name}} must not be lower than or equals {{maxValue}}',
14+
self::STANDARD => '{{name}} must not be lower than {{interval}}',
15+
self::INCLUSIVE => '{{name}} must not be lower than or equals {{interval}}',
1616
),
1717
);
1818

library/Exceptions/MinException.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class MinException extends ValidationException
77

88
public static $defaultTemplates = array(
99
self::MODE_DEFAULT => array(
10-
self::STANDARD => '{{name}} must be greater than {{minValue}}',
11-
self::INCLUSIVE => '{{name}} must be greater than or equals {{minValue}}',
10+
self::STANDARD => '{{name}} must be greater than {{interval}}',
11+
self::INCLUSIVE => '{{name}} must be greater than or equals {{interval}}',
1212
),
1313
self::MODE_NEGATIVE => array(
14-
self::STANDARD => '{{name}} must not be greater than {{minValue}}',
15-
self::INCLUSIVE => '{{name}} must not be greater than or equals {{minValue}}',
14+
self::STANDARD => '{{name}} must not be greater than {{interval}}',
15+
self::INCLUSIVE => '{{name}} must not be greater than or equals {{interval}}',
1616
),
1717
);
1818

library/Rules/AbstractInterval.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace Respect\Validation\Rules;
3+
4+
use Exception;
5+
use DateTime;
6+
7+
abstract class AbstractInterval extends AbstractRule
8+
{
9+
public $interval;
10+
public $inclusive;
11+
12+
public function __construct($interval, $inclusive = false)
13+
{
14+
$this->interval = $interval;
15+
$this->inclusive = $inclusive;
16+
}
17+
18+
protected function filterInterval($value)
19+
{
20+
if (!is_string($value) || is_numeric($value) || empty($value)) {
21+
return $value;
22+
}
23+
24+
if (strlen($value) == 1) {
25+
return $value;
26+
}
27+
28+
try {
29+
return new DateTime($value);
30+
} catch (Exception $e) {
31+
// Pokémon Exception Handling
32+
}
33+
34+
return $value;
35+
}
36+
}

library/Rules/Max.php

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
<?php
22
namespace Respect\Validation\Rules;
33

4-
class Max extends AbstractRule
4+
class Max extends AbstractInterval
55
{
6-
public $maxValue;
7-
public $inclusive;
8-
9-
public function __construct($maxValue, $inclusive = false)
10-
{
11-
$this->maxValue = $maxValue;
12-
$this->inclusive = $inclusive;
13-
}
14-
156
public function validate($input)
167
{
178
if ($this->inclusive) {
18-
return $input <= $this->maxValue;
19-
} else {
20-
return $input < $this->maxValue;
9+
return $this->filterInterval($input) <= $this->filterInterval($this->interval);
2110
}
11+
12+
return $this->filterInterval($input) < $this->filterInterval($this->interval);
2213
}
2314
}

library/Rules/Min.php

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
<?php
22
namespace Respect\Validation\Rules;
33

4-
class Min extends AbstractRule
4+
class Min extends AbstractInterval
55
{
6-
public $inclusive;
7-
public $minValue;
8-
9-
public function __construct($minValue, $inclusive = false)
10-
{
11-
$this->minValue = $minValue;
12-
$this->inclusive = $inclusive;
13-
}
14-
156
public function validate($input)
167
{
178
if ($this->inclusive) {
18-
return $input >= $this->minValue;
19-
} else {
20-
return $input > $this->minValue;
9+
return $this->filterInterval($input) >= $this->filterInterval($this->interval);
2110
}
11+
12+
return $this->filterInterval($input) > $this->filterInterval($this->interval);
2213
}
2314
}

tests/Rules/MaxTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public function providerForValidMax()
3333
array(200, false, -200),
3434
array(200, true, 200),
3535
array(200, false, 0),
36+
array('-18 years', true, '1988-09-09'),
37+
array('z', true, 'z'),
38+
array('z', false, 'y'),
39+
array('tomorrow', true, 'now'),
3640
);
3741
}
3842

tests/Rules/MinTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Respect\Validation\Rules;
33

4+
use DateTime;
5+
46
class MinTest extends \PHPUnit_Framework_TestCase
57
{
68
/**
@@ -35,6 +37,15 @@ public function providerForValidMin()
3537
array(-100, false, 200),
3638
array(200, true, 200),
3739
array(200, false, 300),
40+
array('a', true, 'a'),
41+
array('a', true, 'c'),
42+
array('yesterday', true, 'now'),
43+
44+
// Samples from issue #178
45+
array('13-05-2014 03:16', true, '20-05-2014 03:16'),
46+
array(new DateTime('13-05-2014 03:16'), true, new DateTime('20-05-2014 03:16')),
47+
array('13-05-2014 03:16', true, new DateTime('20-05-2014 03:16')),
48+
array(new DateTime('13-05-2014 03:16'), true, '20-05-2014 03:16'),
3849
);
3950
}
4051

0 commit comments

Comments
 (0)