Completed
Push — master ( 4c4396...b59422 )
by Propa
04:59
created
src/Rules/Phone.php 2 patches
Indentation   +143 added lines, -143 removed lines patch added patch discarded remove patch
@@ -6,147 +6,147 @@
 block discarded – undo
6 6
 
7 7
 class Phone
8 8
 {
9
-    /**
10
-     * The provided phone countries.
11
-     *
12
-     * @var array
13
-     */
14
-    protected $countries = [];
15
-
16
-    /**
17
-     * The input field name to check for a country value.
18
-     *
19
-     * @var string
20
-     */
21
-    protected $countryField;
22
-
23
-    /**
24
-     * The provided phone types.
25
-     *
26
-     * @var array
27
-     */
28
-    protected $types = [];
29
-
30
-    /**
31
-     * Whether the number's country should be auto-detected.
32
-     *
33
-     * @var bool
34
-     */
35
-    protected $detect = false;
36
-
37
-    /**
38
-     * Whether to allow lenient checks (i.e. landline numbers without area codes).
39
-     *
40
-     * @var bool
41
-     */
42
-    protected $lenient = false;
43
-
44
-    /**
45
-     * Set the phone countries.
46
-     *
47
-     * @param string|array $country
48
-     * @return $this
49
-     */
50
-    public function country($country)
51
-    {
52
-        $countries = is_array($country) ? $country : func_get_args();
53
-
54
-        $this->countries = array_merge($this->countries, $countries);
55
-
56
-        return $this;
57
-    }
58
-
59
-    /**
60
-     * Set the country input field.
61
-     *
62
-     * @param string $name
63
-     * @return $this
64
-     */
65
-    public function countryField($name)
66
-    {
67
-        $this->countryField = $name;
68
-
69
-        return $this;
70
-    }
71
-
72
-    /**
73
-     * Set the phone types.
74
-     *
75
-     * @param string|array $type
76
-     * @return $this
77
-     */
78
-    public function type($type)
79
-    {
80
-        $types = is_array($type) ? $type : func_get_args();
81
-
82
-        $this->types = array_merge($this->types, $types);
83
-
84
-        return $this;
85
-    }
86
-
87
-    /**
88
-     * Shortcut method for mobile type restriction.
89
-     *
90
-     * @return $this
91
-     */
92
-    public function mobile()
93
-    {
94
-        $this->type(PhoneNumberType::MOBILE);
95
-
96
-        return $this;
97
-    }
98
-
99
-    /**
100
-     * Shortcut method for fixed line type restriction.
101
-     *
102
-     * @return $this
103
-     */
104
-    public function fixedLine()
105
-    {
106
-        $this->type(PhoneNumberType::FIXED_LINE);
107
-
108
-        return $this;
109
-    }
110
-
111
-    /**
112
-     * Enable automatic country detection.
113
-     *
114
-     * @return $this
115
-     */
116
-    public function detect()
117
-    {
118
-        $this->detect = true;
119
-
120
-        return $this;
121
-    }
122
-
123
-    /**
124
-     * Enable lenient number checking.
125
-     *
126
-     * @return $this
127
-     */
128
-    public function lenient()
129
-    {
130
-        $this->lenient = true;
131
-
132
-        return $this;
133
-    }
134
-
135
-    /**
136
-     * Convert the rule to a validation string.
137
-     *
138
-     * @return string
139
-     */
140
-    public function __toString()
141
-    {
142
-        $parameters = implode(',', array_merge(
143
-            $this->countries,
144
-            $this->types,
145
-            ($this->countryField ? [$this->countryField]: []),
146
-            ($this->detect ? ['AUTO'] : []),
147
-            ($this->lenient ? ['LENIENT'] : [])
148
-        ));
149
-
150
-        return 'phone' . (! empty($parameters) ? ":$parameters" : '');
151
-    }
9
+	/**
10
+	 * The provided phone countries.
11
+	 *
12
+	 * @var array
13
+	 */
14
+	protected $countries = [];
15
+
16
+	/**
17
+	 * The input field name to check for a country value.
18
+	 *
19
+	 * @var string
20
+	 */
21
+	protected $countryField;
22
+
23
+	/**
24
+	 * The provided phone types.
25
+	 *
26
+	 * @var array
27
+	 */
28
+	protected $types = [];
29
+
30
+	/**
31
+	 * Whether the number's country should be auto-detected.
32
+	 *
33
+	 * @var bool
34
+	 */
35
+	protected $detect = false;
36
+
37
+	/**
38
+	 * Whether to allow lenient checks (i.e. landline numbers without area codes).
39
+	 *
40
+	 * @var bool
41
+	 */
42
+	protected $lenient = false;
43
+
44
+	/**
45
+	 * Set the phone countries.
46
+	 *
47
+	 * @param string|array $country
48
+	 * @return $this
49
+	 */
50
+	public function country($country)
51
+	{
52
+		$countries = is_array($country) ? $country : func_get_args();
53
+
54
+		$this->countries = array_merge($this->countries, $countries);
55
+
56
+		return $this;
57
+	}
58
+
59
+	/**
60
+	 * Set the country input field.
61
+	 *
62
+	 * @param string $name
63
+	 * @return $this
64
+	 */
65
+	public function countryField($name)
66
+	{
67
+		$this->countryField = $name;
68
+
69
+		return $this;
70
+	}
71
+
72
+	/**
73
+	 * Set the phone types.
74
+	 *
75
+	 * @param string|array $type
76
+	 * @return $this
77
+	 */
78
+	public function type($type)
79
+	{
80
+		$types = is_array($type) ? $type : func_get_args();
81
+
82
+		$this->types = array_merge($this->types, $types);
83
+
84
+		return $this;
85
+	}
86
+
87
+	/**
88
+	 * Shortcut method for mobile type restriction.
89
+	 *
90
+	 * @return $this
91
+	 */
92
+	public function mobile()
93
+	{
94
+		$this->type(PhoneNumberType::MOBILE);
95
+
96
+		return $this;
97
+	}
98
+
99
+	/**
100
+	 * Shortcut method for fixed line type restriction.
101
+	 *
102
+	 * @return $this
103
+	 */
104
+	public function fixedLine()
105
+	{
106
+		$this->type(PhoneNumberType::FIXED_LINE);
107
+
108
+		return $this;
109
+	}
110
+
111
+	/**
112
+	 * Enable automatic country detection.
113
+	 *
114
+	 * @return $this
115
+	 */
116
+	public function detect()
117
+	{
118
+		$this->detect = true;
119
+
120
+		return $this;
121
+	}
122
+
123
+	/**
124
+	 * Enable lenient number checking.
125
+	 *
126
+	 * @return $this
127
+	 */
128
+	public function lenient()
129
+	{
130
+		$this->lenient = true;
131
+
132
+		return $this;
133
+	}
134
+
135
+	/**
136
+	 * Convert the rule to a validation string.
137
+	 *
138
+	 * @return string
139
+	 */
140
+	public function __toString()
141
+	{
142
+		$parameters = implode(',', array_merge(
143
+			$this->countries,
144
+			$this->types,
145
+			($this->countryField ? [$this->countryField]: []),
146
+			($this->detect ? ['AUTO'] : []),
147
+			($this->lenient ? ['LENIENT'] : [])
148
+		));
149
+
150
+		return 'phone' . (! empty($parameters) ? ":$parameters" : '');
151
+	}
152 152
 }
