File tree Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rakit \Validation \Rules ;
4+
5+ use Rakit \Validation \Rule ;
6+
7+ class Digits extends Rule
8+ {
9+
10+ protected $ message = "The :attribute must be numeric and must have an exact length of :length " ;
11+
12+ protected $ fillable_params = ['length ' ];
13+
14+ public function check ($ value )
15+ {
16+ $ this ->requireParameters ($ this ->fillable_params );
17+
18+ $ length = (int ) $ this ->parameter ('length ' );
19+
20+ return ! preg_match ('/[^0-9]/ ' , $ value )
21+ && strlen ((string ) $ value ) == $ length ;
22+ }
23+
24+ }
Original file line number Diff line number Diff line change @@ -107,6 +107,7 @@ protected function registerBaseValidators()
107107 'lowercase ' => new Rules \Lowercase ,
108108 'uppercase ' => new Rules \Uppercase ,
109109 'json ' => new Rules \Json ,
110+ 'digits ' => new Rules \Digits ,
110111 'defaults ' => new Rules \Defaults ,
111112 'default ' => new Rules \Defaults , // alias of defaults
112113 ];
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ use Rakit \Validation \Rules \Digits ;
4+
5+ class DigitsTest extends PHPUnit_Framework_TestCase
6+ {
7+
8+ public function setUp ()
9+ {
10+ $ this ->rule = new Digits ;
11+ }
12+
13+ public function testValids ()
14+ {
15+ $ this ->assertTrue ($ this ->rule ->fillParameters ([4 ])->check (1243 ));
16+ $ this ->assertTrue ($ this ->rule ->fillParameters ([6 ])->check (124567 ));
17+ $ this ->assertTrue ($ this ->rule ->fillParameters ([3 ])->check ('123 ' ));
18+ }
19+
20+ public function testInvalids ()
21+ {
22+ $ this ->assertFalse ($ this ->rule ->fillParameters ([7 ])->check (12345678 ));
23+ $ this ->assertFalse ($ this ->rule ->fillParameters ([4 ])->check (12 ));
24+ $ this ->assertFalse ($ this ->rule ->fillParameters ([3 ])->check ('foo ' ));
25+ }
26+
27+ }
You can’t perform that action at this time.
0 commit comments