AbstractValidation::validate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 4
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Rules;
5
6
use BrenoRoosevelt\Validation\MaybeBelongsToField;
7
use BrenoRoosevelt\Validation\GuardForValidation;
8
use BrenoRoosevelt\Validation\Result;
9
use BrenoRoosevelt\Validation\Validation;
10
11
abstract class AbstractValidation implements Validation
12
{
13
    use GuardForValidation,
14
        MaybeBelongsToField;
15
16
    protected string $message;
17
18
    public function __construct(?string $message = null)
19
    {
20
        $this->message = $message ?? sprintf('Constraint violation: %s', get_class($this));
21
    }
22
23
    public function validate($input, array $context = []): Result
24
    {
25
        $result = $this->newEmptyValidationResult();
26
        return $this->isValid($input, $context) ? $result: $result->error($this->message);
27
    }
28
29
    abstract protected function isValid($input, array $context = []): bool;
30
}
31