File tree Expand file tree Collapse file tree 5 files changed +84
-11
lines changed
Expand file tree Collapse file tree 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
88
99 public function check ($ value )
1010 {
11- return ! empty ($ value );
11+ return empty ($ value );
1212 }
1313
1414 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()
1616 'numeric ' => new Rule \NumericRule (),
1717 'string ' => new Rule \StringRule (),
1818 'email ' => new Rule \EmailRule (),
19- 'password ' => new Rule \PasswordRule ()
19+ 'password ' => new Rule \PasswordRule (),
20+ 'required ' => new Rule \RequiredRule (),
2021 ];
2122 }
2223
2324 public function validate ($ rules , $ params )
2425 {
2526 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-
3227 $ ruleArray = explode ('| ' , $ ruleString );
33-
3428 foreach ($ ruleArray as $ rule ) {
35-
3629 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 )) {
3832 $ this ->errors [$ ruleField ][] = $ this ->validators [$ rule ]->getMessage ();
3933 }
4034 } 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