Completed
Branch dev-v3 (d0b49e)
by Propa
01:27
created
src/Validation/Phone.php 3 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -4,7 +4,6 @@
 block discarded – undo
4 4
 use Illuminate\Support\Collection;
5 5
 use libphonenumber\PhoneNumberUtil;
6 6
 use Propaganistas\LaravelPhone\Exceptions\InvalidParameterException;
7
-use Propaganistas\LaravelPhone\Exceptions\CountryCodeException;
8 7
 use Propaganistas\LaravelPhone\Exceptions\NumberParseException;
9 8
 use Propaganistas\LaravelPhone\PhoneNumber;
10 9
 use Propaganistas\LaravelPhone\Traits\ParsesCountries;
Please login to merge, or discard this patch.
Indentation   +121 added lines, -121 removed lines patch added patch discarded remove patch
@@ -12,125 +12,125 @@
 block discarded – undo
12 12
 
13 13
 class Phone
14 14
 {
15
-    use ParsesCountries,
16
-        ParsesTypes;
17
-
18
-    /**
19
-     * @var \libphonenumber\PhoneNumberUtil
20
-     */
21
-    protected $lib;
22
-
23
-    /**
24
-     * Phone constructor.
25
-     */
26
-    public function __construct()
27
-    {
28
-        $this->lib = PhoneNumberUtil::getInstance();
29
-    }
30
-
31
-    /**
32
-     * Validates a phone number.
33
-     *
34
-     * @param  string $attribute
35
-     * @param  mixed  $value
36
-     * @param  array  $parameters
37
-     * @param  object $validator
38
-     * @return bool
39
-     */
40
-    public function validate($attribute, $value, array $parameters, $validator)
41
-    {
42
-        $data = $validator->getData();
43
-
44
-        list(
45
-            $countries,
46
-            $types,
47
-            $detect,
48
-            $lenient) = $this->extractParameters($attribute, $parameters, $data);
49
-
50
-        // A "null" country is prepended:
51
-        // 1. In case of auto-detection to have the validation run first without supplying a country.
52
-        // 2. In case of lenient validation without provided countries; we still might have some luck...
53
-        if ($detect || ($lenient && empty($countries))) {
54
-            array_unshift($countries, null);
55
-        }
56
-
57
-        foreach ($countries as $country) {
58
-            try {
59
-                // Parsing the phone number also validates the country, so no need to do this explicitly.
60
-                // It'll throw a PhoneCountryException upon failure.
61
-                $phoneNumber = PhoneNumber::make($value, $country);
62
-
63
-                // Type validation.
64
-                if (! empty($types) && ! $phoneNumber->isOfType($types)) {
65
-                    continue;
66
-                }
67
-
68
-                $lenientPhoneNumber = $phoneNumber->lenient()->getPhoneNumberInstance();
69
-
70
-                // Lenient validation.
71
-                if ($lenient && $this->lib->isPossibleNumber($lenientPhoneNumber, $country)) {
72
-                    return true;
73
-                }
74
-
75
-                $phoneNumberInstance = $phoneNumber->getPhoneNumberInstance();
76
-
77
-
78
-                // Country detection.
79
-                if ($detect && $this->lib->isValidNumber($phoneNumberInstance)) {
80
-                    return true;
81
-                }
82
-
83
-                // Default number+country validation.
84
-                if ($this->lib->isValidNumberForRegion($phoneNumberInstance, $country)) {
85
-                    return true;
86
-                }
87
-            } catch (NumberParseException $e) {
88
-                continue;
89
-            }
90
-        }
91
-
92
-        return false;
93
-    }
94
-
95
-    /**
96
-     * Parse and extract parameters in the appropriate validation arguments.
97
-     *
98
-     * @param string $attribute
99
-     * @param array  $parameters
100
-     * @param array  $data
101
-     * @return array
102
-     * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
103
-     */
104
-    protected function extractParameters($attribute, array $parameters, array $data)
105
-    {
106
-        // Discover if an input field was provided. If not, guess the field's name.
107
-        $inputField = Collection::make($parameters)
108
-                                ->intersect(array_keys(Arr::dot($data)))
109
-                                ->first(null, "${attribute}_country");
110
-
111
-        // Attempt to retrieve the field's value. If no field is present, this will be null.
112
-        $inputCountry = Arr::get($data, $inputField);
113
-
114
-        $countries = static::parseCountries($inputCountry ? [$inputCountry] : $parameters);
115
-        $types = static::parseTypes($parameters);
116
-
117
-        // Force developers to write proper code.
118
-        // Since the static parsers return a validated array with preserved keys, we can safely diff against the keys.
119
-        $leftovers = Collection::make($parameters)
120
-                               ->diffKeys($types)
121
-                               ->diffKeys($inputCountry ? [] : $countries)
122
-                               ->diff(['AUTO', 'LENIENT', $inputField]);
123
-
124
-        if ($leftovers->isNotEmpty()) {
125
-            throw InvalidParameterException::parameters($leftovers);
126
-        }
127
-
128
-        return [
129
-            $countries,
130
-            $types,
131
-            in_array('AUTO', $parameters),
132
-            in_array('LENIENT', $parameters),
133
-            $inputField,
134
-        ];
135
-    }
15
+	use ParsesCountries,
16
+		ParsesTypes;
17
+
18
+	/**
19
+	 * @var \libphonenumber\PhoneNumberUtil
20
+	 */
21
+	protected $lib;
22
+
23
+	/**
24
+	 * Phone constructor.
25
+	 */
26
+	public function __construct()
27
+	{
28
+		$this->lib = PhoneNumberUtil::getInstance();
29
+	}
30
+
31
+	/**
32
+	 * Validates a phone number.
33
+	 *
34
+	 * @param  string $attribute
35
+	 * @param  mixed  $value
36
+	 * @param  array  $parameters
37
+	 * @param  object $validator
38
+	 * @return bool
39
+	 */
40
+	public function validate($attribute, $value, array $parameters, $validator)
41
+	{
42
+		$data = $validator->getData();
43
+
44
+		list(
45
+			$countries,
46
+			$types,
47
+			$detect,
48
+			$lenient) = $this->extractParameters($attribute, $parameters, $data);
49
+
50
+		// A "null" country is prepended:
51
+		// 1. In case of auto-detection to have the validation run first without supplying a country.
52
+		// 2. In case of lenient validation without provided countries; we still might have some luck...
53
+		if ($detect || ($lenient && empty($countries))) {
54
+			array_unshift($countries, null);
55
+		}
56
+
57
+		foreach ($countries as $country) {
58
+			try {
59
+				// Parsing the phone number also validates the country, so no need to do this explicitly.
60
+				// It'll throw a PhoneCountryException upon failure.
61
+				$phoneNumber = PhoneNumber::make($value, $country);
62
+
63
+				// Type validation.
64
+				if (! empty($types) && ! $phoneNumber->isOfType($types)) {
65
+					continue;
66
+				}
67
+
68
+				$lenientPhoneNumber = $phoneNumber->lenient()->getPhoneNumberInstance();
69
+
70
+				// Lenient validation.
71
+				if ($lenient && $this->lib->isPossibleNumber($lenientPhoneNumber, $country)) {
72
+					return true;
73
+				}
74
+
75
+				$phoneNumberInstance = $phoneNumber->getPhoneNumberInstance();
76
+
77
+
78
+				// Country detection.
79
+				if ($detect && $this->lib->isValidNumber($phoneNumberInstance)) {
80
+					return true;
81
+				}
82
+
83
+				// Default number+country validation.
84
+				if ($this->lib->isValidNumberForRegion($phoneNumberInstance, $country)) {
85
+					return true;
86
+				}
87
+			} catch (NumberParseException $e) {
88
+				continue;
89
+			}
90
+		}
91
+
92
+		return false;
93
+	}
94
+
95
+	/**
96
+	 * Parse and extract parameters in the appropriate validation arguments.
97
+	 *
98
+	 * @param string $attribute
99
+	 * @param array  $parameters
100
+	 * @param array  $data
101
+	 * @return array
102
+	 * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
103
+	 */
104
+	protected function extractParameters($attribute, array $parameters, array $data)
105
+	{
106
+		// Discover if an input field was provided. If not, guess the field's name.
107
+		$inputField = Collection::make($parameters)
108
+								->intersect(array_keys(Arr::dot($data)))
109
+								->first(null, "${attribute}_country");
110
+
111
+		// Attempt to retrieve the field's value. If no field is present, this will be null.
112
+		$inputCountry = Arr::get($data, $inputField);
113
+
114
+		$countries = static::parseCountries($inputCountry ? [$inputCountry] : $parameters);
115
+		$types = static::parseTypes($parameters);
116
+
117
+		// Force developers to write proper code.
118
+		// Since the static parsers return a validated array with preserved keys, we can safely diff against the keys.
119
+		$leftovers = Collection::make($parameters)
120
+							   ->diffKeys($types)
121
+							   ->diffKeys($inputCountry ? [] : $countries)
122
+							   ->diff(['AUTO', 'LENIENT', $inputField]);
123
+
124
+		if ($leftovers->isNotEmpty()) {
125
+			throw InvalidParameterException::parameters($leftovers);
126
+		}
127
+
128
+		return [
129
+			$countries,
130
+			$types,
131
+			in_array('AUTO', $parameters),
132
+			in_array('LENIENT', $parameters),
133
+			$inputField,
134
+		];
135
+	}
136 136
 }
