Skip to content

Commit 666d1a3

Browse files
add integer rule
1 parent 55b46c7 commit 666d1a3

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/Rules/Integer.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rakit\Validation\Rules;
4+
5+
use Rakit\Validation\Rule;
6+
7+
class Integer extends Rule
8+
{
9+
10+
protected $message = "The :attribute must be integer";
11+
12+
public function check($value)
13+
{
14+
return filter_var($value, FILTER_VALIDATE_INT) !== false;
15+
}
16+
17+
}

src/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ protected function registerBaseValidators()
9090
'max' => new Rules\Max,
9191
'between' => new Rules\Between,
9292
'url' => new Rules\Url,
93+
'integer' => new Rules\Integer,
9394
'ip' => new Rules\Ip,
9495
'ipv4' => new Rules\Ipv4,
9596
'ipv6' => new Rules\Ipv6,

tests/Rules/IntegerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Rakit\Validation\Rules\Integer;
4+
5+
class IntegerTest extends PHPUnit_Framework_TestCase
6+
{
7+
8+
public function setUp()
9+
{
10+
$this->rule = new Integer;
11+
}
12+
13+
public function testValids()
14+
{
15+
$this->assertTrue($this->rule->check(0));
16+
$this->assertTrue($this->rule->check('0'));
17+
$this->assertTrue($this->rule->check('123'));
18+
$this->assertTrue($this->rule->check('-123'));
19+
$this->assertTrue($this->rule->check(123));
20+
$this->assertTrue($this->rule->check(-123));
21+
22+
}
23+
24+
public function testInvalids()
25+
{
26+
$this->assertFalse($this->rule->check('foo123'));
27+
$this->assertFalse($this->rule->check('123foo'));
28+
$this->assertFalse($this->rule->check([123]));
29+
$this->assertFalse($this->rule->check('123.456'));
30+
$this->assertFalse($this->rule->check('-123.456'));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)