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

RuleChainTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 18
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A __call() 0 14 3
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