Phone::detect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Propaganistas\LaravelPhone\Rules;
2
3
use libphonenumber\PhoneNumberType;
4
use Propaganistas\LaravelPhone\Traits\ParsesCountries;
5
use Propaganistas\LaravelPhone\Traits\ParsesTypes;
6
7
class Phone
8
{
9
    use ParsesTypes;
10
    
11
    /**
12
     * The provided phone countries.
13
     *
14
     * @var array
15
     */
16
    protected $countries = [];
17
18
    /**
19
     * The input field name to check for a country value.
20
     *
21
     * @var string
22
     */
23
    protected $countryField;
24
25
    /**
26
     * The provided phone types.
27
     *
28
     * @var array
29
     */
30
    protected $types = [];
31
32
    /**
33
     * Whether the number's country should be auto-detected.
34
     *
35
     * @var bool
36
     */
37
    protected $detect = false;
38
39
    /**
40
     * Whether to allow lenient checks (i.e. landline numbers without area codes).
41
     *
42
     * @var bool
43
     */
44
    protected $lenient = false;
45
46
    /**
47
     * Set the phone countries.
48
     *
49
     * @param string|array $country
50
     * @return $this
51
     */
52 3
    public function country($country)
53
    {
54 3
        $countries = is_array($country) ? $country : func_get_args();
55
56 3
        $this->countries = array_merge($this->countries, $countries);
57
58 3
        return $this;
59
    }
60
61
    /**
62
     * Set the country input field.
63
     *
64
     * @param string $name
65
     * @return $this
66
     */
67 3
    public function countryField($name)
68
    {
69 3
        $this->countryField = $name;
70
71 3
        return $this;
72
    }
73
74
    /**
75
     * Set the phone types.
76
     *
77
     * @param string|array $type
78
     * @return $this
79
     */
80 3
    public function type($type)
81
    {
82 3
        $types = is_array($type) ? $type : func_get_args();
83
84 3
        $this->types = array_merge($this->types, $types);
85
86 3
        return $this;
87
    }
88
89
    /**
90
     * Shortcut method for mobile type restriction.
91
     *
92
     * @return $this
93
     */
94 3
    public function mobile()
95
    {
96 3
        $this->type(PhoneNumberType::MOBILE);
97
98 3
        return $this;
99
    }
100
101
    /**
102
     * Shortcut method for fixed line type restriction.
103
     *
104
     * @return $this
105
     */
106 3
    public function fixedLine()
107
    {
108 3
        $this->type(PhoneNumberType::FIXED_LINE);
109
110 3
        return $this;
111
    }
112
113
    /**
114
     * Enable automatic country detection.
115
     *
116
     * @return $this
117
     */
118 3
    public function detect()
119
    {
120 3
        $this->detect = true;
121
122 3
        return $this;
123
    }
124
125
    /**
126
     * Enable lenient number checking.
127
     *
128
     * @return $this
129
     */
130 3
    public function lenient()
131
    {
132 3
        $this->lenient = true;
133
134 3
        return $this;
135
    }
136
137
    /**
138
     * Convert the rule to a validation string.
139
     *
140
     * @return string
141
     */
142 3
    public function __toString()
143
    {
144 3
        $parameters = implode(',', array_merge(
145 3
            $this->countries,
146 3
            static::parseTypes($this->types),
147 3
            ($this->countryField ? [$this->countryField]: []),
148 3
            ($this->detect ? ['AUTO'] : []),
149 3
            ($this->lenient ? ['LENIENT'] : [])
150
        ));
151
152 3
        return 'phone' . (! empty($parameters) ? ":$parameters" : '');
153
    }
154
}
155