Skip to content

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 Feb 20, 2014
@augustohp
Respect member

Missing README.md changes.

@augustohp
Respect member

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
Respect member

Ping

@augustohp augustohp modified the milestone: 0.7.0, 0.6.0 Jun 26, 2014
@malukenho malukenho closed this Jun 27, 2014
@augustohp
Respect member

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 Jul 11, 2014
@malukenho

@augustohp what's missing here yet?

@henriquemoody
Respect member

I'm sorry by our late.

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

@henriquemoody henriquemoody commented on an outdated diff Jan 4, 2015
library/Rules/Age.php
+ 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
Respect member
henriquemoody added a note Jan 4, 2015

Avoid to use else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@henriquemoody henriquemoody commented on an outdated diff Jan 4, 2015
library/Rules/Age.php
+ }
+
+ 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
Respect member
henriquemoody added a note Jan 4, 2015

Missing space here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@henriquemoody henriquemoody commented on an outdated diff Jan 4, 2015
library/Rules/MinimumAge.php
@@ -1,36 +0,0 @@
-<?php
-namespace Respect\Validation\Rules;
-
-use DateTime;
-
-class MinimumAge extends AbstractRule
@henriquemoody
Respect member
henriquemoody added a note Jan 4, 2015

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 Jan 7, 2015
malukenho added some commits Feb 17, 2014
@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 Jan 19, 2015
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
Respect member

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

@malukenho
malukenho added a note Jan 19, 2015

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
Respect member

@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 Jan 20, 2015
@henriquemoody
Respect member

@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
Respect member

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

@henriquemoody
Respect member

@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 Feb 11, 2015
@henriquemoody henriquemoody referenced this pull request Feb 19, 2015
Merged

Create "Age" rule #305

@henriquemoody henriquemoody removed this from the Backlog milestone Sep 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.