Completed
Push — master ( e01736...ba6a43 )
by Adam
04:12
created

PhoneValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 90.48%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 75
ccs 19
cts 21
cp 0.9048
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validatePhone() 0 54 9
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
		// Get instance of phone number util
67 1
		$phoneNumberUtil = PhoneNumberUtil::getInstance();
68
69
		// Get list of allowed countries from params
70 1
		$allowedCountries = self::determineCountries($control->getCountries());
71
72
		// Get list of allowed phone types
73 1
		$allowedTypes = self::determineTypes($control->getPhoneTypes());
74
75
		// Perform validation
76 1
		foreach ($allowedCountries as $country) {
77
			try {
78
				// For default countries or country field, the following throws NumberParseException if
79
				// not parsed correctly against the supplied country
80
				// For automatic detection: tries to discover the country code using from the number itself
81 1
				$phoneProto = $phoneNumberUtil->parse($value, $country);
82
83
				// For automatic detection, the number should have a country code
84
				// Check if type is allowed
85
				if (
86 1
					$phoneProto->hasCountryCode() &&
87 1
					$allowedTypes === [] ||
88 1
					in_array($phoneNumberUtil->getNumberType($phoneProto), $allowedTypes)
89 1
				) {
90
					// Automatic detection:
91 1
					if ($country == 'ZZ') {
92
						// Validate if the international phone number is valid for its contained country
93
						return $phoneNumberUtil->isValidNumber($phoneProto);
94
					}
95
96
					// Validate number against the specified country. Return only if success
97
					// If failure, continue loop to next specified country
98 1
					if ($phoneNumberUtil->isValidNumberForRegion($phoneProto, $country)) {
99 1
						return TRUE;
100
					}
101
				}
102
103 1
			} catch (libphonenumber\NumberParseException $ex) {
104
				// Proceed to default validation error
105
			}
106 1
		}
107
108
		// All specified country validations have failed
109 1
		return FALSE;
110
	}
111
}
112