Passed
Push — main ( 93c199...f7f95e )
by Breno
01:50
created

AbstractRule::classShortName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation;
5
6
use BrenoRoosevelt\Validation\Exception\ValidateOrFail;
7
8
abstract class AbstractRule implements Rule
9
{
10
    use ValidateOrFail,
11
        BelongsToField;
12
13
    protected string $message;
14
15
    public function __construct(?string $message = null)
16
    {
17
        $this->message = $message ?? sprintf('Constraint violation: %s', $this->classShortName());
18
    }
19
20
    /**
21
     * @inheritDoc
22
     */
23
    public function validate(mixed $input, array $context = []): Result
24
    {
25
        $result = $this->newEmptyResult();
26
        return $this->isValid($input, $context) ? $result : $result->addError($this->message);
27
    }
28
29
    private function classShortName(): string
30
    {
31
        return array_reverse(explode('\\', get_class($this)))[0];
32
    }
33
34
    abstract public function isValid(mixed $input, array $context = []): bool;
35
}
36