Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add rule "Age" to validation. #150

Closed
wants to merge 5 commits into from

4 participants

@malukenho

Add "Age" rule in favor of "MinimumAge".

Age rule works pretty much the same way “minimumAge”
worked but it optionally accepts a maximum age as second
argument.

See #103.

Blockers for merge

  • Add age rule on README.
  • Add age rule on Validator docblocs.
  • Remove minimumAge rule from Validator docblocs.
  • Modify README to reflect removal of minimumAge (point deprecation in favor of age).
  • Clean up unrelated commits and merges.
  • Make age compatible with minimumAge.
@augustohp augustohp added this to the 0.6.0 milestone
@augustohp
Owner

Missing README.md changes.

@augustohp
Owner

Hmn, there is a merge commit on the pull request. Could you do a git fetch upstream && git rebase upstream/master on your branch and then a forced push to update the pull request and get rid of the unnecessary merge commit?

@augustohp
Owner

Ping

@augustohp augustohp modified the milestone: 0.7.0, 0.6.0
@malukenho malukenho closed this
@augustohp
Owner

Any reason why you closed this? I see some confusion on the commits, If I were you I would:

  1. Rebase this current branch against the 0.6.0 tag.
  2. Create a different branch for issue 190.
  3. Checkout the age branch to d05f7b4 which seems to have the right delta and push --force that to your remote branch.

PS: You may need to create another pull request for issue 190, since you are -apparently- using the same branch for both issues.

@augustohp augustohp reopened this
@malukenho

@augustohp what's missing here yet?

@henriquemoody

I'm sorry by our late.

@malukenho, can you please rebase with our current master an update documentation (README.md and Validator docblocks)?

library/Rules/Age.php
((20 lines not shown))
+ return false;
+ }
+
+ if ($input instanceof DateTime) {
+ $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;
+
+ } elseif (!is_string($input) || (is_null($this->format) && false === strtotime($input))) {
+ return false;
+ } else {
@henriquemoody Owner

Avoid to use else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/Rules/Age.php
((29 lines not shown))
+ }
+
+ 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);
+
+ if ($this->maxAge) {
+ return $age >= $this->minAge and $age <= $this->maxAge;
+
+ }
+ return $age >= $this->minAge;
+ }
+}
@henriquemoody Owner

Missing space here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/Rules/MinimumAge.php
@@ -1,36 +0,0 @@
-<?php
-namespace Respect\Validation\Rules;
-
-use DateTime;
-
-class MinimumAge extends AbstractRule
@henriquemoody Owner

Do the same of what was made in Vowels rule, them we can remove it in the next major release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@henriquemoody henriquemoody modified the milestone: 0.7.0
malukenho added some commits
@malukenho malukenho Add rule "Age" to validation.
Instead of adding a MaximumAge rule, this creates the Age rule which
incorporates both rules: minimum and maximum age. Maximum age is optional.
77c723c
@malukenho malukenho Move files to conform PSR-4.
Fixup this commit.
dc1d59e
@malukenho malukenho MinimumAge test using Age rule.
This needs a fix, the "format" parameters accepted by "minimumAge"
rule is not supported by the new validator. It was supposed to be
compatible and do the exactly same thing as "minimumAge".

It would be cool to keep the same API as the "minimumAge", example:

	<?php
		// As it was before
		v::minimumAge(18, 'Y-m-d')->validate('1990-01-01'); // true

		// Needs to work that way
		v::age(18, 'Y-m-d')->validate('1990-01-01'); // true
		v::age(18, 65, 'Y-m-d')->validate('1990-01-01'); //true
	?>

