Passed
Push — main ( 6afe0c...47aea4 )
by Breno
01:54
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 BrenoRoosevelt\Validation\Contracts\Rule;
7
use RuntimeException;
8
9
/**
10
 * @internal
11
 * @method self allowNull()
12
 * @method self allowEmpty()
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 RuleChain
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