This repository was archived by the owner on Mar 13, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +90
-7
lines changed
Expand file tree Collapse file tree 3 files changed +90
-7
lines changed Original file line number Diff line number Diff line change 77
88class Factory
99{
10- private string $ namespace = 'ProgrammatorDev \\YetAnotherPhpValidator \\Rule ' ;
10+ private array $ namespaces = [ 'ProgrammatorDev \\YetAnotherPhpValidator \\Rule ' ] ;
1111
1212 /**
1313 * @throws RuleNotFoundException
1414 */
1515 public function createRule (string $ ruleName , array $ arguments = []): RuleInterface
1616 {
17- $ className = \sprintf ('%s \\%s ' , $ this ->namespace , \ucfirst ($ ruleName ));
17+ foreach ($ this ->namespaces as $ namespace ) {
18+ $ className = \sprintf ('%s \\%s ' , $ namespace , \ucfirst ($ ruleName ));
1819
19- if (!class_exists ($ className )) {
20- throw new RuleNotFoundException (
21- \sprintf ('"%s" rule does not exist. ' , $ ruleName )
22- );
20+ if (class_exists ($ className )) {
21+ return new $ className (...$ arguments );
22+ }
2323 }
2424
25- return new $ className (...$ arguments );
25+ throw new RuleNotFoundException (
26+ \sprintf ('"%s" rule does not exist. ' , $ ruleName )
27+ );
28+ }
29+
30+ public function getNamespaces (): array
31+ {
32+ return $ this ->namespaces ;
33+ }
34+
35+ public function addNamespace (string $ namespace ): self
36+ {
37+ $ this ->namespaces [] = $ namespace ;
38+
39+ return $ this ;
2640 }
2741}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace ProgrammatorDev \YetAnotherPhpValidator \Test \Dummy ;
4+
5+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \ValidationException ;
6+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \RuleInterface ;
7+
8+ class DummyRule implements RuleInterface
9+ {
10+ public function assert (mixed $ value , string $ name ): void
11+ {
12+ if (!$ value ) {
13+ throw new ValidationException (
14+ message: 'Dummy exception. '
15+ );
16+ }
17+ }
18+
19+ public function validate (mixed $ value ): bool
20+ {
21+ return (bool ) $ value ;
22+ }
23+
24+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace ProgrammatorDev \YetAnotherPhpValidator \Test ;
4+
5+ use ProgrammatorDev \YetAnotherPhpValidator \Exception \RuleNotFoundException ;
6+ use ProgrammatorDev \YetAnotherPhpValidator \Factory \Factory ;
7+ use ProgrammatorDev \YetAnotherPhpValidator \Rule \RuleInterface ;
8+
9+ class FactoryTest extends AbstractTest
10+ {
11+ private Factory $ factory ;
12+
13+ protected function setUp (): void
14+ {
15+ parent ::setUp ();
16+
17+ $ this ->factory = new Factory ();
18+ }
19+
20+ public function testFactoryCreateRuleFailure ()
21+ {
22+ $ this ->expectException (RuleNotFoundException::class);
23+ $ this ->expectExceptionMessage ('"invalidRule" rule does not exist. ' );
24+ $ this ->factory ->createRule ('invalidRule ' );
25+ }
26+
27+ public function testFactoryCreateRuleSuccess ()
28+ {
29+ $ this ->assertInstanceOf (RuleInterface::class, $ this ->factory ->createRule ('notBlank ' ));
30+ }
31+
32+ public function testFactoryGetNamespaces ()
33+ {
34+ $ this ->assertCount (1 , $ this ->factory ->getNamespaces ());
35+ $ this ->assertSame ('ProgrammatorDev \\YetAnotherPhpValidator \\Rule ' , $ this ->factory ->getNamespaces ()[0 ]);
36+ }
37+
38+ public function testFactoryAddNamespace ()
39+ {
40+ $ this ->factory ->addNamespace ('ProgrammatorDev \\YetAnotherPhpValidator \\Test \\Dummy ' );
41+
42+ $ this ->assertCount (2 , $ this ->factory ->getNamespaces ());
43+ $ this ->assertSame ('ProgrammatorDev \\YetAnotherPhpValidator \\Test \\Dummy ' , $ this ->factory ->getNamespaces ()[1 ]);
44+ }
45+ }
You can’t perform that action at this time.
0 commit comments