PhoneMask   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 33
c 1
b 0
f 0
wmc 9
lcom 0
cbo 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C isValid() 0 25 8
A getMessage() 0 4 1
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