Completed
Push — master ( a291e8...e660b5 )
by Propa
03:46
created
src/Validation/Phone.php 2 patches
Indentation   +129 added lines, -129 removed lines patch added patch discarded remove patch
@@ -11,133 +11,133 @@
 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
-        // Discover if an input field was provided. If not, guess the field's name.
105
-        $inputField = Collection::make($parameters)
106
-                                ->intersect(array_keys(Arr::dot($data)))
107
-                                ->first() ?: "${attribute}_country";
108
-
109
-        // Attempt to retrieve the field's value.
110
-        if ($inputCountry = Arr::get($data, $inputField)) {
111
-
112
-            if (static::isValidType($inputField)) {
113
-                throw InvalidParameterException::ambiguous($inputField);
114
-            }
115
-
116
-            $parameters[] = $inputCountry;
117
-        }
118
-
119
-        $countries = static::parseCountries($parameters);
120
-        $types = static::parseTypes($parameters);
121
-
122
-        // Force developers to write proper code.
123
-        // Since the static parsers return a validated array with preserved keys, we can safely diff against the keys.
124
-        // Unfortunately we can't use $collection->diffKeys() as it's not available yet in earlier 5.* versions.
125
-        $leftovers = array_diff_key($parameters, $types, $countries);
126
-        $leftovers = array_diff($leftovers, ['AUTO', 'LENIENT', $inputField]);
127
-
128
-        // Invalid country field values should just validate to false, so exclude from leftovers.
129
-        $leftovers = array_diff($leftovers, [$inputCountry]);
130
-
131
-        if (! empty($leftovers)) {
132
-            throw InvalidParameterException::parameters($leftovers);
133
-        }
134
-
135
-        return [
136
-            $countries,
137
-            $types,
138
-            in_array('AUTO', $parameters),
139
-            in_array('LENIENT', $parameters),
140
-            $inputField,
141
-        ];
142
-    }
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
+		// Discover if an input field was provided. If not, guess the field's name.
105
+		$inputField = Collection::make($parameters)
106
+								->intersect(array_keys(Arr::dot($data)))
107
+								->first() ?: "${attribute}_country";
108
+
109
+		// Attempt to retrieve the field's value.
110
+		if ($inputCountry = Arr::get($data, $inputField)) {
111
+
112
+			if (static::isValidType($inputField)) {
113
+				throw InvalidParameterException::ambiguous($inputField);
114
+			}
115
+
116
+			$parameters[] = $inputCountry;
117
+		}
118
+
119
+		$countries = static::parseCountries($parameters);
120
+		$types = static::parseTypes($parameters);
121
+
122
+		// Force developers to write proper code.
123
+		// Since the static parsers return a validated array with preserved keys, we can safely diff against the keys.
124
+		// Unfortunately we can't use $collection->diffKeys() as it's not available yet in earlier 5.* versions.
125
+		$leftovers = array_diff_key($parameters, $types, $countries);
126
+		$leftovers = array_diff($leftovers, ['AUTO', 'LENIENT', $inputField]);
127
+
128
+		// Invalid country field values should just validate to false, so exclude from leftovers.
129
+		$leftovers = array_diff($leftovers, [$inputCountry]);
130
+
131
+		if (! empty($leftovers)) {
132
+			throw InvalidParameterException::parameters($leftovers);
133
+		}
134
+
135
+		return [
136
+			$countries,
137
+			$types,
138
+			in_array('AUTO', $parameters),
139
+			in_array('LENIENT', $parameters),
140
+			$inputField,
141
+		];
142
+	}
143 143
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
                 $phoneNumber = PhoneNumber::make($value, $country);
61 61
 
62 62
                 // Type validation.