153 153
\ 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
@@ -142,11 +142,11 @@
 block discarded – undo
142 142
         $parameters = implode(',', array_merge(
143 143
             $this->countries,
144 144
             $this->types,
145
-            ($this->countryField ? [$this->countryField]: []),
145
+            ($this->countryField ? [$this->countryField] : []),
146 146
             ($this->detect ? ['AUTO'] : []),
147 147
             ($this->lenient ? ['LENIENT'] : [])
148 148
         ));
149 149
 
150
-        return 'phone' . (! empty($parameters) ? ":$parameters" : '');
150
+        return 'phone'.(!empty($parameters) ? ":$parameters" : '');
151 151
     }
152 152
 }
153 153
\ No newline at end of file
Please login to merge, or discard this patch.
src/Validation/Phone.php 2 patches
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
 
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
         $leftovers = array_diff_key($parameters, $types, $inputCountry ? [] : $countries);
119 119
         $leftovers = array_diff($leftovers, ['AUTO', 'LENIENT', $inputField]);
120 120
 
121
-        if (! empty($leftovers)) {
121
+        if (!empty($leftovers)) {
122 122
             throw InvalidParameterException::parameters($leftovers);
123 123
         }
124 124
 
Please login to merge, or discard this patch.
Indentation   +119 added lines, -119 removed lines patch added patch discarded remove patch
@@ -11,123 +11,123 @@
 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. If no field is present, this will be null.
110
-        $inputCountry = Arr::get($data, $inputField);
111
-
112
-        $countries = static::parseCountries($inputCountry ? [$inputCountry] : $parameters);
113
-        $types = static::parseTypes($parameters);
114
-
115
-        // Force developers to write proper code.
116
-        // Since the static parsers return a validated array with preserved keys, we can safely diff against the keys.
117
-        // Unfortunately we can't use $collection->diffKeys() as it's not available yet in earlier 5.* versions.
118
-        $leftovers = array_diff_key($parameters, $types, $inputCountry ? [] : $countries);
119
-        $leftovers = array_diff($leftovers, ['AUTO', 'LENIENT', $inputField]);
120
-
121
-        if (! empty($leftovers)) {
122
-            throw InvalidParameterException::parameters($leftovers);
123
-        }
124
-
125
-        return [
126
-            $countries,
127
-            $types,
128
-            in_array('AUTO', $parameters),
129
-            in_array('LENIENT', $parameters),
130
-            $inputField,
131
-        ];
132
-    }
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. If no field is present, this will be null.
110
+		$inputCountry = Arr::get($data, $inputField);
111
+
112
+		$countries = static::parseCountries($inputCountry ? [$inputCountry] : $parameters);
113
+		$types = static::parseTypes($parameters);
114
+
115
+		// Force developers to write proper code.
116
+		// Since the static parsers return a validated array with preserved keys, we can safely diff against the keys.
117
+		// Unfortunately we can't use $collection->diffKeys() as it's not available yet in earlier 5.* versions.
118
+		$leftovers = array_diff_key($parameters, $types, $inputCountry ? [] : $countries);
119
+		$leftovers = array_diff($leftovers, ['AUTO', 'LENIENT', $inputField]);
120
+
121
+		if (! empty($leftovers)) {
122
+			throw InvalidParameterException::parameters($leftovers);
123
+		}
124
+
125
+		return [
126
+			$countries,
127
+			$types,
128
+			in_array('AUTO', $parameters),
129
+			in_array('LENIENT', $parameters),
130
+			$inputField,
131
+		];
132
+	}
133 133
 }
Please login to merge, or discard this patch.