137 137
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -61,7 +61,7 @@
 block discarded – undo
61 61
                 $phoneNumber = PhoneNumber::make($value, $country);
62 62
 
63 63
                 // Type validation.
64
-                if (! empty($types) && ! $phoneNumber->isOfType($types)) {
64
+                if (!empty($types) && !$phoneNumber->isOfType($types)) {
65 65
                     continue;
66 66
                 }
67 67
 
Please login to merge, or discard this patch.
src/Exceptions/InvalidParameterException.php 2 patches
Indentation   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -4,16 +4,16 @@
 block discarded – undo
4 4
 
5 5
 class InvalidParameterException extends \Exception
6 6
 {
7
-    /**
8
-     * Invalid parameters static constructor.
9
-     *
10
-     * @param array|Collection $parameters
11
-     * @return static
12
-     */
13
-    public static function parameters($parameters)
14
-    {
15
-        $parameters = Collection::make($parameters);
7
+	/**
8
+	 * Invalid parameters static constructor.
9
+	 *
10
+	 * @param array|Collection $parameters
11
+	 * @return static
12
+	 */
13
+	public static function parameters($parameters)
14
+	{
15
+		$parameters = Collection::make($parameters);
16 16
 
17
-        return new static('Invalid phone validation parameters: "' . $parameters->implode(',') . '".');
18
-    }
17
+		return new static('Invalid phone validation parameters: "' . $parameters->implode(',') . '".');
18
+	}
19 19
 }
20 20
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -14,6 +14,6 @@
 block discarded – undo
14 14
     {
15 15
         $parameters = Collection::make($parameters);
16 16
 
17
-        return new static('Invalid phone validation parameters: "' . $parameters->implode(',') . '".');
17
+        return new static('Invalid phone validation parameters: "'.$parameters->implode(',').'".');
18 18
     }
19 19
 }
20 20
\ No newline at end of file
Please login to merge, or discard this patch.
src/Exceptions/NumberParseException.php 2 patches
Indentation   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -4,17 +4,17 @@
 block discarded – undo
4 4
 
5 5
 class NumberParseException extends libNumberParseException
6 6
 {
7
-    /**
8
-     * Country specification required static constructor.
9
-     *
10
-     * @param string $number
11
-     * @return static
12
-     */
13
-    public static function countryRequired($number)
14
-    {
15
-        return new static(
16
-            libNumberParseException::INVALID_COUNTRY_CODE,
17
-            'Country specification for number "' . $number . '" required.'
18
-        );
19
-    }
7
+	/**
8
+	 * Country specification required static constructor.
9
+	 *
10
+	 * @param string $number
11
+	 * @return static
12
+	 */
13
+	public static function countryRequired($number)
14
+	{
15
+		return new static(
16
+			libNumberParseException::INVALID_COUNTRY_CODE,
17
+			'Country specification for number "' . $number . '" required.'
18
+		);
19
+	}
20 20
 }
21 21
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -14,7 +14,7 @@
 block discarded – undo
14 14
     {
15 15
         return new static(
16 16
             libNumberParseException::INVALID_COUNTRY_CODE,
17
-            'Country specification for number "' . $number . '" required.'
17
+            'Country specification for number "'.$number.'" required.'
18 18
         );
19 19
     }
20 20
 }
21 21
\ No newline at end of file
Please login to merge, or discard this patch.
src/Exceptions/CountryCodeException.php 2 patches
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -2,14 +2,14 @@
 block discarded – undo
2 2
 
3 3
 class CountryCodeException extends \Exception