63
-                if (! empty($types) && ! $phoneNumber->isOfType($types)) {
63
+                if (!empty($types) && !$phoneNumber->isOfType($types)) {
64 64
                     continue;
65 65
                 }
66 66
 
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
         // Invalid country field values should just validate to false, so exclude from leftovers.
129 129
         $leftovers = array_diff($leftovers, [$inputCountry]);
130 130
 
131
-        if (! empty($leftovers)) {
131
+        if (!empty($leftovers)) {
132 132
             throw InvalidParameterException::parameters($leftovers);
133 133
         }
134 134
 
Please login to merge, or discard this patch.
src/Traits/ParsesTypes.php 2 patches
Indentation   +63 added lines, -63 removed lines patch added patch discarded remove patch
@@ -7,74 +7,74 @@
 block discarded – undo
7 7
 
8 8
 trait ParsesTypes
9 9
 {
10
-    /**
11
-     * Array of available phone types.
12
-     *
13
-     * @var array
14
-     */
15
-    protected static $types;
10
+	/**
11
+	 * Array of available phone types.
12
+	 *
13
+	 * @var array
14
+	 */
15
+	protected static $types;
16 16
 
17
-    /**
18
-     * Determine whether the given type is valid.
19
-     *
20
-     * @param string $type
21
-     * @return bool
22
-     */
23
-    public static function isValidType($type)
24
-    {
25
-        return ! empty(static::parseTypes($type));
26
-    }
17
+	/**
18
+	 * Determine whether the given type is valid.
19
+	 *
20
+	 * @param string $type
21
+	 * @return bool
22
+	 */
23
+	public static function isValidType($type)
24
+	{
25
+		return ! empty(static::parseTypes($type));
26
+	}
27 27
 
28
-    /**
29
-     * Parse a phone type into constant's value.
30
-     *
31
-     * @param string|array $types
32
-     * @return array
33
-     */
34
-    protected static function parseTypes($types)
35
-    {
36
-        static::loadTypes();
28
+	/**
29
+	 * Parse a phone type into constant's value.
30
+	 *
31
+	 * @param string|array $types
32
+	 * @return array
33
+	 */
34
+	protected static function parseTypes($types)
35
+	{
36
+		static::loadTypes();
37 37
 
38
-        return Collection::make(is_array($types) ? $types : func_get_args())
39
-                         ->map(function ($type) {
40
-                             // If the type equals a constant's value, just return it.
41
-                             if (is_numeric($type) && in_array($type, static::$types)) {
42
-                                 return (int) $type;
43
-                             }
38
+		return Collection::make(is_array($types) ? $types : func_get_args())
39
+						 ->map(function ($type) {
40
+							 // If the type equals a constant's value, just return it.
41
+							 if (is_numeric($type) && in_array($type, static::$types)) {
42
+								 return (int) $type;
43
+							 }
44 44
 
45
-                             // Otherwise we'll assume the type is the constant's name.
46
-                             return Arr::get(static::$types, strtoupper($type));
47
-                         })
48
-                         ->reject(function ($value) {
49
-                             return is_null($value) || $value === false;
50
-                         })->toArray();
51
-    }
45
+							 // Otherwise we'll assume the type is the constant's name.
46
+							 return Arr::get(static::$types, strtoupper($type));
47
+						 })
48
+						 ->reject(function ($value) {
49
+							 return is_null($value) || $value === false;
50
+						 })->toArray();
51
+	}
52 52
 
53
-    /**
54
-     * Parse a phone type into its string representation.
55
-     *
56
-     * @param string|array $types
57
-     * @return array
58
-     */
59
-    protected static function parseTypesAsStrings($types)
60
-    {
61
-        static::loadTypes();
53
+	/**
54
+	 * Parse a phone type into its string representation.
55
+	 *
56
+	 * @param string|array $types
57
+	 * @return array
58
+	 */
59
+	protected static function parseTypesAsStrings($types)
60
+	{
61
+		static::loadTypes();
62 62
 
63
-        return array_keys(
64
-            array_intersect(
65
-                static::$types,
66
-                static::parseTypes($types)
67
-            )
68
-        );
69
-    }
63
+		return array_keys(
64
+			array_intersect(
65
+				static::$types,
66
+				static::parseTypes($types)
67
+			)
68
+		);
69
+	}
70 70
 
71
-    /**
72
-     * Load all available formats once.
73
-     */
74
-    private static function loadTypes()
75
-    {
76
-        if (! static::$types) {
77
-            static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants();
78
-        }
79
-    }
71
+	/**
72
+	 * Load all available formats once.
73
+	 */
74
+	private static function loadTypes()
75
+	{
76
+		if (! static::$types) {
77
+			static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants();
78
+		}
79
+	}
80 80
 }
81 81
\ 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
@@ -22,7 +22,7 @@  discard block
 block discarded – undo
22 22
      */
23 23
     public static function isValidType($type)
24 24
     {
25
-        return ! empty(static::parseTypes($type));
25
+        return !empty(static::parseTypes($type));
26 26
     }
27 27
 
28 28
     /**
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
         static::loadTypes();
37 37
 
38 38
         return Collection::make(is_array($types) ? $types : func_get_args())
39
-                         ->map(function ($type) {
39
+                         ->map(function($type) {
40 40
                              // If the type equals a constant's value, just return it.
41 41
                              if (is_numeric($type) && in_array($type, static::$types)) {
42 42
                                  return (int) $type;
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
                              // Otherwise we'll assume the type is the constant's name.
46 46
                              return Arr::get(static::$types, strtoupper($type));
47 47
                          })
48
-                         ->reject(function ($value) {
48
+                         ->reject(function($value) {
49 49
                              return is_null($value) || $value === false;
50 50
                          })->toArray();
51 51
     }
@@ -73,7 +73,7 @@  discard block
 block discarded – undo
73 73
      */
74 74
     private static function loadTypes()
75 75
     {
76
-        if (! static::$types) {
76
+        if (!static::$types) {
77 77
             static::$types = with(new ReflectionClass(PhoneNumberType::class))->getConstants();
78 78
         }
79 79
     }
Please login to merge, or discard this patch.