1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PhoneValidator.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:FormPhone! |
9
|
|
|
* @subpackage Forms |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 12.12.15 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace IPub\FormPhone\Forms; |
16
|
|
|
|
17
|
|
|
use Nette; |
18
|
|
|
use Nette\Forms; |
19
|
|
|
|
20
|
|
|
use libphonenumber; |
21
|
|
|
use libphonenumber\PhoneNumberUtil; |
22
|
|
|
|
23
|
|
|
use IPub\FormPhone; |
24
|
|
|
use IPub\FormPhone\Controls; |
25
|
|
|
use IPub\FormPhone\Exceptions; |
26
|
|
|
|
27
|
|
|
use IPub\Phone; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Phone number control form field validator |
31
|
|
|
* |
32
|
|
|
* @package iPublikuj:FormPhone! |
33
|
|
|
* @subpackage Forms |
34
|
|
|
* |
35
|
|
|
* @author Adam Kadlec <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
class PhoneValidator extends Phone\Forms\PhoneValidator |
38
|
1 |
|
{ |
39
|
|
|
/** |
40
|
|
|
* Define class name |
41
|
|
|
*/ |
42
|
|
|
const CLASS_NAME = __CLASS__; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Define validator calling constant |
46
|
|
|
*/ |
47
|
|
|
const PHONE = 'IPub\FormPhone\Forms\PhoneValidator::validatePhone'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Forms\IControl $control |
51
|
|
|
* @param array|NULL $params |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
* |
55
|
|
|
* @throws Exceptions\NoValidCountryException |
56
|
|
|
*/ |
57
|
|
|
public static function validatePhone(Forms\IControl $control, $params = []) |
58
|
|
|
{ |
59
|
1 |
|
if (!$control instanceof Controls\Phone) { |
60
|
1 |
|
throw new Exceptions\InvalidArgumentException('This validator could be used only on text field. You used it on: "' . get_class($control) . '"'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Get form element value |
64
|
1 |
|
$value = $control->getValue(); |
65
|
|
|
|
66
|
|
|
// Value have to be phone entity |
67
|
1 |
|
if ($value instanceof Phone\Entities\Phone) { |
68
|
1 |
|
$number = $value->getRawOutput(); |
69
|
|
|
|
70
|
|
|
// Wrong value |
71
|
1 |
|
} else { |
72
|
1 |
|
return FALSE; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Get instance of phone number util |
76
|
1 |
|
$phoneNumberUtil = PhoneNumberUtil::getInstance(); |
77
|
|
|
|
78
|
|
|
// Get list of allowed countries from params |
79
|
1 |
|
$allowedCountries = self::determineCountries($control->getAllowedCountries()); |
80
|
|
|
|
81
|
|
|
// Get list of allowed phone types |
82
|
1 |
|
$allowedTypes = self::determineTypes($control->getAllowedPhoneTypes()); |
83
|
|
|
|
84
|
|
|
// Perform validation |
85
|
1 |
|
foreach ($allowedCountries as $country) { |
86
|
|
|
try { |
87
|
|
|
// For default countries or country field, the following throws NumberParseException if |
88
|
|
|
// not parsed correctly against the supplied country |
89
|
|
|
// For automatic detection: tries to discover the country code using from the number itself |
90
|
1 |
|
$phoneProto = $phoneNumberUtil->parse($number, $country); |
91
|
|
|
|
92
|
|
|
// For automatic detection, the number should have a country code |
93
|
|
|
// Check if type is allowed |
94
|
|
|
if ( |
95
|
1 |
|
$phoneProto->hasCountryCode() && |
96
|
1 |
|
$allowedTypes === [] || |
97
|
1 |
|
in_array($phoneNumberUtil->getNumberType($phoneProto), $allowedTypes) |
98
|
1 |
|
) { |
99
|
|
|
// Automatic detection: |
100
|
1 |
|
if ($country == 'ZZ') { |
101
|
|
|
// Validate if the international phone number is valid for its contained country |
102
|
|
|
return $phoneNumberUtil->isValidNumber($phoneProto); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Validate number against the specified country. Return only if success |
106
|
|
|
// If failure, continue loop to next specified country |
107
|
1 |
|
if ($phoneNumberUtil->isValidNumberForRegion($phoneProto, $country)) { |
108
|
1 |
|
return TRUE; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
} catch (libphonenumber\NumberParseException $ex) { |
113
|
|
|
// Proceed to default validation error |
114
|
|
|
} |
115
|
1 |
|
} |
116
|
|
|
|
117
|
|
|
// All specified country validations have failed |
118
|
1 |
|
return FALSE; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|