Skip to content

Commit

Permalink
Add at validator extended is positive integer
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardosoliv committed Jan 15, 2014
1 parent 743834e commit a2f7bcf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/EBT/Validator/ValidatorExtended.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
10 changes: 10 additions & 0 deletions tests/EBT/Validator/Tests/ValidatorExtendedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit a2f7bcf

Please sign in to comment.