Skip to content
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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,7 +1,7 @@
<?php
namespace Respect\Validation\Exceptions;

class MinimumAgeException extends ValidationException
class AgeException extends ValidationException
{
public static $defaultTemplates = array(
self::MODE_DEFAULT => array(
Expand Down
51 changes: 51 additions & 0 deletions library/Rules/Age.php
@@ -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;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space here.

33 changes: 7 additions & 26 deletions library/Rules/MinimumAge.php
Expand Up @@ -3,33 +3,14 @@

use DateTime;

class MinimumAge extends AbstractRule
/**
Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}
69 changes: 69 additions & 0 deletions tests/Rules/AgeTest.php
@@ -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'),
);
}
}
23 changes: 12 additions & 11 deletions tests/Rules/MininumAgeTest.php
Expand Up @@ -6,39 +6,40 @@
class MinimumAgeTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerForValidDateValidMinimumAge
* @dataProvider providerForValidDateValidAge
*/
public function testValidMinimumAgeInsideBoundsShouldPass($age, $format, $input)
public function testValidAgeInsideBoundsShouldPass($age, $format, $input)
{
$minimumAge = new MinimumAge($age, $format);
$maximumAge = null;
$minimumAge = new Age($age, $maximumAge, $format);
$this->assertTrue($minimumAge->__invoke($input));
$this->assertTrue($minimumAge->assert($input));
$this->assertTrue($minimumAge->check($input));
}

/**
* @dataProvider providerForValidDateInvalidMinimumAge
* @expectedException Respect\Validation\Exceptions\MinimumAgeException
* @dataProvider providerForValidDateInvalidAge
* @expectedException Respect\Validation\Exceptions\AgeException
*/
public function testInvalidMinimumAgeShouldThrowException($age, $format, $input)
public function testInvalidAgeShouldThrowException($age, $format, $input)
{
$minimumAge = new MinimumAge($age, $format);
$minimumAge = new Age($age, $format);
$this->assertFalse($minimumAge->__invoke($input));
$this->assertFalse($minimumAge->assert($input));
}

/**
* @dataProvider providerForInvalidDate
* @expectedException Respect\Validation\Exceptions\MinimumAgeException
* @expectedException Respect\Validation\Exceptions\AgeException
*/
public function testInvalidDateShouldNotPass($age, $format, $input)
{
$minimumAge = new MinimumAge($age, $format);
$minimumAge = new Age($age, $format);
$this->assertFalse($minimumAge->__invoke($input));
$this->assertFalse($minimumAge->assert($input));
}

public function providerForValidDateValidMinimumAge()
public function providerForValidDateValidAge()
{
return array(
array(18, 'Y-m-d', ''),
Expand All @@ -48,7 +49,7 @@ public function providerForValidDateValidMinimumAge()
);
}

public function providerForValidDateInvalidMinimumAge()
public function providerForValidDateInvalidAge()
{
return array(
array(18, 'Y-m-d', '2002-06-30'),
Expand Down
69 changes: 69 additions & 0 deletions tests/library/Respect/Validation/Rules/Age.php
@@ -0,0 +1,69 @@
<?php
namespace Respect\Validation\Rules;

use DateTime;

class AgeTest extends \PHPUnit_Framework_TestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong file name, also wrong path, it must be tests/Rules/AgeTest.php

Copy link
Author

Choose a reason for hiding this comment

The 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'),
);
}
}