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