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