Passed
Push — main ( aa2ba1...352c83 )
by Breno
01:51
created

AbstractRule::isValid()   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 2
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
/**
7
 * helper class to create single message validations
8
 */
9
abstract class AbstractRule implements Rule
10
{
11
    use GuardForValidation,
12
        MaybeBelongsToField;
13
14
    protected string $message;
15
16
    public function __construct(?string $message = null)
17
    {
18
        $this->message = $message ?? sprintf('Constraint violation: %s', get_class($this));
19
    }
20
21
    /**
22
     * @inheritDoc
23
     */
24
    public function validate(mixed $input, array $context = []): Result
25
    {
26
        $result = $this->newEmptyValidationResult();
27
        return $this->evaluate($input, $context) ? $result : $result->error($this->message);
28
    }
29
30
    /**
31
     * @param mixed $input
32
     * @param array $context
33
     * @return bool
34
     */
35
    public function isValid(mixed $input, array $context = []): bool
36
    {
37
        return $this->evaluate($input, $context);
38
    }
39
40
    /**
41
     * @param mixed $input
42
     * @param array $context
43
     * @return bool
44
     */
45
    abstract protected function evaluate(mixed $input, array $context = []): bool;
46
}
47