It could work that way as the `format` will always be something
acceptable by `strtotime()` function. And an `age` (an small integer)
can also be a string (Ex: "18") but is not a valid format. So, if the
second parameter is given, and is not an `is_numeric()`, it is a format.
2a9782a
@malukenho malukenho Fix EOL EOF
3e4e79e
@malukenho malukenho Translate tabs to space
9ee22b2
@henriquemoody henriquemoody commented on the diff
tests/library/Respect/Validation/Rules/Age.php
@@ -0,0 +1,69 @@
+<?php
+namespace Respect\Validation\Rules;
+
+use DateTime;
+
+class AgeTest extends \PHPUnit_Framework_TestCase
@henriquemoody Owner

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

My fail, that need to be removed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@henriquemoody

@malukenho, I'm sorry to make you change it again, but we decided to keep BC between minor versions, so just let maximumAge() as it is unless age() is totally compatible with maximumAge() rule, if that's the case you can just use inheritance on MaximumAge.

@henriquemoody henriquemoody added this to the 0.8 milestone
@henriquemoody

@malukenho, is there any progress on that issue?

@AndyWendt

I could look into this if you like and get it fixed up. Unless there is something else you would rather have me work on.

@henriquemoody

If @malukenho wants to pass this to another one I'm ok with that, let's just wait for his answer.

@henriquemoody

@AndyWendt, since @malukenho didn't answer our question and there are 21 days since the last interaction, I'd be glad if you finish it.

@henriquemoody henriquemoody modified the milestone: Backlog, 0.8
@henriquemoody henriquemoody referenced this pull request
Merged

Create "Age" rule #305

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 19, 2015
  1. @malukenho

    Add rule "Age" to validation.

    malukenho authored
    Instead of adding a MaximumAge rule, this creates the Age rule which
    incorporates both rules: minimum and maximum age. Maximum age is optional.
  2. @malukenho

    Move files to conform PSR-4.

    malukenho authored malukenho committed
    Fixup this commit.
  3. @malukenho

    MinimumAge test using Age rule.

    malukenho authored malukenho committed
    This needs a fix, the "format" parameters accepted by "minimumAge"
    rule is not supported by the new validator. It was supposed to be
    compatible and do the exactly same thing as "minimumAge".
    
    It would be cool to keep the same API as the "minimumAge", example:
    
    	<?php
    		// As it was before
    		v::minimumAge(18, 'Y-m-d')->validate('1990-01-01'); // true
    
    		// Needs to work that way
    		v::age(18, 'Y-m-d')->validate('1990-01-01'); // true
    		v::age(18, 65, 'Y-m-d')->validate('1990-01-01'); //true
    	?>
    
    It could work that way as the `format` will always be something
    acceptable by `strtotime()` function. And an `age` (an small integer)
    can also be a string (Ex: "18") but is not a valid format. So, if the
    second parameter is given, and is not an `is_numeric()`, it is a format.
  4. @malukenho

    Fix EOL EOF

    malukenho authored
  5. @malukenho

    Translate tabs to space

    malukenho authored
This page is out of date. Refresh to see the latest.
View
2  library/Exceptions/MinimumAgeException.php → library/Exceptions/AgeException.php
@@ -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(
View
51 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;
+ }
+}
View
33 library/Rules/MinimumAge.php
@@ -3,33 +3,14 @@
use DateTime;
-class MinimumAge extends AbstractRule
+/**
+ * @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);
}
}
View
69 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'),
+ );
+ }
+}
View
23 tests/Rules/MininumAgeTest.php
@@ -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', ''),
@@ -48,7 +49,7 @@ public function providerForValidDateValidMinimumAge()
);
}
- public function providerForValidDateInvalidMinimumAge()
+ public function providerForValidDateInvalidAge()
{
return array(
array(18, 'Y-m-d', '2002-06-30'),
View
69 tests/library/Respect/Validation/Rules/Age.php
@@ -0,0 +1,69 @@
+<?php
+namespace Respect\Validation\Rules;
+
+use DateTime;
+
+class AgeTest extends \PHPUnit_Framework_TestCase
@henriquemoody Owner

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

My fail, that need to be removed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+{
+ /**
+ * @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'),
+ );
+ }
+}
+
Something went wrong with that request. Please try again.