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