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

FoneComDDD::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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