Skip to content

Commit 81ca951

Browse files
authored
Merge pull request #54 from DenislavParvanov/master
add integer rule
2 parents 55b46c7 + 4a546d2 commit 81ca951

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ Below is list of all available validation rules
287287
* [digits](#rule-digits)
288288
* [digits_between](#rule-digits_between)
289289
* [url](#rule-url)
290+
* [integer](#rule-integer)
290291
* [ip](#rule-ip)
291292
* [ipv4](#rule-ipv4)
292293
* [ipv6](#rule-ipv6)
@@ -513,6 +514,10 @@ $validation = $validator->validate($inputs, [
513514
> For common URL scheme and mailto, we combine `FILTER_VALIDATE_URL` to validate URL format and `preg_match` to validate it's scheme.
514515
Except for JDBC URL, currently it just check a valid JDBC scheme.
515516

517+
<a id="rule-integer"></a>
518+
#### integer
519+
The field under this rule must be integer.
520+
516521
<a id="rule-ip"></a>
517522
#### ip
518523

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)