Completed
Push — master ( 238484...b2edf5 )
by Tyler
126:56 queued 61:56
created

Phone   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A passes() 0 4 1
A message() 0 4 1
A isPhone() 0 4 3
A isDigits() 0 8 1
A isE164() 0 9 1
A isNANP() 0 6 1
1
<?php
2
namespace LVR\Phone;
3
4
use Illuminate\Contracts\Validation\Rule;
5
6
class Phone implements Rule
7
{
8
    /**
9
     * Determine if the validation rule passes.
10
     *
11
     * @param  string $attribute
12
     * @param  mixed  $value
13
     *
14
     * @return bool
15
     */
16
    public function passes($attribute, $value)
17
    {
18
        return $this->isPhone($value);
19
    }
20
21
    /**
22
     * Get the validation error message.
23
     *
24
     * @return string
25
     */
26
    public function message()
27
    {
28
        return 'Incorrect phone format for :attribute.';
29
    }
30
31
    /**
32
     * Checks through all validation methods to verify it is in a
33
     * phone number format of some type
34
     * @param  string  $value The phone number to check
35
     * @return boolean        is it correct format?
36
     */
37
    protected function isPhone($value)
38
    {
39
        return $this->isE164($value) || $this->isNANP($value) || $this->isDigits($value);
40
    }
41
42
    /**
43
     * Format example 5555555555, 15555555555
44
     * @param  [type]  $value [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
45
     * @return boolean        [description]
46
     */
47
    protected function isDigits($value)
48
    {
49
        $conditions = [];
50
        $conditions[] = strlen($value) >= 10;
51
        $conditions[] = strlen($value) <= 16;
52
        $conditions[] = preg_match("/[^\d]/i", $value) === 0;
53
        return (bool) array_product($conditions);
54
    }
55
56
    /**
57
     * Format example +15555555555
58
     * @param  string  $value The phone number to check
59
     * @return boolean        is it correct format?
60
     */
61
    protected function isE164($value)
62
    {
63
        $conditions = [];
64
        $conditions[] = strpos($value, "+") === 0;
65
        $conditions[] = strlen($value) >= 9;
66
        $conditions[] = strlen($value) <= 16;
67
        $conditions[] = preg_match("/[^\d+]/i", $value) === 0;
68
        return (bool) array_product($conditions);
69
    }
70
71
    /**
72
     * Format examples: (555) 555-5555, 1 (555) 555-5555, 1-555-555-5555, 555-555-5555, 1 555 555-5555
73
     * https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers#United_States.2C_Canada.2C_and_other_NANP_countries
74
     * @param  string  $value The phone number to check
75
     * @return boolean        is it correct format?
76
     */
77
    protected function isNANP($value)
78
    {
79
        $conditions = [];
80
        $conditions[] = preg_match("/^(?:\+1|1)?\s?-?\(?\d{3}\)?(\s|-)?\d{3}-\d{4}$/i", $value) > 0;
81
        return (bool) array_product($conditions);
82
    }
83
}