File tree Expand file tree Collapse file tree 3 files changed +9
-38
lines changed Expand file tree Collapse file tree 3 files changed +9
-38
lines changed Original file line number Diff line number Diff line change 3
3
namespace Morebec \Validator \Rule ;
4
4
5
5
6
+ use InvalidArgumentException ;
6
7
use Morebec \Validator \ValidationRuleInterface ;
7
8
8
9
class Length implements ValidationRuleInterface
@@ -26,6 +27,8 @@ public function __construct(
26
27
?string $ message = null
27
28
)
28
29
{
30
+ if ($ length <0 )
31
+ throw new InvalidArgumentException ();
29
32
$ this ->length = $ length ;
30
33
$ this ->message = $ message ;
31
34
}
@@ -37,7 +40,7 @@ public function __construct(
37
40
*/
38
41
public function validate ($ v ): bool
39
42
{
40
- return strlen ($ v ) == $ this ->length ;
43
+ return strlen ($ v ) === $ this ->length ;
41
44
}
42
45
43
46
/**
@@ -47,6 +50,6 @@ public function validate($v): bool
47
50
*/
48
51
public function getMessage ($ v ): string
49
52
{
50
- return $ this ->message ?:"' $ {$ v }' is supposed to be exactly " .$ this ->length ." characters long " ;
53
+ return $ this ->message ?:"' $ {$ v }' is expected to be exactly " .$ this ->length ." characters long " ;
51
54
}
52
55
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 3
3
namespace Tests \Morebec \Validator \Rule ;
4
4
5
5
6
+ use InvalidArgumentException ;
6
7
use Morebec \Validator \Rule \Length ;
7
8
use PHPUnit \Framework \TestCase ;
8
9
@@ -15,5 +16,8 @@ public function testValidate(){
15
16
$ this ->assertTrue ($ ruleFirst ->validate ("test " ));
16
17
$ this ->assertFalse ($ ruleFirst ->validate ("test message " ));
17
18
$ this ->assertEquals ("Custom message " ,$ ruleSecond ->getMessage ("test " ));
19
+
20
+ $ this ->expectException (InvalidArgumentException::class);
21
+ $ ruleThird = new Length (-1 );
18
22
}
19
23
}
You can’t perform that action at this time.
0 commit comments