Completed
Push — master ( ff7651...bda366 )
by Propa
06:07 queued 03:38
created
src/Traits/ParsesCountries.php 1 patch
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -5,39 +5,39 @@
 block discarded – undo
5 5
 
6 6
 trait ParsesCountries
7 7
 {
8
-    /**
9
-     * Determine whether the given country code is valid.
10
-     *
11
-     * @param string $country
12
-     * @return bool
13
-     */
14
-    public static function isValidCountryCode($country)
15
-    {
16
-    	$iso3166 = new ISO3166;
8
+	/**
9
+	 * Determine whether the given country code is valid.
10
+	 *
11
+	 * @param string $country
12
+	 * @return bool
13
+	 */
14
+	public static function isValidCountryCode($country)
15
+	{
16
+		$iso3166 = new ISO3166;
17 17
 
18
-    	try {
19
-    		$iso3166->alpha2($country);
18
+		try {
19
+			$iso3166->alpha2($country);
20 20
 
21
-    		return true;
22
-    	} catch (\Exception $e) {
23
-    		return false;
24
-    	}
25
-    }
21
+			return true;
22
+		} catch (\Exception $e) {
23
+			return false;
24
+		}
25
+	}
26 26
 
27
-    /**
28
-     * Parse the provided phone countries to a valid array.
29
-     *
30
-     * @param string|array $countries
31
-     * @return array
32
-     */
33
-    protected function parseCountries($countries)
34
-    {
35
-        return Collection::make(is_array($countries) ? $countries : func_get_args())
36
-                         ->map(function ($country) {
37
-                             return strtoupper($country);
38
-                         })
39
-                         ->filter(function ($value) {
40
-                             return static::isValidCountryCode($value);
41
-                         })->toArray();
42
-    }
27
+	/**
28
+	 * Parse the provided phone countries to a valid array.
29
+	 *
30
+	 * @param string|array $countries
31
+	 * @return array
32
+	 */
33
+	protected function parseCountries($countries)
34
+	{
35
+		return Collection::make(is_array($countries) ? $countries : func_get_args())
36
+						 ->map(function ($country) {
37
+							 return strtoupper($country);
38
+						 })
39
+						 ->filter(function ($value) {
40
+							 return static::isValidCountryCode($value);
41
+						 })->toArray();
42
+	}
43 43
 }
44 44
\ No newline at end of file
Please login to merge, or discard this patch.
src/Validation/Phone.php 1 patch
Indentation   +130 added lines, -130 removed lines patch added patch discarded remove patch
@@ -11,134 +11,134 @@
 block discarded – undo
11 11
 
12 12
 class Phone
