PhoneMask::isValid()   C
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 5.3846
cc 8
eloc 13
nc 6
nop 0
1
<?php
2
3
/**
4
 * @author: Abdul Qureshi. <[email protected]>
5
 * 
6
 * This file has been modified from the original source.
7
 * See original here:
8
 *
9
 * @link: https://github.com/progsmile/request-validator
10
 */
11
12
namespace TheSupportGroup\Common\Validator\Rules;
13
14
use TheSupportGroup\Common\Validator\Contracts\Rules\RuleInterface;
15
16
class PhoneMask extends BaseRule implements RuleInterface
17
{
18
    public function isValid()
19
    {
20
        if ($this->isNotRequiredAndEmpty()) {
21
            return true;
22
        }
23
24
        $phone = trim($this->getParams()[1]);
25
        $phoneMask = substr($this->getParams()[2], 1, -1);
26
27
        if (strlen($phone) != strlen($phoneMask)) {
28
            return false;
29
        }
30
31
        foreach (str_split($phoneMask) as $index => $maskChar) {
32
            if ($maskChar != '#' && $maskChar != $phone[$index]) {
33
                return false;
34
            }
35
36
            if ($maskChar == '#' && !is_numeric($phone[$index])) {
37
                return false;
38
            }
39
        }
40
41
        return true;
42
    }
43
44
    public function getMessage()
45
    {
46
        return 'Field :field: has bad phone format';
47
    }
48
}
49