4 4
 {
5
-    /**
6
-     * Invalid country code static constructor.
7
-     *
8
-     * @param string $country
9
-     * @return static
10
-     */
11
-    public static function invalid($country)
12
-    {
13
-        return new static('Invalid country code "' . $country . '".');
14
-    }
5
+	/**
6
+	 * Invalid country code static constructor.
7
+	 *
8
+	 * @param string $country
9
+	 * @return static
10
+	 */
11
+	public static function invalid($country)
12
+	{
13
+		return new static('Invalid country code "' . $country . '".');
14
+	}
15 15
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -10,6 +10,6 @@
 block discarded – undo
10 10
      */
11 11
     public static function invalid($country)
12 12
     {
13
-        return new static('Invalid country code "' . $country . '".');
13
+        return new static('Invalid country code "'.$country.'".');
14 14
     }
15 15
 }
Please login to merge, or discard this patch.
src/Exceptions/NumberFormatException.php 2 patches
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -2,14 +2,14 @@
 block discarded – undo
2 2
 
3 3
 class NumberFormatException extends \Exception
4 4
 {
5
-    /**
6
-     * Invalid number format static constructor.
7
-     *
8
-     * @param string $format
9
-     * @return static
10
-     */
11
-    public static function invalid($format)
12
-    {
13
-        return new static('Invalid number format "' . $format . '".');
14
-    }
5
+	/**
6
+	 * Invalid number format static constructor.
7
+	 *
8
+	 * @param string $format
9
+	 * @return static
10
+	 */
11
+	public static function invalid($format)
12
+	{
13
+		return new static('Invalid number format "' . $format . '".');
14
+	}
15 15
 }
16 16
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -10,6 +10,6 @@
 block discarded – undo
10 10
      */
11 11
     public static function invalid($format)
12 12
     {
13
-        return new static('Invalid number format "' . $format . '".');
13
+        return new static('Invalid number format "'.$format.'".');
14 14
     }
15 15
 }
16 16
\ No newline at end of file
Please login to merge, or discard this patch.
src/helpers.php 2 patches
Indentation   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -3,22 +3,22 @@
 block discarded – undo
3 3
 use Propaganistas\LaravelPhone\PhoneNumber;
4 4
 
