Passed
Push — main ( 9fd9c1...757bec )
by Breno
01:56
created

FoneComDDD   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isValid() 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
use Throwable;
9
10
#[Attribute(Attribute::TARGET_PROPERTY)]
11
class FoneComDDD extends AbstractRule
12
{
13
    const MASK = '/^\([0-9]{2}\)[0-9]{4,5}-[0-9]{4}$/';
14
    const UNMASK = '/^\[0-9]{10,11}$/';
15
16
    public function __construct(private bool $mask = true, ?string $message = 'Telefone inválido')
17
    {
18
        parent::__construct($message);
19
    }
20
21
    public function isValid($input, array $context = []): bool
22
    {
23
        if (!is_string($input) || !is_numeric($input)) {
24
            return false;
25
        }
26
27
        $pattern = $this->mask ? self::MASK : self::UNMASK;
28
        return preg_match($pattern, (string) $input) === 1;
29
    }
30
}
31