Passed
Push — main ( f7f95e...c4cb1e )
by Breno
02:19
created

RuleChainTrait::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation;
5
6
use RuntimeException;
7
8
/**
9
 * @internal
10
 * @method self allowsNull()
11
 * @method self allowsEmpty()
12
 * @method self allowsEmptyString()
13
 * @method self required(?string $message = null)
14
 * @method self notEmpty(?string $message = null)
15
 * @method self notEmptyString(?string $message = null)
16
 * @method self notNull(?string $message = null)
17
 * @method self email(?string $message = null)
18
 */
19
trait RuleChainTrait
20
{
21
    abstract public function add(Rule|RuleSet ...$rules): static;
22
23
    public function __call($name, $arguments): static
24
    {
25
        $namespace = __NAMESPACE__ . '\\Rules';
26
        $class = sprintf("%s\%s", $namespace, ucfirst($name));
27
        if (!class_exists($class)) {
28
            throw new RuntimeException(sprintf('Rule not found: (%s).', $name));
29
        }
30
31
        $rule = new $class(...$arguments);
32
        if ($rule instanceof Rule) {
33
            return $this->add($rule);
34
        }
35
36
        return $this;
37
    }
38
}
39