New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add rule "Age" to validation. #150
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
namespace Respect\Validation\Rules; | ||
|
||
use DateTime; | ||
|
||
class Age extends AbstractRule | ||
{ | ||
private $maxAge = null; | ||
private $minAge = null; | ||
|
||
public function __construct($min, $max = null) | ||
{ | ||
$this->minAge = $min; | ||
$this->maxAge = $max; | ||
} | ||
|
||
public function validate($input) | ||
{ | ||
if (! is_int($this->minAge) || ! is_int($this->maxAge)) { | ||
return false; | ||
} | ||
|
||
if ($input instanceof DateTime) { | ||
return $this->dateTimeResolver(); | ||
} | ||
|
||
if (!is_string($input) || (is_null($this->format) && false === strtotime($input))) { | ||
return false; | ||
} | ||
|
||
$age = ((date('Ymd') - date('Ymd', strtotime($input))) / 10000); | ||
|
||
if ($this->maxAge) { | ||
return $age >= $this->minAge and $age <= $this->maxAge; | ||
} | ||
|
||
return $age >= $this->minAge; | ||
} | ||
|
||
private function dateTimeResolver($input) | ||
{ | ||
$birthday = new DateTime('now - '.$this->minAge.' year'); | ||
|
||
if ($this->maxAge) { | ||
$limit = new DateTime('now + '.$this->maxAge. ' year'); | ||
return $birthday > $input and $limit < $input; | ||
} | ||
|
||
return $birthday > $input; | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,33 +3,14 @@ | |
|
||
use DateTime; | ||
|
||
class MinimumAge extends AbstractRule | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do the same of what was made in Vowels rule, them we can remove it in the next major release. |
||
* @deprecated | ||
*/ | ||
class MinimumAge extends Age | ||
{ | ||
public $age = null; | ||
public $format = null; | ||
|
||
public function __construct($age, $format = null) | ||
{ | ||
$this->age = $age; | ||
$this->format = $format; | ||
} | ||
|
||
public function validate($input) | ||
public function __construct($min, $max = null) | ||
{ | ||
if (!is_int($this->age)) { | ||
return false; | ||
} | ||
|
||
if ($input instanceof DateTime) { | ||
$birthday = new \DateTime('now - '.$this->age.' year'); | ||
|
||
return $birthday > $input; | ||
} elseif (!is_string($input) || (is_null($this->format) && false === strtotime($input))) { | ||
return false; | ||
} else { | ||
$age = ((date('Ymd') - date('Ymd', strtotime($input))) / 10000); | ||
|
||
return $age >= $this->age; | ||
} | ||
parent::__construct($min, $max = null); | ||
trigger_error("Use Age instead.", E_USER_DEPRECATED); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
namespace Respect\Validation\Rules; | ||
|
||
use DateTime; | ||
|
||
class AgeTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider providerForValidDateValidAge | ||
*/ | ||
public function testValidAgeInsideBoundsShouldPass($age, $format, $input) | ||
{ | ||
$maximumAge = null; | ||
$minimumAge = new Age($age, $maximumAge, $format); | ||
$this->assertTrue($minimumAge->__invoke($input)); | ||
$this->assertTrue($minimumAge->assert($input)); | ||
$this->assertTrue($minimumAge->check($input)); | ||
} | ||
|
||
/** | ||
* @dataProvider providerForValidDateInvalidAge | ||
* @expectedException Respect\Validation\Exceptions\AgeException | ||
*/ | ||
public function testInvalidAgeShouldThrowException($age, $format, $input) | ||
{ | ||
$minimumAge = new Age($age, $format); | ||
$this->assertFalse($minimumAge->__invoke($input)); | ||
$this->assertFalse($minimumAge->assert($input)); | ||
} | ||
|
||
/** | ||
* @dataProvider providerForInvalidDate | ||
* @expectedException Respect\Validation\Exceptions\AgeException | ||
*/ | ||
public function testInvalidDateShouldNotPass($age, $format, $input) | ||
{ | ||
$minimumAge = new Age($age, $format); | ||
$this->assertFalse($minimumAge->__invoke($input)); | ||
$this->assertFalse($minimumAge->assert($input)); | ||
} | ||
|
||
public function providerForValidDateValidAge() | ||
{ | ||
return array( | ||
array(18, 'Y-m-d', ''), | ||
array(18, 'Y-m-d', '1969-07-20'), | ||
array(18, null, new \DateTime('1969-07-20')), | ||
array(18, 'Y-m-d', new \DateTime('1969-07-20')), | ||
); | ||
} | ||
|
||
public function providerForValidDateInvalidAge() | ||
{ | ||
return array( | ||
array(18, 'Y-m-d', '2002-06-30'), | ||
array(18, null, new \DateTime('2002-06-30')), | ||
array(18, 'Y-m-d', new \DateTime('2002-06-30')), | ||
); | ||
} | ||
|
||
public function providerForInvalidDate() | ||
{ | ||
return array( | ||
array(18, null, 'invalid-input'), | ||
array(18, null, new \stdClass), | ||
array(18, 'y-m-d', '2002-06-30'), | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
namespace Respect\Validation\Rules; | ||
|
||
use DateTime; | ||
|
||
class AgeTest extends \PHPUnit_Framework_TestCase | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrong file name, also wrong path, it must be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My fail, that need to be removed |
||
{ | ||
/** | ||
* @dataProvider providerForValidDateValidAge | ||
*/ | ||
public function testValidMinimumAgeInsideBoundsShouldPass($minAge, $maxAge, $input) | ||
{ | ||
$minimumAge = new Age($minAge, $maxAge); | ||
$this->assertTrue($minimumAge->__invoke($input)); | ||
$this->assertTrue($minimumAge->assert($input)); | ||
$this->assertTrue($minimumAge->check($input)); | ||
} | ||
|
||
/** | ||
* @dataProvider providerForValidDateInvalidAge | ||
* @expectedException Respect\Validation\Exceptions\AgeException | ||
*/ | ||
public function testInvalidMinimumAgeShouldThrowException($minAge, $maxAge, $input) | ||
{ | ||
$minimumAge = new Age($minAge, $maxAge); | ||
$this->assertFalse($minimumAge->__invoke($input)); | ||
$this->assertFalse($minimumAge->assert($input)); | ||
} | ||
|
||
/** | ||
* @dataProvider providerForInvalidDate | ||
* @expectedException Respect\Validation\Exceptions\AgeException | ||
*/ | ||
public function testInvalidDateShouldNotPass($minAge, $maxAge, $input) | ||
{ | ||
$minimumAge = new Age($minAge, $maxAge); | ||
$this->assertFalse($minimumAge->__invoke($input)); | ||
$this->assertFalse($minimumAge->assert($input)); | ||
} | ||
|
||
public function providerForValidDateValidAge() | ||
{ | ||
return array( | ||
array(18, 25, ''), | ||
array(18, 25, '1969-07-20'), | ||
array(18, null, new \DateTime('1969-07-20')), | ||
array(18, 25, new \DateTime('1969-07-20')), | ||
); | ||
} | ||
|
||
public function providerForValidDateInvalidAge() | ||
{ | ||
return array( | ||
array(18, 25, '2002-06-30'), | ||
array(18, null, new \DateTime('2002-06-30')), | ||
array(18, 35, new \DateTime('2002-06-30')), | ||
); | ||
} | ||
|
||
public function providerForInvalidDate() | ||
{ | ||
return array( | ||
array(18, null, 'invalid-input'), | ||
array(18, 25, new \stdClass), | ||
array(18, 25, '2002-06-30'), | ||
); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space here.