This repository was archived by the owner on Sep 13, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 8 files changed +179
-2
lines changed
Expand file tree Collapse file tree 8 files changed +179
-2
lines changed Original file line number Diff line number Diff line change 1313 "require" : {
1414 "php" : " >=7.0" ,
1515 "illuminate/database" : " ^5.4" ,
16- "illuminate/support" : " ^5.4"
16+ "illuminate/support" : " ^5.4" ,
17+ "saritasa/php-common" : " ^1.0"
1718 },
1819 "require-dev" : {
1920 "phpunit/phpunit" : " ^6.0"
2021 },
2122 "minimum-stability" : " stable" ,
2223 "autoload" : {
2324 "psr-4" : {
24- "Saritasa\\ Laravel\\ Validation" : " src/"
25+ "Saritasa\\ Laravel\\ Validation\\ " : " src/"
2526 }
2627 },
28+ "autoload-dev" : {
29+ "psr-4" : { "Saritasa\\ Laravel\\ Validation\\ Tests\\ " : " tests/" }
30+ },
2731 "config" : {
2832 "preferred-install" : " dist" ,
2933 "sort-packages" : true
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Saritasa \Laravel \Validation ;
4+
5+ class GenericRuleSet extends RuleSet
6+ {
7+ function int (): IntRuleSet
8+ {
9+ return new IntRuleSet ($ this ->rules );
10+ }
11+
12+ function string (): StringRuleSet
13+ {
14+ return new StringRuleSet ($ this ->rules );
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Saritasa \Laravel \Validation ;
4+
5+ interface IRule
6+ {
7+ function toString (): string ;
8+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Saritasa \Laravel \Validation ;
4+
5+ class IntRuleSet extends RuleSet
6+ {
7+ public function __construct (array $ rules = [])
8+ {
9+ parent ::__construct (self ::mergeIfNotExists ('int ' , $ rules ));
10+ }
11+
12+ public function min (int $ minimalValue ): IntRuleSet
13+ {
14+ return $ this ->appendIfNotExists ("min: $ minimalValue " );
15+ }
16+
17+ public function max (int $ maximalValue ): IntRuleSet
18+ {
19+ return $ this ->appendIfNotExists ("max: $ maximalValue " );
20+ }
21+ }
Original file line number Diff line number Diff line change 22
33namespace Saritasa \Laravel \Validation ;
44
5+ /**
6+ * Root builder for validation rules
7+ */
58class Rule
69{
10+ static function required (): GenericRuleSet
11+ {
12+ return (new GenericRuleSet ())->required ();
13+ }
14+
15+ static function int (): IntRuleSet
16+ {
17+ return new IntRuleSet ();
18+ }
19+
20+ static function string (): StringRuleSet
21+ {
22+ return new StringRuleSet ();
23+ }
724}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Saritasa \Laravel \Validation ;
4+
5+ class RuleSet
6+ {
7+ /** @var array */
8+ protected $ rules ;
9+
10+ public function __construct (array $ rules = [])
11+ {
12+ $ this ->rules = $ rules ;
13+ }
14+
15+ /**
16+ * @return static
17+ */
18+ public function required ()
19+ {
20+ return $ this ->appendIfNotExists ('required ' );
21+ }
22+
23+ protected function appendIfNotExists (string $ rule )
24+ {
25+ if (in_array ($ rule , $ this ->rules )) {
26+ return $ this ;
27+ }
28+ else {
29+ return new static (array_merge ($ this ->rules , [$ rule ]));
30+ }
31+ }
32+
33+ protected static function mergeIfNotExists (string $ rule , array $ rules = []): array
34+ {
35+ if (in_array ($ rule , $ rules )) {
36+ return $ rules ;
37+ }
38+ else {
39+ return array_merge ($ rules , [$ rule ]);
40+ }
41+ }
42+
43+ public function toArray ()
44+ {
45+ return array_map (function ($ rule ) {
46+ if ($ rule instanceof IRule) {
47+ return $ rule ->toString ();
48+ } else {
49+ return $ rule ;
50+ }
51+ }, $ this ->rules );
52+ }
53+
54+ public function toString ()
55+ {
56+ return implode ('| ' , $ this ->toArray ());
57+ }
58+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Saritasa \Laravel \Validation ;
4+
5+ class StringRuleSet extends RuleSet
6+ {
7+ public function __construct (array $ rules = [])
8+ {
9+ parent ::__construct (self ::mergeIfNotExists ('string ' , $ rules ));
10+ }
11+
12+ public function email (): StringRuleSet
13+ {
14+ return $ this ->appendIfNotExists ('email ' );
15+ }
16+
17+ public function regex (string $ pattern ){
18+ return $ this ->appendIfNotExists ("regex: $ pattern " );
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Saritasa \Laravel \Validation \Tests ;
4+
5+ use PHPUnit \Framework \TestCase ;
6+ use Saritasa \Laravel \Validation \Rule ;
7+
8+ class RuleTest extends TestCase
9+ {
10+ public function testRequired ()
11+ {
12+ $ rules = Rule::required ();
13+ $ this ->assertEquals ('required ' , $ rules ->toString ());
14+ }
15+
16+ public function testString ()
17+ {
18+ $ rules = Rule::string ();
19+ $ this ->assertEquals ('string ' , $ rules ->toString ());
20+ }
21+
22+ public function testInt ()
23+ {
24+ $ rules = Rule::int ();
25+ $ this ->assertEquals ('int ' , $ rules ->toString ());
26+ }
27+
28+ public function testFluidString ()
29+ {
30+ $ rules = Rule::required ()->string ()->email ();
31+ $ this ->assertEquals ('required|string|email ' , $ rules ->toString ());
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments