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