Completed
Push — master ( fe1b55...a5cb0c )
by Propa
07:17
created
src/PhoneValidator.php 2 patches
Indentation   +195 added lines, -195 removed lines patch added patch discarded remove patch
@@ -10,200 +10,200 @@
 block discarded – undo
10 10
 class PhoneValidator
11 11
 {
12 12
 
13
-    /**
14
-     * The phone number attribute.
15
-     *
16
-     * @var string
17
-     */
18
-    protected $attribute;
19
-
20
-    /**
21
-     * Data from the validator instance.
22
-     *
23
-     * @var array
24
-     */
25
-    protected $data;
26
-
27
-    /**
28
-     * Countries to validate against.
29
-     *
30
-     * @var array
31
-     */
32
-    protected $allowedCountries = [];
33
-
34
-    /**
35
-     * Untransformed phone number types to validate against.
36
-     *
37
-     * @var array
38
-     */
39
-    protected $untransformedTypes = [];
40
-
41
-    /**
42
-     * Transformed phone number types to validate against.
43
-     *
44
-     * @var array
45
-     */
46
-    protected $allowedTypes = [];
47
-
48
-    /**
49
-     * Supplied validator parameters.
50
-     *
51
-     * @var array
52
-     */
53
-    protected $parameters;
54
-
55
-    /**
56
-     * Validates a phone number.
57
-     *
58
-     * @param  string                                     $attribute
59
-     * @param  mixed                                      $value
60
-     * @param  array                                      $parameters
61
-     * @param  \Illuminate\Contracts\Validation\Validator $validator
62
-     * @return bool
63
-     * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
64
-     * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
65
-     */
66
-    public function validatePhone($attribute, $value, array $parameters, Validator $validator)
67
-    {
68
-        $this->attribute = $attribute;
69
-        $this->data = $validator->getData();
70
-        $this->parameters = array_map('strtoupper', $parameters);
71
-
72
-        $this->determineCountries();
73
-        $this->determineTypes();
74
-        $this->checkLeftoverParameters();
75
-
76
-        $phoneUtil = PhoneNumberUtil::getInstance();
77
-
78
-        // Perform validation.
79
-        foreach ($this->allowedCountries as $country) {
80
-            try {
81
-                // For default countries or country field, the following throws NumberParseException if
82
-                // not parsed correctly against the supplied country.
83
-                // For automatic detection: tries to discover the country code using from the number itself.
84
-                $phoneProto = $phoneUtil->parse($value, $country);
85
-
86
-                // For automatic detection, the number should have a country code.
87
-                // Check if type is allowed.
88
-                if ($phoneProto->hasCountryCode() && empty($this->allowedTypes) || in_array($phoneUtil->getNumberType($phoneProto),
89
-                        $this->allowedTypes)
90
-                ) {
91
-
92
-                    // Automatic detection:
93
-                    if ($country == 'ZZ') {
94
-                        // Validate if the international phone number is valid for its contained country.
95
-                        return $phoneUtil->isValidNumber($phoneProto);
96
-                    }
97
-
98
-                    // Force validation of number against the specified country.
99
-                    return $phoneUtil->isValidNumberForRegion($phoneProto, $country);
100
-                }
101
-
102
-            } catch (NumberParseException $e) {
103
-                // Proceed to default validation error.
104
-            }
105
-        }
106
-
107
-        return false;
108
-    }
109
-
110
-    /**
111
-     * Checks if the supplied string is a valid country code using some arbitrary country validation.
112
-     * If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'.
113
-     *
114
-     * @param  string $country
115
-     * @return bool
116
-     */
117
-    public function isPhoneCountry($country)
118
-    {
119
-        return (strlen($country) === 2 && ctype_alpha($country) && ctype_upper($country) && $country != 'ZZ');
120
-    }
121
-
122
-    /**
123
-     * Checks if the supplied string is a valid phone number type.
124
-     *
125
-     * @param  string $type
126
-     * @return bool
127
-     */
128
-    public function isPhoneType($type)
129
-    {
130
-        // Legacy support.
131
-        $type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type);
132
-
133
-        return defined($this->constructPhoneTypeConstant($type));
134
-    }
135
-
136
-    /**
137
-     * Constructs the corresponding namespaced class constant for a phone number type.
138
-     *
139
-     * @param  string $type
140
-     * @return string
141
-     */
142
-    protected function constructPhoneTypeConstant($type)
143
-    {
144
-        return '\libphonenumber\PhoneNumberType::' . $type;
145
-    }
146
-
147
-    /**
148
-     * Sets the countries to validate against.
149
-     *
150
-     * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
151
-     */
152
-    protected function determineCountries()
153
-    {
154
-        // Check for the existence of a country field.
155
-        $field = $this->attribute . '_country';
156
-        if (isset($this->data[$field])) {
157
-            $this->allowedCountries = ($this->isPhoneCountry($this->data[$field])) ? [$this->data[$field]] : [];
158
-            // No exception should be thrown since empty country fields should validate to false.
159
-        } // Or if we need to parse for automatic detection.
160
-        elseif (in_array('AUTO', $this->parameters)) {
161
-            $this->allowedCountries = ['ZZ'];
162
-        } // Else use the supplied parameters.
163
-        else {
164
-            $this->allowedCountries = array_filter($this->parameters, function ($item) {
165
-                return $this->isPhoneCountry($item);
166
-            });
167
-
168
-            if (empty($this->allowedCountries)) {
169
-                throw new NoValidCountryFoundException;
170
-            }
171
-        }
172
-    }
173
-
174
-    /**
175
-     * Sets the phone number types to validate against.
176
-     */
177
-    protected function determineTypes()
178
-    {
179
-        // Get phone types.
180
-        $this->untransformedTypes = array_filter($this->parameters, function ($item) {
181
-            return $this->isPhoneType($item);
182
-        });
183
-
184
-        // Transform valid types to their namespaced class constant.
185
-        $this->allowedTypes = array_map(function ($item) {
186
-            return constant($this->constructPhoneTypeConstant($item));
187
-        }, $this->untransformedTypes);
188
-
189
-        // Add in the unsure number type if applicable.
190
-        if (array_intersect(['FIXED_LINE', 'MOBILE'], $this->parameters)) {
191
-            $this->allowedTypes[] = PhoneNumberType::FIXED_LINE_OR_MOBILE;
192
-        }
193
-    }
194
-
195
-    /**
196
-     * Checks for parameter leftovers to force developers to write proper code.
197
-     *
198
-     * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
199
-     */
200
-    protected function checkLeftoverParameters()
201
-    {
202
-        // Remove the automatic detection option if applicable.
203
-        $leftovers = array_diff($this->parameters, $this->allowedCountries, $this->untransformedTypes, array('AUTO'));
204
-        if (!empty($leftovers)) {
205
-            throw new InvalidParameterException(implode(', ', $leftovers));
206
-        }
207
-    }
13
+	/**
14
+	 * The phone number attribute.
15
+	 *
16
+	 * @var string
17
+	 */
18
+	protected $attribute;
19
+
20
+	/**
21
+	 * Data from the validator instance.
22
+	 *
23
+	 * @var array
24
+	 */
25
+	protected $data;
26
+
27
+	/**
28
+	 * Countries to validate against.
29
+	 *
30
+	 * @var array
31
+	 */
32
+	protected $allowedCountries = [];
33
+
34
+	/**
35
+	 * Untransformed phone number types to validate against.
36
+	 *
37
+	 * @var array
38
+	 */
39
+	protected $untransformedTypes = [];
40
+
41
+	/**
42
+	 * Transformed phone number types to validate against.
43
+	 *
44
+	 * @var array
45
+	 */
46
+	protected $allowedTypes = [];
47
+
48
+	/**
49
+	 * Supplied validator parameters.
50
+	 *
51
+	 * @var array
52
+	 */
53
+	protected $parameters;
54
+
55
+	/**
56
+	 * Validates a phone number.
57
+	 *
58
+	 * @param  string                                     $attribute
59
+	 * @param  mixed                                      $value
60
+	 * @param  array                                      $parameters
61
+	 * @param  \Illuminate\Contracts\Validation\Validator $validator
62
+	 * @return bool
63
+	 * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
64
+	 * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
65
+	 */
66
+	public function validatePhone($attribute, $value, array $parameters, Validator $validator)
67
+	{
68
+		$this->attribute = $attribute;
69
+		$this->data = $validator->getData();
70
+		$this->parameters = array_map('strtoupper', $parameters);
71
+
72
+		$this->determineCountries();
73
+		$this->determineTypes();
74
+		$this->checkLeftoverParameters();
75
+
76
+		$phoneUtil = PhoneNumberUtil::getInstance();
77
+
78
+		// Perform validation.
79
+		foreach ($this->allowedCountries as $country) {
80
+			try {
81
+				// For default countries or country field, the following throws NumberParseException if
82
+				// not parsed correctly against the supplied country.
83
+				// For automatic detection: tries to discover the country code using from the number itself.
84
+				$phoneProto = $phoneUtil->parse($value, $country);
85
+
86
+				// For automatic detection, the number should have a country code.
87
+				// Check if type is allowed.
88
+				if ($phoneProto->hasCountryCode() && empty($this->allowedTypes) || in_array($phoneUtil->getNumberType($phoneProto),
89
+						$this->allowedTypes)
90
+				) {
91
+
92
+					// Automatic detection:
93
+					if ($country == 'ZZ') {
94
+						// Validate if the international phone number is valid for its contained country.
95
+						return $phoneUtil->isValidNumber($phoneProto);
96
+					}
97
+
98
+					// Force validation of number against the specified country.
99
+					return $phoneUtil->isValidNumberForRegion($phoneProto, $country);
100
+				}
101
+
102
+			} catch (NumberParseException $e) {
103
+				// Proceed to default validation error.
104
+			}
105
+		}
106
+
107
+		return false;
108
+	}
109
+
110
+	/**
111
+	 * Checks if the supplied string is a valid country code using some arbitrary country validation.
112
+	 * If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'.
113
+	 *
114
+	 * @param  string $country
115
+	 * @return bool
116
+	 */
117
+	public function isPhoneCountry($country)
118
+	{
119
+		return (strlen($country) === 2 && ctype_alpha($country) && ctype_upper($country) && $country != 'ZZ');
120
+	}
121
+
122
+	/**
123
+	 * Checks if the supplied string is a valid phone number type.
124
+	 *
125
+	 * @param  string $type
126
+	 * @return bool
127
+	 */
128
+	public function isPhoneType($type)
129
+	{
130
+		// Legacy support.
131
+		$type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type);
132
+
133
+		return defined($this->constructPhoneTypeConstant($type));
134
+	}
135
+
136
+	/**
137
+	 * Constructs the corresponding namespaced class constant for a phone number type.
138
+	 *
139
+	 * @param  string $type
140
+	 * @return string
141
+	 */
142
+	protected function constructPhoneTypeConstant($type)
143
+	{
144
+		return '\libphonenumber\PhoneNumberType::' . $type;
145
+	}
146
+
147
+	/**
148
+	 * Sets the countries to validate against.
149
+	 *
150
+	 * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
151
+	 */
152
+	protected function determineCountries()
153
+	{
154
+		// Check for the existence of a country field.
155
+		$field = $this->attribute . '_country';
156
+		if (isset($this->data[$field])) {
157
+			$this->allowedCountries = ($this->isPhoneCountry($this->data[$field])) ? [$this->data[$field]] : [];
158
+			// No exception should be thrown since empty country fields should validate to false.
159
+		} // Or if we need to parse for automatic detection.
160
+		elseif (in_array('AUTO', $this->parameters)) {
161
+			$this->allowedCountries = ['ZZ'];
162
+		} // Else use the supplied parameters.
163
+		else {
164
+			$this->allowedCountries = array_filter($this->parameters, function ($item) {
165
+				return $this->isPhoneCountry($item);
166
+			});
167
+
168
+			if (empty($this->allowedCountries)) {
169
+				throw new NoValidCountryFoundException;
170
+			}
171
+		}
172
+	}
173
+
174
+	/**
175
+	 * Sets the phone number types to validate against.
176
+	 */
177
+	protected function determineTypes()
178
+	{
179
+		// Get phone types.
180
+		$this->untransformedTypes = array_filter($this->parameters, function ($item) {
181
+			return $this->isPhoneType($item);
182
+		});
183
+
184
+		// Transform valid types to their namespaced class constant.
185
+		$this->allowedTypes = array_map(function ($item) {
186
+			return constant($this->constructPhoneTypeConstant($item));
187
+		}, $this->untransformedTypes);
188
+
189
+		// Add in the unsure number type if applicable.
190
+		if (array_intersect(['FIXED_LINE', 'MOBILE'], $this->parameters)) {
191
+			$this->allowedTypes[] = PhoneNumberType::FIXED_LINE_OR_MOBILE;
192
+		}
193
+	}
194
+
195
+	/**
196
+	 * Checks for parameter leftovers to force developers to write proper code.
197
+	 *
198
+	 * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
199
+	 */
200
+	protected function checkLeftoverParameters()
201
+	{
202
+		// Remove the automatic detection option if applicable.
203
+		$leftovers = array_diff($this->parameters, $this->allowedCountries, $this->untransformedTypes, array('AUTO'));
204
+		if (!empty($leftovers)) {
205
+			throw new InvalidParameterException(implode(', ', $leftovers));
206
+		}
207
+	}
208 208
 
