Skip to content

Commit cca7339

Browse files
KennedyTedescohenriquemoody
authored andcommitted
Throw an exception when age is not an integer
1 parent 9460a4c commit cca7339

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

library/Rules/MinimumAge.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Respect\Validation\Rules;
1313

1414
use DateTime;
15+
use Respect\Validation\Exceptions\ComponentException;
1516

1617
class MinimumAge extends AbstractRule
1718
{
@@ -20,16 +21,16 @@ class MinimumAge extends AbstractRule
2021

2122
public function __construct($age, $format = null)
2223
{
24+
if (!filter_var($age, FILTER_VALIDATE_INT)) {
25+
throw new ComponentException('The age must be a integer value.');
26+
}
27+
2328
$this->age = $age;
2429
$this->format = $format;
2530
}
2631

2732
public function validate($input)
2833
{
29-
if (!filter_var($this->age, FILTER_VALIDATE_INT)) {
30-
return false;
31-
}
32-
3334
if ($input instanceof DateTime) {
3435
$birthday = new \DateTime('now - '.$this->age.' year');
3536

tests/unit/Rules/MininumAgeTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public function testInvalidDateShouldNotPass($age, $format, $input)
5151
$this->assertFalse($minimumAge->assert($input));
5252
}
5353

54+
/**
55+
* @expectedException Respect\Validation\Exceptions\ComponentException
56+
* @expectedExceptionMessage The age must be a integer value
57+
*/
58+
public function testShouldNotAcceptNonIntegerAgeOnConstructor()
59+
{
60+
new MinimumAge('L12');
61+
}
62+
5463
public function providerForValidDateValidMinimumAge()
5564
{
5665
return [

0 commit comments

Comments
 (0)