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

Cep   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 19
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A evaluate() 0 8 4
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