Passed
Push — main ( 954523...045e26 )
by Breno
01:54
created

Choice   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 22
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 3 1
A __construct() 0 7 1
A translatedMessage() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Rules;
5
6
use Attribute;
7
use BrenoRoosevelt\Validation\AbstractRule;
8
use BrenoRoosevelt\Validation\StopSign;
9
use BrenoRoosevelt\Validation\Translation\Translator;
10
11
#[Attribute(Attribute::TARGET_PROPERTY)]
12
class Choice extends AbstractRule
13
{
14
    const MESSAGE = 'Invalid choice, accepted values: %s';
15
16
    public function __construct(
17
        private array $list,
18
        private bool $strict = true,
19
        ?string $message = null,
20
        int $stopOnFailure = StopSign::DONT_STOP
21
    ) {
22
        parent::__construct($message, $stopOnFailure);
23
    }
24
25
    public function isValid($input, array $context = []): bool
26
    {
27
        return in_array($input, $this->list, $this->strict);
28
    }
29
30
    public function translatedMessage(): ?string
31
    {
32
        return Translator::translate(self::MESSAGE, trim(implode(', ', $this->list)));
33
    }
34
}
35