5 5
 if (! function_exists('phone')) {
6
-    /**
7
-     * Get a PhoneNumber instance or a formatted string.
8
-     *
9
-     * @param string       $number
10
-     * @param string|array $country
11
-     * @param string       $format
12
-     * @return string|Propaganistas\LaravelPhone\PhoneNumber
13
-     */
14
-    function phone($number, $country = null, $format = null)
15
-    {
16
-        $phone = PhoneNumber::make($number, $country);
6
+	/**
7
+	 * Get a PhoneNumber instance or a formatted string.
8
+	 *
9
+	 * @param string       $number
10
+	 * @param string|array $country
11
+	 * @param string       $format
12
+	 * @return string|Propaganistas\LaravelPhone\PhoneNumber
13
+	 */
14
+	function phone($number, $country = null, $format = null)
15
+	{
16
+		$phone = PhoneNumber::make($number, $country);
17 17
 
18
-        if (! is_null($format)) {
19
-            return $phone->format($format);
20
-        }
18
+		if (! is_null($format)) {
19
+			return $phone->format($format);
20
+		}
21 21
 
22
-        return $phone;
23
-    }
22
+		return $phone;
23
+	}
24 24
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -2,7 +2,7 @@  discard block
 block discarded – undo
2 2
 
3 3
 use Propaganistas\LaravelPhone\PhoneNumber;
4 4
 
5
-if (! function_exists('phone')) {
5
+if (!function_exists('phone')) {
6 6
     /**
7 7
      * Get a PhoneNumber instance or a formatted string.
8 8
      *
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
15 15
     {
16 16
         $phone = PhoneNumber::make($number, $country);
17 17
 
18
-        if (! is_null($format)) {
18
+        if (!is_null($format)) {
19 19
             return $phone->format($format);
20 20
         }
21 21
 
Please login to merge, or discard this patch.
src/PhoneServiceProvider.php 2 patches
Indentation   +51 added lines, -51 removed lines patch added patch discarded remove patch
@@ -10,63 +10,63 @@
 block discarded – undo
10 10
 
11 11
 class PhoneServiceProvider extends ServiceProvider
12 12
 {
13
-    /**
14
-     * Bootstrap the application events.
15
-     *
16
-     * @return void
17
-     */
18
-    public function boot()
19
-    {
20
-        $this->registerValidator();
13
+	/**
14
+	 * Bootstrap the application events.
15
+	 *
16
+	 * @return void
17
+	 */
18
+	public function boot()
19
+	{
20
+		$this->registerValidator();
21 21
 
22
-        $this->registerRule();
23
-    }
22
+		$this->registerRule();
23
+	}
24 24
 
25
-    /**
26
-     * Register the service provider.
27
-     *
28
-     * @return void
29
-     */
30
-    public function register()
31
-    {
32
-        $this->app->singleton('libphonenumber', function ($app) {
33
-            return PhoneNumberUtil::getInstance();
34
-        });
25
+	/**
26
+	 * Register the service provider.
27
+	 *
28
+	 * @return void
29
+	 */
30
+	public function register()
31
+	{
32
+		$this->app->singleton('libphonenumber', function ($app) {
33
+			return PhoneNumberUtil::getInstance();
34
+		});
35 35
 
36
-        $this->app->alias('libphonenumber', PhoneNumberUtil::class);
37
-    }
36
+		$this->app->alias('libphonenumber', PhoneNumberUtil::class);
37
+	}
38 38
 
39
-    /**
40
-     * Register the "phone" validator.
41
-     */
42
-    protected function registerValidator()
43
-    {
44
-        $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend';
39
+	/**
40
+	 * Register the "phone" validator.
41
+	 */
42
+	protected function registerValidator()
43
+	{
44
+		$extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend';
45 45
 
46
-        $this->app['validator']->{$extend}('phone', Validation\Phone::class . '@validate');
47
-    }
46
+		$this->app['validator']->{$extend}('phone', Validation\Phone::class . '@validate');
47
+	}
48 48
 
49
-    /**
50
-     * Register the "phone" rule macro.
51
-     */
52
-    protected function registerRule()
53
-    {
54
-        if (class_exists('Illuminate\Validation\Rule') && class_uses(Rule::class, Macroable::class)) {
55
-            Rule::macro('phone', function () {
56
-                return new Rules\Phone;
57
-            });
58
-        }
59
-    }
49
+	/**
50
+	 * Register the "phone" rule macro.
51
+	 */
52
+	protected function registerRule()
53
+	{
54
+		if (class_exists('Illuminate\Validation\Rule') && class_uses(Rule::class, Macroable::class)) {
55
+			Rule::macro('phone', function () {
56
+				return new Rules\Phone;
57
+			});
58
+		}
59
+	}
60 60
 
61
-    /**
62
-     * Determine whether we can register a dependent validator.
63
-     *
64
-     * @return bool
65
-     */
66
-    public static function canUseDependentValidation()
67
-    {
68
-        $validator = new ReflectionClass('\Illuminate\Validation\Factory');
61
+	/**
62
+	 * Determine whether we can register a dependent validator.
63
+	 *
64
+	 * @return bool
65
+	 */
66
+	public static function canUseDependentValidation()
67
+	{
68
+		$validator = new ReflectionClass('\Illuminate\Validation\Factory');
69 69
 
70
-        return $validator->hasMethod('extendDependent');
71
-    }
70
+		return $validator->hasMethod('extendDependent');
71
+	}
72 72
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -29,7 +29,7 @@  discard block
 block discarded – undo
29 29
      */
30 30
     public function register()
31 31
     {
32
-        $this->app->singleton('libphonenumber', function ($app) {
32
+        $this->app->singleton('libphonenumber', function($app) {
33 33
             return PhoneNumberUtil::getInstance();
34 34
         });
35 35
 
@@ -43,7 +43,7 @@  discard block
 block discarded – undo
43 43
     {
44 44
         $extend = static::canUseDependentValidation() ? 'extendDependent' : 'extend';
45 45
 
46
-        $this->app['validator']->{$extend}('phone', Validation\Phone::class . '@validate');
46
+        $this->app['validator']->{$extend}('phone', Validation\Phone::class.'@validate');
47 47
     }
48 48
 
49 49
     /**
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
     protected function registerRule()
53 53
     {
54 54
         if (class_exists('Illuminate\Validation\Rule') && class_uses(Rule::class, Macroable::class)) {
55
-            Rule::macro('phone', function () {
55
+            Rule::macro('phone', function() {
56 56
                 return new Rules\Phone;
57 57
             });
58 58
         }
Please login to merge, or discard this patch.
src/PhoneNumber.php 2 patches
Indentation   +352 added lines, -352 removed lines patch added patch discarded remove patch
@@ -18,356 +18,356 @@
 block discarded – undo
18 18
 
19 19
 class PhoneNumber implements Jsonable, JsonSerializable, Serializable
20 20
 {
21
-    use ParsesCountries,
22
-        ParsesFormats,
23
-        ParsesTypes;
24
-
25
-    /**
26
-     * The provided phone number.
27
-     *
28
-     * @var string
29
-     */
30
-    protected $number;
31
-
32
-    /**
33
-     * The provided phone country.
34
-     *
35
-     * @var array
36
-     */
37
-    protected $countries = [];
38
-
39
-    /**
40
-     * The detected phone country.
41
-     *
42
-     * @var string
43
-     */
44
-    protected $country;
45
-
46
-    /**
47
-     * Whether to allow lenient checks (i.e. landline numbers without area codes).
48
-     *
49
-     * @var bool
50
-     */
51
-    protected $lenient = false;
52
-
53
-    /**
54
-     * @var \libphonenumber\PhoneNumberUtil
55
-     */
56
-    protected $lib;
57
-
58
-    /**
59
-     * Phone constructor.
60
-     *
61
-     * @param string $number
62
-     */
63
-    public function __construct($number)
64
-    {
65
-        $this->number = $number;
66
-        $this->lib = PhoneNumberUtil::getInstance();
67
-    }
68
-
69
-    /**
70
-     * Create a phone instance.
71
-     *
72
-     * @param string       $number
73
-     * @param string|array $country
74
-     * @return static
75
-     */
76
-    public static function make($number, $country = [])
77
-    {
78
-        $instance = new static($number);
79
-
80
-        return $instance->ofCountry($country);
81
-    }
82
-
83
-    /**
84
-     * Set the country to which the phone number belongs to.
85
-     *
86
-     * @param string|array $country
87
-     * @return static
88
-     */
89
-    public function ofCountry($country)
90
-    {
91
-        $countries = is_array($country) ? $country : func_get_args();
92
-
93
-        $instance = clone $this;
94
-        $instance->countries = array_unique(
95
-            array_merge($instance->countries, static::parseCountries($countries))
96
-        );
97
-
98
-        return $instance;
99
-    }
100
-
101
-    /**
102
-     * Format the phone number in international format.
103
-     *
104
-     * @return string
105
-     */
106
-    public function formatInternational()
107
-    {
108
-        return $this->format(PhoneNumberFormat::INTERNATIONAL);
109
-    }
110
-
111
-    /**
112
-     * Format the phone number in national format.
113
-     *
114
-     * @return string
115
-     */
116
-    public function formatNational()
117
-    {
118
-        return $this->format(PhoneNumberFormat::NATIONAL);
119
-    }
120
-
121
-    /**
122
-     * Format the phone number in E164 format.
123
-     *
124
-     * @return string
125
-     */
126
-    public function formatE164()
127
-    {
128
-        return $this->format(PhoneNumberFormat::E164);
129
-    }
130
-
131
-    /**
132
-     * Format the phone number in RFC3966 format.
133
-     *
134
-     * @return string
135
-     */
136
-    public function formatRFC3966()
137
-    {
138
-        return $this->format(PhoneNumberFormat::RFC3966);
139
-    }
140
-
141
-    /**
142
-     * Format the phone number in a given format.
143
-     *
144
-     * @param string $format
145
-     * @return string
146
-     * @throws \Propaganistas\LaravelPhone\Exceptions\NumberFormatException
147
-     */
148
-    public function format($format)
149
-    {
150
-        $parsedFormat = static::parseFormat($format);
151
-
152
-        if (is_null($parsedFormat)) {
153
-            throw NumberFormatException::invalid($format);
154
-        }
155
-
156
-        return $this->lib->format(
157
-            $this->getPhoneNumberInstance(),
158
-            $parsedFormat
159
-        );
160
-    }
161
-
162
-    /**
163
-     * Format the phone number in a way that it can be dialled from the provided country.
164
-     *
165
-     * @param string $country
166
-     * @return string
167
-     * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException
168
-     */
169
-    public function formatForCountry($country)
170
-    {
171
-        if (! static::isValidCountryCode($country)) {
172
-            throw CountryCodeException::invalid($country);
173
-        }
174
-
175
-        return $this->lib->formatOutOfCountryCallingNumber(
176
-            $this->getPhoneNumberInstance(),
177
-            $country
178
-        );
179
-    }
180
-
181
-    /**
182
-     * Format the phone number in a way that it can be dialled from the provided country using a cellphone.
183
-     *
184
-     * @param string $country
185
-     * @param bool   $removeFormatting
186
-     * @return string
187
-     * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException
188
-     */
189
-    public function formatForMobileDialingInCountry($country, $removeFormatting = false)
190
-    {
191
-        if (! static::isValidCountryCode($country)) {
192
-            throw CountryCodeException::invalid($country);
193
-        }
194
-
195
-        return $this->lib->formatNumberForMobileDialing(
196
-            $this->getPhoneNumberInstance(),
197
-            $country,
198
-            $removeFormatting
199
-        );
200
-    }
201
-
202
-    /**
203
-     * Get the phone number's country.
204
-     *
205
-     * @return string
206
-     */
207
-    public function getCountry()
208
-    {
209
-        if (! $this->country) {
210
-            $this->country = $this->filterValidCountry($this->countries);
211
-        }
212
-
213
-        return $this->country;
214
-    }
215
-
216
-    /**
217
-     * Filter the provided countries to the one that is valid for the number.
218
-     *
219
-     * @param string|array $countries
220
-     * @return string
221
-     * @throws \Propaganistas\LaravelPhone\Exceptions\NumberParseException
222
-     */
223
-    protected function filterValidCountry($countries)
224
-    {
225
-        $result = Collection::make($countries)
226
-                            ->filter(function ($country) {
227
-                                $instance = $this->lib->parse($this->number, $country);
228
-
229
-                                return $this->lenient
230
-                                    ? $this->lib->isPossibleNumber($instance, $country)
231
-                                    : $this->lib->isValidNumberForRegion($instance, $country);
232
-                            })->first();
233
-
234
-        // If we got a new result, return it.
235
-        if ($result) {
236
-            return $result;
237
-        }
238
-
239
-        // Last resort: try to detect it from an international number.
240
-        if ($this->numberLooksInternational()) {
241
-            $instance = $this->lib->parse($this->number, null);
242
-
243
-            if ($this->lib->isValidNumber($instance)) {
244
-                return $this->lib->getRegionCodeForNumber($instance);
245
-            }
246
-        }
247
-
248
-        throw NumberParseException::countryRequired($this->number);
249
-    }
250
-
251
-    /**
252
-     * Get the phone number's type.
253
-     *
254
-     * @param bool $asConstant
255
-     * @return string|int|null
256
-     */
257
-    public function getType($asConstant = false)
258
-    {
259
-        $type = $this->lib->getNumberType($this->getPhoneNumberInstance());
260
-
261
-        if ($asConstant) {
262
-            return $type;
263
-        }
264
-
265
-        $stringType = Arr::first(static::parseTypesAsStrings($type));
266
-
267
-        return $stringType ? strtolower($stringType) : null;
268
-    }
269
-
270
-    /**
271
-     * Check if the phone number is of (a) given type(s).
272
-     *
273
-     * @param string $type
274
-     * @return bool
275
-     */
276
-    public function isOfType($type)
277
-    {
278
-        $types = static::parseTypes($type);
279
-
280
-        return in_array($this->getType(true), $types, true);
281
-    }
282
-
283
-    /**
284
-     * Get the PhoneNumber instance of the current number.
285
-     *
286
-     * @return \libphonenumber\PhoneNumber
287
-     */
288
-    public function getPhoneNumberInstance()
289
-    {
290
-        return $this->lib->parse($this->number, $this->getCountry());
291
-    }
292
-
293
-    /**
294
-     * Determine whether the phone number seems to be in international format.
295
-     *
296
-     * @return bool
297
-     */
298
-    protected function numberLooksInternational()
299
-    {
300
-        return Str::startsWith($this->number, '+');
301
-    }
302
-
303
-    /**
304
-     * Enable lenient number parsing.
305
-     *
306
-     * @return $this
307
-     */
308
-    public function lenient()
309
-    {
310
-        $this->lenient = true;
311
-
312
-        return $this;
313
-    }
314
-
315
-    /**
316
-     * Convert the phone instance to JSON.
317
-     *
318
-     * @param  int $options
319
-     * @return string
320
-     */
321
-    public function toJson($options = 0)
322
-    {
323
-        return json_encode($this->jsonSerialize(), $options);
324
-    }
325
-
326
-    /**
327
-     * Convert the phone instance into something JSON serializable.
328
-     *
329
-     * @return string
330
-     */
331
-    public function jsonSerialize()
332
-    {
333
-        return $this->formatE164();
334
-    }
335
-
336
-    /**
337
-     * Convert the phone instance into a string representation.
338
-     *
339
-     * @return string
340
-     */
341
-    public function serialize()
342
-    {
343
-        return $this->formatE164();
344
-    }
345
-
346
-    /**
347
-     * Reconstructs the phone instance from a string representation.
348
-     *
349
-     * @param string $serialized
350
-     */
351
-    public function unserialize($serialized)
352
-    {
353
-        $this->lib = PhoneNumberUtil::getInstance();
354
-        $this->number = $serialized;
355
-        $this->country = $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance());
356
-    }
357
-
358
-    /**
359
-     * Convert the phone instance to a formatted number.
360
-     *
361
-     * @return string
362
-     */
363
-    public function __toString()
364
-    {
365
-        // Formatting the phone number could throw an exception, but __toString() doesn't cope well with that.
366
-        // Let's just return the original number in that case.
367
-        try {
368
-            return $this->formatE164();
369
-        } catch (Exception $exception) {
370
-            return $this->number;
371
-        }
372
-    }
21
+	use ParsesCountries,
22
+		ParsesFormats,
23
+		ParsesTypes;
24
+
25
+	/**
26
+	 * The provided phone number.
27
+	 *
28
+	 * @var string
29
+	 */
30
+	protected $number;
31
+
32
+	/**
33
+	 * The provided phone country.
34
+	 *
35
+	 * @var array
36
+	 */
37
+	protected $countries = [];
38
+
39
+	/**
40
+	 * The detected phone country.
41
+	 *
42
+	 * @var string
43
+	 */
44
+	protected $country;
45
+
46
+	/**
47
+	 * Whether to allow lenient checks (i.e. landline numbers without area codes).
48
+	 *
49
+	 * @var bool
50
+	 */
51
+	protected $lenient = false;
52
+
53
+	/**
54
+	 * @var \libphonenumber\PhoneNumberUtil
55
+	 */
56
+	protected $lib;
57
+
58
+	/**
59
+	 * Phone constructor.
60
+	 *
61
+	 * @param string $number
62
+	 */
63
+	public function __construct($number)
64
+	{
65
+		$this->number = $number;
66
+		$this->lib = PhoneNumberUtil::getInstance();
67
+	}
68
+
69
+	/**
70
+	 * Create a phone instance.
71
+	 *
72
+	 * @param string       $number
73
+	 * @param string|array $country
74
+	 * @return static
75
+	 */
76
+	public static function make($number, $country = [])
77
+	{
78
+		$instance = new static($number);
79
+
80
+		return $instance->ofCountry($country);
81
+	}
82
+
83
+	/**
84
+	 * Set the country to which the phone number belongs to.
85
+	 *
86
+	 * @param string|array $country
87
+	 * @return static
88
+	 */
89
+	public function ofCountry($country)
90
+	{
91
+		$countries = is_array($country) ? $country : func_get_args();
92
+
93
+		$instance = clone $this;
94
+		$instance->countries = array_unique(
95
+			array_merge($instance->countries, static::parseCountries($countries))
96
+		);
97
+
98
+		return $instance;
99
+	}
100
+
101
+	/**
102
+	 * Format the phone number in international format.
103
+	 *
104
+	 * @return string
105
+	 */
106
+	public function formatInternational()
107
+	{
108
+		return $this->format(PhoneNumberFormat::INTERNATIONAL);
109
+	}
110
+
111
+	/**
112
+	 * Format the phone number in national format.
113
+	 *
114
+	 * @return string
115
+	 */
116
+	public function formatNational()
117
+	{
118
+		return $this->format(PhoneNumberFormat::NATIONAL);
119
+	}
120
+
121
+	/**
122
+	 * Format the phone number in E164 format.
123
+	 *
124
+	 * @return string
125
+	 */
126
+	public function formatE164()
127
+	{
128
+		return $this->format(PhoneNumberFormat::E164);
129
+	}
130
+
131
+	/**
132
+	 * Format the phone number in RFC3966 format.
133
+	 *
134
+	 * @return string
135
+	 */
136
+	public function formatRFC3966()
137
+	{
138
+		return $this->format(PhoneNumberFormat::RFC3966);
139
+	}
140
+
141
+	/**
142
+	 * Format the phone number in a given format.
143
+	 *
144
+	 * @param string $format
145
+	 * @return string
146
+	 * @throws \Propaganistas\LaravelPhone\Exceptions\NumberFormatException
147
+	 */
148
+	public function format($format)
149
+	{
150
+		$parsedFormat = static::parseFormat($format);
151
+
152
+		if (is_null($parsedFormat)) {
153
+			throw NumberFormatException::invalid($format);
154
+		}
155
+
156
+		return $this->lib->format(
157
+			$this->getPhoneNumberInstance(),
158
+			$parsedFormat
159
+		);
160
+	}
161
+
162
+	/**
163
+	 * Format the phone number in a way that it can be dialled from the provided country.
164
+	 *
165
+	 * @param string $country
166
+	 * @return string
167
+	 * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException
168
+	 */
169
+	public function formatForCountry($country)
170
+	{
171
+		if (! static::isValidCountryCode($country)) {
172
+			throw CountryCodeException::invalid($country);
173
+		}
174
+
175
+		return $this->lib->formatOutOfCountryCallingNumber(
176
+			$this->getPhoneNumberInstance(),
177
+			$country
178
+		);
179
+	}
180
+
181
+	/**
182
+	 * Format the phone number in a way that it can be dialled from the provided country using a cellphone.
183
+	 *
184
+	 * @param string $country
185
+	 * @param bool   $removeFormatting
186
+	 * @return string
187
+	 * @throws \Propaganistas\LaravelPhone\Exceptions\CountryCodeException
188
+	 */
189
+	public function formatForMobileDialingInCountry($country, $removeFormatting = false)
190
+	{
191
+		if (! static::isValidCountryCode($country)) {
192
+			throw CountryCodeException::invalid($country);
193
+		}
194
+
195
+		return $this->lib->formatNumberForMobileDialing(
196
+			$this->getPhoneNumberInstance(),
197
+			$country,
198
+			$removeFormatting
199
+		);
200
+	}
201
+
202
+	/**
203
+	 * Get the phone number's country.
204
+	 *
205
+	 * @return string
206
+	 */
207
+	public function getCountry()
208
+	{
209
+		if (! $this->country) {
210
+			$this->country = $this->filterValidCountry($this->countries);
211
+		}
212
+
213
+		return $this->country;
214
+	}
215
+
216
+	/**
217
+	 * Filter the provided countries to the one that is valid for the number.
218
+	 *
219
+	 * @param string|array $countries
220
+	 * @return string
221
+	 * @throws \Propaganistas\LaravelPhone\Exceptions\NumberParseException
222
+	 */
223
+	protected function filterValidCountry($countries)
224
+	{
225
+		$result = Collection::make($countries)
226
+							->filter(function ($country) {
227
+								$instance = $this->lib->parse($this->number, $country);
228
+
229
+								return $this->lenient
230
+									? $this->lib->isPossibleNumber($instance, $country)
231
+									: $this->lib->isValidNumberForRegion($instance, $country);
232
+							})->first();
233
+
234
+		// If we got a new result, return it.
235
+		if ($result) {
236
+			return $result;
237
+		}
238
+
239
+		// Last resort: try to detect it from an international number.
240
+		if ($this->numberLooksInternational()) {
241
+			$instance = $this->lib->parse($this->number, null);
242
+
243
+			if ($this->lib->isValidNumber($instance)) {
244
+				return $this->lib->getRegionCodeForNumber($instance);
245
+			}
246
+		}
247
+
248
+		throw NumberParseException::countryRequired($this->number);
249
+	}
250
+
251
+	/**
252
+	 * Get the phone number's type.
253
+	 *
254
+	 * @param bool $asConstant
255
+	 * @return string|int|null
256
+	 */
257
+	public function getType($asConstant = false)
258
+	{
259
+		$type = $this->lib->getNumberType($this->getPhoneNumberInstance());
260
+
261
+		if ($asConstant) {
262
+			return $type;
263
+		}
264
+
265
+		$stringType = Arr::first(static::parseTypesAsStrings($type));
266
+
267
+		return $stringType ? strtolower($stringType) : null;
268
+	}
269
+
270
+	/**
271
+	 * Check if the phone number is of (a) given type(s).
272
+	 *
273
+	 * @param string $type
274
+	 * @return bool
275
+	 */
276
+	public function isOfType($type)
277
+	{
278
+		$types = static::parseTypes($type);
279
+
280
+		return in_array($this->getType(true), $types, true);
281
+	}
282
+
283
+	/**
284
+	 * Get the PhoneNumber instance of the current number.
285
+	 *
286
+	 * @return \libphonenumber\PhoneNumber
287
+	 */
288
+	public function getPhoneNumberInstance()
289
+	{
290
+		return $this->lib->parse($this->number, $this->getCountry());
291
+	}
292
+
293
+	/**
294
+	 * Determine whether the phone number seems to be in international format.
295
+	 *
296
+	 * @return bool
297
+	 */
298
+	protected function numberLooksInternational()
299
+	{
300
+		return Str::startsWith($this->number, '+');
301
+	}
302
+
303
+	/**
304
+	 * Enable lenient number parsing.
305
+	 *
306
+	 * @return $this
307
+	 */
308
+	public function lenient()
309
+	{
310
+		$this->lenient = true;
311
+
312
+		return $this;
313
+	}
314
+
315
+	/**
316
+	 * Convert the phone instance to JSON.
317
+	 *
318
+	 * @param  int $options
319
+	 * @return string
320
+	 */
321
+	public function toJson($options = 0)
322
+	{
323
+		return json_encode($this->jsonSerialize(), $options);
324
+	}
325
+
326
+	/**
327
+	 * Convert the phone instance into something JSON serializable.
328
+	 *
329
+	 * @return string
330
+	 */
331
+	public function jsonSerialize()
332
+	{
333
+		return $this->formatE164();
334
+	}
335
+
336
+	/**
337
+	 * Convert the phone instance into a string representation.
338
+	 *
339
+	 * @return string
340
+	 */
341
+	public function serialize()
342
+	{
343
+		return $this->formatE164();
344
+	}
345
+
346
+	/**
347
+	 * Reconstructs the phone instance from a string representation.
348
+	 *
349
+	 * @param string $serialized
350
+	 */
351
+	public function unserialize($serialized)
352
+	{
353
+		$this->lib = PhoneNumberUtil::getInstance();
354
+		$this->number = $serialized;
355
+		$this->country = $this->lib->getRegionCodeForNumber($this->getPhoneNumberInstance());
356
+	}
357
+
358
+	/**
359
+	 * Convert the phone instance to a formatted number.
360
+	 *
361
+	 * @return string
362
+	 */
363
+	public function __toString()
364
+	{
365
+		// Formatting the phone number could throw an exception, but __toString() doesn't cope well with that.
366
+		// Let's just return the original number in that case.
367
+		try {
368
+			return $this->formatE164();
369
+		} catch (Exception $exception) {
370
+			return $this->number;
371
+		}
372
+	}
373 373
 }
374 374
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -168,7 +168,7 @@  discard block
 block discarded – undo
168 168
      */
169 169
     public function formatForCountry($country)
170 170
     {
171
-        if (! static::isValidCountryCode($country)) {
171
+        if (!static::isValidCountryCode($country)) {
172 172
             throw CountryCodeException::invalid($country);
173 173
         }
174 174
 
@@ -188,7 +188,7 @@  discard block
 block discarded – undo
188 188
      */
189 189
     public function formatForMobileDialingInCountry($country, $removeFormatting = false)
190 190
     {
191
-        if (! static::isValidCountryCode($country)) {
191
+        if (!static::isValidCountryCode($country)) {
192 192
             throw CountryCodeException::invalid($country);
193 193
         }
194 194
 
@@ -206,7 +206,7 @@  discard block
 block discarded – undo
206 206
      */
207 207
     public function getCountry()
208 208
     {
209
-        if (! $this->country) {
209
+        if (!$this->country) {
210 210
             $this->country = $this->filterValidCountry($this->countries);
211 211
         }
212 212
 
@@ -223,7 +223,7 @@  discard block
 block discarded – undo
223 223
     protected function filterValidCountry($countries)
224 224
     {
225 225
         $result = Collection::make($countries)
226
-                            ->filter(function ($country) {
226
+                            ->filter(function($country) {
227 227
                                 $instance = $this->lib->parse($this->number, $country);
228 228
 
229 229
                                 return $this->lenient
Please login to merge, or discard this patch.
src/Rules/Phone.php 2 patches
Indentation   +152 added lines, -152 removed lines patch added patch discarded remove patch
@@ -6,156 +6,156 @@
 block discarded – undo
6 6
 
7 7
 class Phone
8 8
 {
9
-    use ParsesCountries,
10
-        ParsesTypes;
11
-
12
-    /**
13
-     * The provided phone countries.
14
-     *
15
-     * @var array
16
-     */
17
-    protected $allowedCountries = [];
18
-
19
-    /**
20
-     * The input field name to check for a country value.
21
-     *
22
-     * @var string
23
-     */
24
-    protected $countryField;
25
-
26
-    /**
27
-     * The provided phone types.
28
-     *
29
-     * @var array
30
-     */
31
-    protected $allowedTypes = [];
32
-
33
-    /**
34
-     * Whether the number's country should be auto-detected.
35
-     *
36
-     * @var bool
37
-     */
38
-    protected $detect = false;
39
-
40
-    /**
41
-     * Whether to allow lenient checks (i.e. landline numbers without area codes).
42
-     *
43
-     * @var bool
44
-     */
45
-    protected $lenient = false;
46
-
47
-    /**
48
-     * Set the phone countries.
49
-     *
50
-     * @param string|array $country
51
-     * @return $this
52
-     */
53
-    public function country($country)
54
-    {
55
-        $countries = is_array($country) ? $country : func_get_args();
56
-
57
-        $this->allowedCountries = array_merge(
58
-            $this->allowedCountries,
59
-            static::parseCountries($countries)
60
-        );
61
-
62
-        return $this;
63
-    }
64
-
65
-    /**
66
-     * Set the country input field.
67
-     *
68
-     * @param string $name
69
-     * @return $this
70
-     */
71
-    public function countryField($name)
72
-    {
73
-        $this->countryField = $name;
74
-
75
-        return $this;
76
-    }
77
-
78
-    /**
79
-     * Set the phone types.
80
-     *
81
-     * @param string|array $type
82
-     * @return $this
83
-     */
84
-    public function type($type)
85
-    {
86
-        $types = is_array($type) ? $type : func_get_args();
87
-
88
-        $this->allowedTypes = array_merge(
89
-            $this->allowedTypes,
90
-            static::parseTypesAsStrings($types)
91
-        );
92
-
93
-        return $this;
94
-    }
95
-
96
-    /**
97
-     * Shortcut method for mobile type restriction.
98
-     *
99
-     * @return $this
100
-     */
101
-    public function mobile()
102
-    {
103
-        $this->type(PhoneNumberType::MOBILE);
104
-
105
-        return $this;
106
-    }
107
-
108
-    /**
109
-     * Shortcut method for fixed line type restriction.
110
-     *
111
-     * @return $this
112
-     */
113
-    public function fixedLine()
114
-    {
115
-        $this->type(PhoneNumberType::FIXED_LINE);
116
-
117
-        return $this;
118
-    }
119
-
120
-    /**
121
-     * Enable automatic country detection.
122
-     *
123
-     * @return $this
124
-     */
125
-    public function detect()
126
-    {
127
-        $this->detect = true;
128
-
129
-        return $this;
130
-    }
131
-
132
-    /**
133
-     * Enable lenient number checking.
134
-     *
135
-     * @return $this
136
-     */
137
-    public function lenient()
138
-    {
139
-        $this->lenient = true;
140
-
141
-        return $this;
142
-    }
143
-
144
-    /**
145
-     * Convert the rule to a validation string.
146
-     *
147
-     * @return string
148
-     */
149
-    public function __toString()
150
-    {
151
-        $parameters = implode(',', array_merge(
152
-            $this->allowedCountries,
153
-            $this->allowedTypes,
154
-            ($this->countryField ? [$this->countryField]: []),
155
-            ($this->detect ? ['AUTO'] : []),
156
-            ($this->lenient ? ['LENIENT'] : [])
157
-        ));
158
-
159
-        return 'phone' . (! empty($parameters) ? ":$parameters" : '');
160
-    }
9
+	use ParsesCountries,
10
+		ParsesTypes;
11
+
12
+	/**
13
+	 * The provided phone countries.
14
+	 *
15
+	 * @var array
16
+	 */
17
+	protected $allowedCountries = [];
18
+
19
+	/**
20
+	 * The input field name to check for a country value.
21
+	 *
22
+	 * @var string
23
+	 */
24
+	protected $countryField;
25
+
26
+	/**
27
+	 * The provided phone types.
28
+	 *
29
+	 * @var array
30
+	 */
31
+	protected $allowedTypes = [];
32
+
33
+	/**
34
+	 * Whether the number's country should be auto-detected.
35
+	 *
36
+	 * @var bool
37
+	 */
38
+	protected $detect = false;
39
+
40
+	/**
41
+	 * Whether to allow lenient checks (i.e. landline numbers without area codes).
42
+	 *
43
+	 * @var bool
44
+	 */
45
+	protected $lenient = false;
46
+
47
+	/**
48
+	 * Set the phone countries.
49
+	 *
50
+	 * @param string|array $country
51
+	 * @return $this
52
+	 */
53
+	public function country($country)
54
+	{
55
+		$countries = is_array($country) ? $country : func_get_args();
56
+
57
+		$this->allowedCountries = array_merge(
58
+			$this->allowedCountries,
59
+			static::parseCountries($countries)
60
+		);
61
+
62
+		return $this;
63
+	}
64
+
65
+	/**
66
+	 * Set the country input field.
67
+	 *
68
+	 * @param string $name
69
+	 * @return $this
70
+	 */
71
+	public function countryField($name)
72
+	{
73
+		$this->countryField = $name;
74
+
75
+		return $this;
76
+	}
77
+
78
+	/**
79
+	 * Set the phone types.
80
+	 *
81
+	 * @param string|array $type
82
+	 * @return $this
83
+	 */
84
+	public function type($type)
85
+	{
86
+		$types = is_array($type) ? $type : func_get_args();
87
+
88
+		$this->allowedTypes = array_merge(
89
+			$this->allowedTypes,
90
+			static::parseTypesAsStrings($types)
91
+		);
92
+
93
+		return $this;
94
+	}
95
+
96
+	/**
97
+	 * Shortcut method for mobile type restriction.
98
+	 *
99
+	 * @return $this
100
+	 */
101
+	public function mobile()
102
+	{
103
+		$this->type(PhoneNumberType::MOBILE);
104
+
105
+		return $this;
106
+	}
107
+
108
+	/**
109
+	 * Shortcut method for fixed line type restriction.
110
+	 *
111
+	 * @return $this
112
+	 */
113
+	public function fixedLine()
114
+	{
115
+		$this->type(PhoneNumberType::FIXED_LINE);
116
+
117
+		return $this;
118
+	}
119
+
120
+	/**
121
+	 * Enable automatic country detection.
122
+	 *
123
+	 * @return $this
124
+	 */
125
+	public function detect()
126
+	{
127
+		$this->detect = true;
128
+
129
+		return $this;
130
+	}
131
+
132
+	/**
133
+	 * Enable lenient number checking.
134
+	 *
135
+	 * @return $this
136
+	 */
137
+	public function lenient()
138
+	{
139
+		$this->lenient = true;
140
+
141
+		return $this;
142
+	}
143
+
144
+	/**
145
+	 * Convert the rule to a validation string.
146
+	 *
147
+	 * @return string
148
+	 */
149
+	public function __toString()
150
+	{
151
+		$parameters = implode(',', array_merge(
152
+			$this->allowedCountries,
153
+			$this->allowedTypes,
154
+			($this->countryField ? [$this->countryField]: []),
155
+			($this->detect ? ['AUTO'] : []),
156
+			($this->lenient ? ['LENIENT'] : [])
157
+		));
158
+
159
+		return 'phone' . (! empty($parameters) ? ":$parameters" : '');
160
+	}
161 161
 }
162 162
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -151,11 +151,11 @@
 block discarded – undo
151 151
         $parameters = implode(',', array_merge(
152 152
             $this->allowedCountries,
153 153
             $this->allowedTypes,
154
-            ($this->countryField ? [$this->countryField]: []),
154
+            ($this->countryField ? [$this->countryField] : []),
155 155
             ($this->detect ? ['AUTO'] : []),
156 156
             ($this->lenient ? ['LENIENT'] : [])
157 157
         ));
158 158
 
159
-        return 'phone' . (! empty($parameters) ? ":$parameters" : '');
159
+        return 'phone'.(!empty($parameters) ? ":$parameters" : '');
160 160
     }
161 161
 }
162 162
\ No newline at end of file
Please login to merge, or discard this patch.