13 13
 {
14
-    use ParsesCountries,
15
-        ParsesTypes;
16
-
17
-    /**
18
-     * @var \libphonenumber\PhoneNumberUtil
19
-     */
20
-    protected $lib;
21
-
22
-    /**
23
-     * Phone constructor.
24
-     */
25
-    public function __construct()
26
-    {
27
-        $this->lib = PhoneNumberUtil::getInstance();
28
-    }
29
-
30
-    /**
31
-     * Validates a phone number.
32
-     *
33
-     * @param  string $attribute
34
-     * @param  mixed  $value
35
-     * @param  array  $parameters
36
-     * @param  object $validator
37
-     * @return bool
38
-     */
39
-    public function validate($attribute, $value, array $parameters, $validator)
40
-    {
41
-        $data = $validator->getData();
42
-
43
-        list(
44
-            $countries,
45
-            $types,
46
-            $detect,
47
-            $lenient) = $this->extractParameters($attribute, $parameters, $data);
48
-
49
-        // A "null" country is prepended:
50
-        // 1. In case of auto-detection to have the validation run first without supplying a country.
51
-        // 2. In case of lenient validation without provided countries; we still might have some luck...
52
-        if ($detect || ($lenient && empty($countries))) {
53
-            array_unshift($countries, null);
54
-        }
55
-
56
-        foreach ($countries as $country) {
57
-            try {
58
-                // Parsing the phone number also validates the country, so no need to do this explicitly.
59
-                // It'll throw a PhoneCountryException upon failure.
60
-                $phoneNumber = PhoneNumber::make($value, $country);
61
-
62
-                // Type validation.
63
-                if (! empty($types) && ! $phoneNumber->isOfType($types)) {
64
-                    continue;
65
-                }
66
-
67
-                $lenientPhoneNumber = $phoneNumber->lenient()->getPhoneNumberInstance();
68
-
69
-                // Lenient validation.
70
-                if ($lenient && $this->lib->isPossibleNumber($lenientPhoneNumber, $country)) {
71
-                    return true;
72
-                }
73
-
74
-                $phoneNumberInstance = $phoneNumber->getPhoneNumberInstance();
75
-
76
-                // Country detection.
77
-                if ($detect && $this->lib->isValidNumber($phoneNumberInstance)) {
78
-                    return true;
79
-                }
80
-
81
-                // Default number+country validation.
82
-                if ($this->lib->isValidNumberForRegion($phoneNumberInstance, $country)) {
83
-                    return true;
84
-                }
85
-            } catch (NumberParseException $e) {
86
-                continue;
87
-            }
88
-        }
89
-
90
-        return false;
91
-    }
92
-
93
-    /**
94
-     * Parse and extract parameters in the appropriate validation arguments.
95
-     *
96
-     * @param string $attribute
97
-     * @param array  $parameters
98
-     * @param array  $data
99
-     * @return array
100
-     * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
101
-     */
102
-    protected function extractParameters($attribute, array $parameters, array $data)
103
-    {
104
-        $originalParameters = $parameters;
105
-        $parameters = array_map('strtolower', $parameters);
106
-
107
-        // Discover if an input field was provided. If not, guess the field's name.
108
-        $inputField = Collection::make($parameters)
109
-                                ->intersect(array_keys(Arr::dot($data)))
110
-                                ->first() ?: "${attribute}_country";
111
-
112
-        // Attempt to retrieve the field's value.
113
-        if ($inputCountry = Arr::get($data, $inputField)) {
114
-
115
-            if (static::isValidType($inputField)) {
116
-                throw InvalidParameterException::ambiguous($inputField);
117
-            }
118
-
119
-            // Invalid country field values should just validate to false, and this is exactly what
120
-            // we're getting when simply excluding invalid values.
121
-            // This will also prevent parameter hijacking through the country field.
122
-            if (static::isValidCountryCode($inputCountry)) {
123
-                $parameters[] = $inputCountry;
124
-            }
125
-        }
126
-
127
-        $countries = static::parseCountries($parameters);
128
-        $types = static::parseTypes($parameters);
129
-        $auto = in_array('auto', $parameters);
130
-        $lenient = in_array('lenient', $parameters);
131
-
132
-        // Force developers to write proper code.
133
-        // Since the static parsers return a validated array with preserved keys, we can safely diff against the keys.
134
-        // Unfortunately we can't use $collection->diffKeys() as it's not available yet in earlier 5.* versions.
135
-        $leftovers = array_diff_key($parameters, $countries, $types);
136
-        $leftovers = array_diff($leftovers, ['auto', 'lenient', $inputField]);
137
-
138
-        if (! empty($leftovers)) {
139
-            throw InvalidParameterException::parameters(array_intersect_key($originalParameters, $leftovers));
140
-        }
141
-
142
-        return [$countries, $types, $auto, $lenient, $inputField];
143
-    }
14
+	use ParsesCountries,
15
+		ParsesTypes;
16
+
17
+	/**
18
+	 * @var \libphonenumber\PhoneNumberUtil
19
+	 */
20
+	protected $lib;
21
+
22
+	/**
23
+	 * Phone constructor.
24
+	 */
25
+	public function __construct()
26
+	{
27
+		$this->lib = PhoneNumberUtil::getInstance();
28
+	}
29
+
30
+	/**
31
+	 * Validates a phone number.
32
+	 *
33
+	 * @param  string $attribute
34
+	 * @param  mixed  $value
35
+	 * @param  array  $parameters
36
+	 * @param  object $validator
37
+	 * @return bool
38
+	 */
39
+	public function validate($attribute, $value, array $parameters, $validator)
40
+	{
41
+		$data = $validator->getData();
42
+
43
+		list(
44
+			$countries,
45
+			$types,
46
+			$detect,
47
+			$lenient) = $this->extractParameters($attribute, $parameters, $data);
48
+
49
+		// A "null" country is prepended:
50
+		// 1. In case of auto-detection to have the validation run first without supplying a country.
51
+		// 2. In case of lenient validation without provided countries; we still might have some luck...
52
+		if ($detect || ($lenient && empty($countries))) {
53
+			array_unshift($countries, null);
54
+		}
55
+
56
+		foreach ($countries as $country) {
57
+			try {
58
+				// Parsing the phone number also validates the country, so no need to do this explicitly.
59
+				// It'll throw a PhoneCountryException upon failure.
60
+				$phoneNumber = PhoneNumber::make($value, $country);
61
+
62
+				// Type validation.
63
+				if (! empty($types) && ! $phoneNumber->isOfType($types)) {
64
+					continue;
65
+				}
66
+
67
+				$lenientPhoneNumber = $phoneNumber->lenient()->getPhoneNumberInstance();
68
+
69
+				// Lenient validation.
70
+				if ($lenient && $this->lib->isPossibleNumber($lenientPhoneNumber, $country)) {
71
+					return true;
72
+				}
73
+
74
+				$phoneNumberInstance = $phoneNumber->getPhoneNumberInstance();
75
+
76
+				// Country detection.
77
+				if ($detect && $this->lib->isValidNumber($phoneNumberInstance)) {
78
+					return true;
79
+				}
80
+
81
+				// Default number+country validation.
82
+				if ($this->lib->isValidNumberForRegion($phoneNumberInstance, $country)) {
83
+					return true;
84
+				}
85
+			} catch (NumberParseException $e) {
86
+				continue;
87
+			}
88
+		}
89
+
90
+		return false;
91
+	}
92
+
93
+	/**
94
+	 * Parse and extract parameters in the appropriate validation arguments.
95
+	 *
96
+	 * @param string $attribute
97
+	 * @param array  $parameters
98
+	 * @param array  $data
99
+	 * @return array
100
+	 * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
101
+	 */
102
+	protected function extractParameters($attribute, array $parameters, array $data)
103
+	{
104
+		$originalParameters = $parameters;
105
+		$parameters = array_map('strtolower', $parameters);
106
+
107
+		// Discover if an input field was provided. If not, guess the field's name.
108
+		$inputField = Collection::make($parameters)
109
+								->intersect(array_keys(Arr::dot($data)))
110
+								->first() ?: "${attribute}_country";
111
+
112
+		// Attempt to retrieve the field's value.
113
+		if ($inputCountry = Arr::get($data, $inputField)) {
114
+
115
+			if (static::isValidType($inputField)) {
116
+				throw InvalidParameterException::ambiguous($inputField);
117
+			}
118
+
119
+			// Invalid country field values should just validate to false, and this is exactly what
120
+			// we're getting when simply excluding invalid values.
121
+			// This will also prevent parameter hijacking through the country field.
122
+			if (static::isValidCountryCode($inputCountry)) {
123
+				$parameters[] = $inputCountry;
124
+			}
125
+		}
126
+
127
+		$countries = static::parseCountries($parameters);
128
+		$types = static::parseTypes($parameters);
129
+		$auto = in_array('auto', $parameters);
130
+		$lenient = in_array('lenient', $parameters);
131
+
132
+		// Force developers to write proper code.
133
+		// Since the static parsers return a validated array with preserved keys, we can safely diff against the keys.
134
+		// Unfortunately we can't use $collection->diffKeys() as it's not available yet in earlier 5.* versions.
135
+		$leftovers = array_diff_key($parameters, $countries, $types);
136
+		$leftovers = array_diff($leftovers, ['auto', 'lenient', $inputField]);
137
+
138
+		if (! empty($leftovers)) {
139
+			throw InvalidParameterException::parameters(array_intersect_key($originalParameters, $leftovers));
140
+		}
141
+
142
+		return [$countries, $types, $auto, $lenient, $inputField];
143
+	}
144 144
 }
Please login to merge, or discard this patch.