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

FoneComDDD::isValid()   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
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