diff --git a/src/EBT/Validator/ValidatorExtended.php b/src/EBT/Validator/ValidatorExtended.php index 3e5a75d..99b25cf 100644 --- a/src/EBT/Validator/ValidatorExtended.php +++ b/src/EBT/Validator/ValidatorExtended.php @@ -13,6 +13,7 @@ use Symfony\Component\Validator\Constraints\NotBlank as NotBlankConstraint; use Symfony\Component\Validator\Constraints\Type as TypeConstraint; +use Symfony\Component\Validator\Constraints\Range as RangeConstraint; /** * ValidatorExtended @@ -37,4 +38,23 @@ public static function isStringAndNotBlank($value) return static::violationsToBool($violations); } + + /** + * @param mixed $value + * + * @return bool + */ + public static function isPositiveInteger($value) + { + $violations = static::getValidator()->validateValue( + $value, + array( + new TypeConstraint(array('type' => 'integer')), + new RangeConstraint(array('min' => 1)) + ) + ); + static::setViolations($violations); + + return static::violationsToBool($violations); + } } diff --git a/tests/EBT/Validator/Tests/ValidatorExtendedTest.php b/tests/EBT/Validator/Tests/ValidatorExtendedTest.php index a01398f..5c44583 100644 --- a/tests/EBT/Validator/Tests/ValidatorExtendedTest.php +++ b/tests/EBT/Validator/Tests/ValidatorExtendedTest.php @@ -25,4 +25,14 @@ public function testIsStringAndNotBlank() $this->assertFalse(ValidatorExtended::isStringAndNotBlank('')); $this->assertTrue(ValidatorExtended::isStringAndNotBlank('test')); } + + public function testIsPositiveInteger() + { + // this fails fow now (so is commented) + //$this->assertFalse(ValidatorExtended::isPositiveInteger(array())); + $this->assertFalse(ValidatorExtended::isPositiveInteger('test')); + $this->assertFalse(ValidatorExtended::isPositiveInteger(-5)); + $this->assertTrue(ValidatorExtended::isPositiveInteger(1)); + $this->assertTrue(ValidatorExtended::isPositiveInteger(10)); + } }