209 209
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
             $this->allowedCountries = ['ZZ'];
162 162
         } // Else use the supplied parameters.
163 163
         else {
164
-            $this->allowedCountries = array_filter($this->parameters, function ($item) {
164
+            $this->allowedCountries = array_filter($this->parameters, function($item) {
165 165
                 return $this->isPhoneCountry($item);
166 166
             });
167 167
 
@@ -177,12 +177,12 @@  discard block
 block discarded – undo
177 177
     protected function determineTypes()
178 178
     {
179 179
         // Get phone types.
180
-        $this->untransformedTypes = array_filter($this->parameters, function ($item) {
180
+        $this->untransformedTypes = array_filter($this->parameters, function($item) {
181 181
             return $this->isPhoneType($item);
182 182
         });
183 183
 
184 184
         // Transform valid types to their namespaced class constant.
185
-        $this->allowedTypes = array_map(function ($item) {
185
+        $this->allowedTypes = array_map(function($item) {
186 186
             return constant($this->constructPhoneTypeConstant($item));
187 187
         }, $this->untransformedTypes);
188 188
 
Please login to merge, or discard this patch.
src/helpers.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -1,8 +1,8 @@
 block discarded – undo
1 1
 <?php
2 2
 
3 3
 function phone_format($phone, $country, $format = null) {
4
-    $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
5
-    $phoneProto = $phoneUtil->parse($phone, $country);
6
-    $format = is_null($format) ? \libphonenumber\PhoneNumberFormat::INTERNATIONAL : $format;
7
-    return $phoneUtil->format($phoneProto, $format);
4
+	$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
5
+	$phoneProto = $phoneUtil->parse($phone, $country);
6
+	$format = is_null($format) ? \libphonenumber\PhoneNumberFormat::INTERNATIONAL : $format;
7
+	return $phoneUtil->format($phoneProto, $format);
8 8
 }
Please login to merge, or discard this patch.
src/LaravelPhoneServiceProvider.php 2 patches
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -6,33 +6,33 @@
 block discarded – undo
6 6
 class LaravelPhoneServiceProvider extends ServiceProvider
7 7
 {
8 8
 
9
-    /**
10
-     * Indicates if loading of the provider is deferred.
11
-     *
12
-     * @var bool
13
-     */
14
-    protected $defer = false;
9
+	/**
10
+	 * Indicates if loading of the provider is deferred.
11
+	 *
12
+	 * @var bool
13
+	 */
14
+	protected $defer = false;
15 15
 
16
-    /**
17
-     * Bootstrap the application events.
18
-     *
19
-     * @return void
20
-     */
21
-    public function boot()
22
-    {
23
-        $this->app['validator']->extend('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone');
24
-    }
16
+	/**
17
+	 * Bootstrap the application events.
18
+	 *
19
+	 * @return void
20
+	 */
21
+	public function boot()
22
+	{
23
+		$this->app['validator']->extend('phone', 'Propaganistas\LaravelPhone\PhoneValidator@validatePhone');
24
+	}
25 25
 
26
-    /**
27
-     * Register the service provider.
28
-     *
29
-     * @return void
30
-     */
31
-    public function register()
32
-    {
33
-        // Make libphonenumber available in the application container.
34
-        $this->app->singleton('libphonenumber', function ($app) {
35
-            return PhoneNumberUtil::getInstance();
36
-        });
37
-    }
26
+	/**
27
+	 * Register the service provider.
28
+	 *
29
+	 * @return void
30
+	 */
31
+	public function register()
32
+	{
33
+		// Make libphonenumber available in the application container.
34
+		$this->app->singleton('libphonenumber', function ($app) {
35
+			return PhoneNumberUtil::getInstance();
36
+		});
37
+	}
38 38
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -31,7 +31,7 @@
 block discarded – undo
31 31
     public function register()
32 32
     {
33 33
         // Make libphonenumber available in the application container.
34
-        $this->app->singleton('libphonenumber', function ($app) {
34
+        $this->app->singleton('libphonenumber', function($app) {
35 35
             return PhoneNumberUtil::getInstance();
36 36
         });
37 37
     }
Please login to merge, or discard this patch.