Passed
Push — main ( 0f17ea...1f5a74 )
by Breno
01:47
created

Compare::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 5
dl 0
loc 8
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Rules\Comparison;
5
6
use Attribute;
7
use BrenoRoosevelt\Validation\AbstractRule;
8
use BrenoRoosevelt\Validation\Comparator;
9
use BrenoRoosevelt\Validation\Translation\Translator;
10
11
#[Attribute(Attribute::TARGET_PROPERTY)]
12
class Compare extends AbstractRule
13
{
14
    const MESSAGE = 'Value should be %s `%s`';
15
16
    use Comparator;
17
18
    public function __construct(
19
        protected string $operator,
20
        protected mixed $value,
21
        ?string $message = null,
22
        ?int $stopOnFailure = null,
23
        ?int $priority = null
24
    ) {
25
        parent::__construct($message, $stopOnFailure, $priority);
26
    }
27
28
    public function isValid($input, array $context = []): bool
29
    {
30
        return $this->compare($input, $this->operator, $this->value);
31
    }
32
33
    public function translatedMessage(): ?string
34
    {
35
        $translatedOperator = Translator::translate($this->operator);
36
        return Translator::translate(self::MESSAGE, $translatedOperator, $this->value);
37
    }
38
}
39