Skip to content

Commit

Permalink
Merge pull request #292 from henriquemoody/date_time
Browse files Browse the repository at this point in the history
Improve date and time handling on "Min" and "Max" rules
  • Loading branch information
henriquemoody committed Feb 11, 2015
2 parents 3e46726 + d6855c0 commit d311c5e
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 34 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 4 additions & 4 deletions library/Exceptions/MaxException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}}',
),
);

Expand Down
8 changes: 4 additions & 4 deletions library/Exceptions/MinException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ class MinException extends ValidationException

public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
self::STANDARD => '{{name}} must be greater than {{minValue}}',
self::INCLUSIVE => '{{name}} must be greater than or equals {{minValue}}',
self::STANDARD => '{{name}} must be greater than {{interval}}',
self::INCLUSIVE => '{{name}} must be greater than or equals {{interval}}',
),
self::MODE_NEGATIVE => array(
self::STANDARD => '{{name}} must not be greater than {{minValue}}',
self::INCLUSIVE => '{{name}} must not be greater than or equals {{minValue}}',
self::STANDARD => '{{name}} must not be greater than {{interval}}',
self::INCLUSIVE => '{{name}} must not be greater than or equals {{interval}}',
),
);

Expand Down
36 changes: 36 additions & 0 deletions library/Rules/AbstractInterval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Respect\Validation\Rules;

use Exception;
use DateTime;

abstract class AbstractInterval extends AbstractRule
{
public $interval;
public $inclusive;

public function __construct($interval, $inclusive = false)
{
$this->interval = $interval;
$this->inclusive = $inclusive;
}

protected function filterInterval($value)
{
if (!is_string($value) || is_numeric($value) || empty($value)) {
return $value;
}

if (strlen($value) == 1) {
return $value;
}

try {
return new DateTime($value);
} catch (Exception $e) {
// Pokémon Exception Handling
}

return $value;
}
}
17 changes: 4 additions & 13 deletions library/Rules/Max.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
17 changes: 4 additions & 13 deletions library/Rules/Min.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
<?php
namespace Respect\Validation\Rules;

class Min extends AbstractRule
class Min extends AbstractInterval
{
public $inclusive;
public $minValue;

public function __construct($minValue, $inclusive = false)
{
$this->minValue = $minValue;
$this->inclusive = $inclusive;
}

public function validate($input)
{
if ($this->inclusive) {
return $input >= $this->minValue;
} else {
return $input > $this->minValue;
return $this->filterInterval($input) >= $this->filterInterval($this->interval);
}

return $this->filterInterval($input) > $this->filterInterval($this->interval);
}
}
4 changes: 4 additions & 0 deletions tests/Rules/MaxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function providerForValidMax()
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'),
);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Rules/MinTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Respect\Validation\Rules;

use DateTime;

class MinTest extends \PHPUnit_Framework_TestCase
{
/**
Expand Down Expand Up @@ -35,6 +37,15 @@ public function providerForValidMin()
array(-100, false, 200),
array(200, true, 200),
array(200, false, 300),
array('a', true, 'a'),
array('a', true, 'c'),
array('yesterday', true, 'now'),

// Samples from issue #178
array('13-05-2014 03:16', true, '20-05-2014 03:16'),
array(new DateTime('13-05-2014 03:16'), true, new DateTime('20-05-2014 03:16')),
array('13-05-2014 03:16', true, new DateTime('20-05-2014 03:16')),
array(new DateTime('13-05-2014 03:16'), true, '20-05-2014 03:16'),
);
}

Expand Down

0 comments on commit d311c5e

Please sign in to comment.