Improve date and time handling on "Max" rule

This commit is contained in:
Henrique Moody 2015-02-11 11:02:20 -02:00
parent e91d0ef22e
commit d6855c0722
4 changed files with 19 additions and 17 deletions

View file

@ -1406,6 +1406,13 @@ Also accepts dates:
v::date()->max('2012-01-01')->validate('2010-01-01'); //true
```
Also date intervals:
```php
// Same of minimum age validation
v::date()->max('-18 years')->validate('1988-09-09'); //true
```
`true` may be passed as a parameter to indicate that inclusive
values must be used.

View file

@ -7,12 +7,12 @@ class MaxException extends ValidationException
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be lower than {{maxValue}}',
self::INCLUSIVE => '{{name}} must be lower than or equals {{maxValue}}',
self::STANDARD => '{{name}} must be lower than {{interval}}',
self::INCLUSIVE => '{{name}} must be lower than or equals {{interval}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be lower than {{maxValue}}',
self::INCLUSIVE => '{{name}} must not be lower than or equals {{maxValue}}',
self::STANDARD => '{{name}} must not be lower than {{interval}}',
self::INCLUSIVE => '{{name}} must not be lower than or equals {{interval}}',
),
);

View file

@ -1,23 +1,14 @@
<?php
namespace Respect\Validation\Rules;
class Max extends AbstractRule
class Max extends AbstractInterval
{
public $maxValue;
public $inclusive;
public function __construct($maxValue, $inclusive = false)
{
$this->maxValue = $maxValue;
$this->inclusive = $inclusive;
}
public function validate($input)
{
if ($this->inclusive) {
return $input <= $this->maxValue;
} else {
return $input < $this->maxValue;
return $this->filterInterval($input) <= $this->filterInterval($this->interval);
}
return $this->filterInterval($input) < $this->filterInterval($this->interval);
}
}

View file

@ -33,6 +33,10 @@ class MaxTest extends \PHPUnit_Framework_TestCase
array(200, false, -200),
array(200, true, 200),
array(200, false, 0),
array('-18 years', true, '1988-09-09'),
array('z', true, 'z'),
array('z', false, 'y'),
array('tomorrow', true, 'now'),
);
}