Skip to content

Commit

Permalink
Merge pull request #54 from DenislavParvanov/master
Browse files Browse the repository at this point in the history
add integer rule
  • Loading branch information
emsifa authored Sep 18, 2018
2 parents 55b46c7 + 4a546d2 commit 81ca951
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ Below is list of all available validation rules
* [digits](#rule-digits)
* [digits_between](#rule-digits_between)
* [url](#rule-url)
* [integer](#rule-integer)
* [ip](#rule-ip)
* [ipv4](#rule-ipv4)
* [ipv6](#rule-ipv6)
Expand Down Expand Up @@ -513,6 +514,10 @@ $validation = $validator->validate($inputs, [
> For common URL scheme and mailto, we combine `FILTER_VALIDATE_URL` to validate URL format and `preg_match` to validate it's scheme.
Except for JDBC URL, currently it just check a valid JDBC scheme.

<a id="rule-integer"></a>
#### integer
The field under this rule must be integer.

<a id="rule-ip"></a>
#### ip

Expand Down
17 changes: 17 additions & 0 deletions src/Rules/Integer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rakit\Validation\Rules;

use Rakit\Validation\Rule;

class Integer extends Rule
{

protected $message = "The :attribute must be integer";

public function check($value)
{
return filter_var($value, FILTER_VALIDATE_INT) !== false;
}

}
1 change: 1 addition & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ protected function registerBaseValidators()
'max' => new Rules\Max,
'between' => new Rules\Between,
'url' => new Rules\Url,
'integer' => new Rules\Integer,
'ip' => new Rules\Ip,
'ipv4' => new Rules\Ipv4,
'ipv6' => new Rules\Ipv6,
Expand Down
33 changes: 33 additions & 0 deletions tests/Rules/IntegerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Rakit\Validation\Rules\Integer;

class IntegerTest extends PHPUnit_Framework_TestCase
{

public function setUp()
{
$this->rule = new Integer;
}

public function testValids()
{
$this->assertTrue($this->rule->check(0));
$this->assertTrue($this->rule->check('0'));
$this->assertTrue($this->rule->check('123'));
$this->assertTrue($this->rule->check('-123'));
$this->assertTrue($this->rule->check(123));
$this->assertTrue($this->rule->check(-123));

}

public function testInvalids()
{
$this->assertFalse($this->rule->check('foo123'));
$this->assertFalse($this->rule->check('123foo'));
$this->assertFalse($this->rule->check([123]));
$this->assertFalse($this->rule->check('123.456'));
$this->assertFalse($this->rule->check('-123.456'));
}

}

0 comments on commit 81ca951

Please sign in to comment.