Passed
Push — main ( 7c50b2...52de70 )
by Breno
01:51
created

RuleChain::__call()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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