File tree 5 files changed +84
-11
lines changed
5 files changed +84
-11
lines changed Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ class EmptyRule extends AbstractRule
8
8
9
9
public function check ($ value )
10
10
{
11
- return ! empty ($ value );
11
+ return empty ($ value );
12
12
}
13
13
14
14
public function getMessage ()
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Atakde \PhpValidation \Rule ;
4
+
5
+ class RequiredRule extends AbstractRule
6
+ {
7
+ private $ ruleName = 'required ' ;
8
+
9
+ public function check ($ value )
10
+ {
11
+ return isset ($ value );
12
+ }
13
+
14
+ public function getMessage ()
15
+ {
16
+ return 'The value is required ' ;
17
+ }
18
+
19
+ public function getRuleName ()
20
+ {
21
+ return $ this ->ruleName ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change @@ -16,25 +16,19 @@ public function __construct()
16
16
'numeric ' => new Rule \NumericRule (),
17
17
'string ' => new Rule \StringRule (),
18
18
'email ' => new Rule \EmailRule (),
19
- 'password ' => new Rule \PasswordRule ()
19
+ 'password ' => new Rule \PasswordRule (),
20
+ 'required ' => new Rule \RequiredRule (),
20
21
];
21
22
}
22
23
23
24
public function validate ($ rules , $ params )
24
25
{
25
26
foreach ($ rules as $ ruleField => $ ruleString ) {
26
-
27
- if (!isset ($ params [$ ruleField ])) {
28
- $ this ->errors [$ ruleField ][] = 'The field ' . $ ruleField . ' is not exists. ' ;
29
- continue ;
30
- }
31
-
32
27
$ ruleArray = explode ('| ' , $ ruleString );
33
-
34
28
foreach ($ ruleArray as $ rule ) {
35
-
36
29
if (isset ($ this ->validators [$ rule ])) {
37
- if (!$ this ->validators [$ rule ]->check ($ params [$ ruleField ])) {
30
+ $ checkValue = isset ($ params [$ ruleField ]) ? $ params [$ ruleField ] : null ; // test fail if not doing this for now.
31
+ if (!$ this ->validators [$ rule ]->check ($ checkValue )) {
38
32
$ this ->errors [$ ruleField ][] = $ this ->validators [$ rule ]->getMessage ();
39
33
}
40
34
} else {
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ require 'vendor/autoload.php ' ;
4
+
5
+ use Atakde \PhpValidation \Validator ;
6
+ use PHPUnit \Framework \TestCase ;
7
+
8
+ final class RequiredRuleTest extends TestCase
9
+ {
10
+ protected $ validator ;
11
+
12
+ protected function setUp ()
13
+ {
14
+ $ this ->validator = new Validator ;
15
+ }
16
+
17
+ /**
18
+ * Check required rule is working or not
19
+ */
20
+ public function testEmptyRule ()
21
+ {
22
+ $ this ->validator ->validate (['myInput ' => 'required ' ], ['myInput ' => '' ]); // giving an the input value in array
23
+ $ this ->assertTrue ($ this ->validator ->passes ());
24
+
25
+ $ this ->validator ->validate (['myInput ' => 'required ' ], []); // sending an empty array
26
+ $ this ->assertTrue ($ this ->validator ->fails ());
27
+ }
28
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ require 'vendor/autoload.php ' ;
4
+
5
+ use Atakde \PhpValidation \Validator ;
6
+ use PHPUnit \Framework \TestCase ;
7
+
8
+ final class StringRuleTest extends TestCase
9
+ {
10
+ protected $ validator ;
11
+
12
+ protected function setUp ()
13
+ {
14
+ $ this ->validator = new Validator ;
15
+ }
16
+
17
+ /**
18
+ * Check string rule is working or not
19
+ */
20
+ public function testEmptyRule ()
21
+ {
22
+ $ this ->validator ->validate (['myInput ' => 'string ' ], ['myInput ' => 'my string ' ]); // giving a string value
23
+ $ this ->assertTrue ($ this ->validator ->passes ());
24
+
25
+ $ this ->validator ->validate (['myInput ' => 'string ' ], ['myInput ' => 123 ]); // giving a number value
26
+ $ this ->assertTrue ($ this ->validator ->fails ());
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments