Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 567c23d

Browse files
committed
feat: allow multiple Factory namespaces
1 parent 3881efb commit 567c23d

File tree

3 files changed

+90
-7
lines changed

3 files changed

+90
-7
lines changed

src/Factory/Factory.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,35 @@
77

88
class 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
}

src/Test/Dummy/DummyRule.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}

tests/FactoryTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

0 commit comments

Comments
 (0)