Passed
Push — main ( bcfe1e...2c9533 )
by Breno
01:52
created

Cep::evaluate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 3
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\Rules\Brazilian;
5
6
use Attribute;
7
use BrenoRoosevelt\Validation\AbstractRule;
8
9
#[Attribute(Attribute::TARGET_PROPERTY)]
10
class Cep extends AbstractRule
11
{
12
    const MASK = '/^[0-9]{5}\-[0-9]{3}$/';
13
    const UNMASK = '/^[0-9]{8}$/';
14
15
    public function __construct(private bool $mask = true, ?string $message = 'CEP inválido')
16
    {
17
        parent::__construct( $message);
18
    }
19
20
    protected function evaluate(mixed $input, array $context = []): bool
21
    {
22
        if (!is_string($input) || !is_numeric($input)) {
23
            return false;
24
        }
25
26
        $pattern = $this->mask ? Cep::MASK : Cep::UNMASK;
27
        return preg_match($pattern, (string) $input) === 1;
28
    }
29
}
30