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