Completed
Push — master ( f871c7...194122 )
by Propa
02:21
created

PhoneValidator::assignParameters()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 8.6738
cc 6
eloc 14
nc 6
nop 1
crap 6
1
<?php namespace Propaganistas\LaravelPhone;
2
3
use libphonenumber\NumberParseException;
4
use libphonenumber\PhoneNumberType;
5
use libphonenumber\PhoneNumberUtil;
6
use Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException;
7
use Propaganistas\LaravelPhone\Exceptions\InvalidParameterException;
8
9
class PhoneValidator
10
{
11
12
    /**
13
     * @var \libphonenumber\PhoneNumberUtil
14
     */
15
    protected $lib;
16
17
    /**
18
     * Whether the country should be auto-detected.
19
     *
20
     * @var bool
21
     */
22
    protected $autodetect = false;
23
24
    /**
25
     * Whether to allow lenient checking of numbers (i.e. landline without area codes).
26
     *
27
     * @var bool
28
     */
29
    protected $lenient = false;
30
31
    /**
32
     * Countries to validate against.
33
     *
34
     * @var array
35
     */
36
    protected $countries = [];
37
38
    /**
39
     * Transformed phone number types to validate against.
40
     *
41
     * @var array
42
     */
43
    protected $types = [];
44
45
    /**
46
     * PhoneValidator constructor.
47
     */
48 24
    public function __construct(PhoneNumberUtil $lib)
49
    {
50 24
        $this->lib = $lib;
51 24
    }
52
53
    /**
54
     * Validates a phone number.
55
     *
56
     * @param  string $attribute
57
     * @param  mixed  $value
58
     * @param  array  $parameters
59
     * @param  object $validator
60
     * @return bool
61
     * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
62
     * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
63
     */
64 24
    public function validatePhone($attribute, $value, array $parameters, $validator)
65
    {
66 24
        $this->assignParameters(array_map('strtoupper', $parameters));
67
68 21
        $this->checkCountries($attribute, $validator);
69
70
        // If autodetecting, let's first try without a country.
71
        // Otherwise use provided countries as default.
72 18
        if ($this->autodetect || ($this->lenient && empty($this->countries))) {
73 6
            array_unshift($this->countries, null);
74 4
        }
75
76 18
        foreach ($this->countries as $country) {
77 18
            if ($this->isValidNumber($value, $country)) {
78 18
                return true;
79
            }
80 12
        }
81
82
        // All specified country validations have failed.
83 18
        return false;
84
    }
85
86
    /**
87
     * Parses the supplied validator parameters.
88
     *
89
     * @param array $parameters
90
     * @throws \Propaganistas\LaravelPhone\Exceptions\InvalidParameterException
91
     */
92 24
    protected function assignParameters(array $parameters)
93
    {
94 24
        $types = array();
95
96 24
        foreach ($parameters as $parameter) {
97 18
            if ($this->isPhoneCountry($parameter)) {
98 15
                $this->countries[] = $parameter;
99 17
            } elseif ($this->isPhoneType($parameter)) {
100 9
                $types[] = $parameter;
101 13
            } elseif ($parameter == 'AUTO') {
102 3
                $this->autodetect = true;
103 8
            } elseif ($parameter == 'LENIENT') {
104 3
                $this->lenient = true;
105 2
            } else {
106
                // Force developers to write proper code.
107 8
                throw new InvalidParameterException($parameter);
108
            }
109 16
        }
110
111 21
        $this->types = $this->parseTypes($types);
112
113 21
    }
114
115
    /**
116
     * Checks the detected countries. Overrides countries if a country field is present.
117
     * When using a country field, we should validate to false if country is empty so no exception
118
     * will be thrown.
119
     *
120
     * @param string $attribute
121
     * @param $validator
122
     * @throws \Propaganistas\LaravelPhone\Exceptions\NoValidCountryFoundException
123
     */
124 21
    protected function checkCountries($attribute, $validator)
125
    {
126 21
        $data = $validator->getData();
127 21
        $countryField = $attribute . '_country';
128
129 21
        if (isset($data[$countryField])) {
130 9
            $this->countries = array($data[$countryField]);
131 19
        } elseif (!$this->autodetect && !$this->lenient && empty($this->countries)) {
132 3
            throw new NoValidCountryFoundException;
133
        }
134 18
    }
135
136
    /**
137
     * Performs the actual validation of the phone number.
138
     *
139
     * @param mixed $number
140
     * @param null  $country
141
     * @return bool
142
     */
143 18
    protected function isValidNumber($number, $country = null)
144
    {
145
        try {
146
            // Throws NumberParseException if not parsed correctly against the supplied country.
147
            // If no country was given, tries to discover the country code from the number itself.
148 18
            $phoneNumber = $this->lib->parse($number, $country);
149
150
            // Check if type is allowed.
151 18
            if (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types)) {
152
153
                // Lenient validation.
154 18
                if ($this->lenient) {
155 3
                    return $this->lib->isPossibleNumber($phoneNumber, $country);
156
                }
157
158
                // For automatic detection, the number should have a country code.
159 18
                if ($phoneNumber->hasCountryCode()) {
160
161
                    // Automatic detection:
162 18
                    if ($this->autodetect) {
163
                        // Validate if the international phone number is valid for its contained country.
164 3
                        return $this->lib->isValidNumber($phoneNumber);
165
                    }
166
167
                    // Validate number against the specified country.
168 15
                    return $this->lib->isValidNumberForRegion($phoneNumber, $country);
169
                }
170
            }
171
172 10
        } catch (NumberParseException $e) {
173
            // Proceed to default validation error.
174
        }
175
176 12
        return false;
177
    }
178
179
    /**
180
     * Parses the supplied phone number types.
181
     *
182
     * @param array $types
183
     * @return array
184
     */
185
    protected function parseTypes(array $types)
186
    {
187
        // Transform types to their namespaced class constant.
188 21
        array_walk($types, function(&$type) {
189 6
            $type = constant('\libphonenumber\PhoneNumberType::' . $type);
190 21
        });
191
192
        // Add in the unsure number type if applicable.
193 21
        if (array_intersect([PhoneNumberType::FIXED_LINE, PhoneNumberType::MOBILE], $types)) {
194 6
            $types[] = PhoneNumberType::FIXED_LINE_OR_MOBILE;
195 4
        }
196
197 21
        return $types;
198
    }
199
200
    /**
201
     * Checks if the supplied string is a valid country code using some arbitrary country validation.
202
     * If using a package based on umpirsky/country-list, invalidate the option 'ZZ => Unknown or invalid region'.
203
     *
204
     * @param  string $country
205
     * @return bool
206
     */
207 18
    public function isPhoneCountry($country)
208
    {
209 18
        return (strlen($country) === 2 && ctype_alpha($country) && ctype_upper($country) && $country != 'ZZ');
210
    }
211
212
    /**
213
     * Checks if the supplied string is a valid phone number type.
214
     *
215
     * @param  string $type
216
     * @return bool
217
     */
218 15
    public function isPhoneType($type)
219
    {
220
        // Legacy support.
221 15
        $type = ($type == 'LANDLINE' ? 'FIXED_LINE' : $type);
222
223 15
        return defined('\libphonenumber\PhoneNumberType::' . $type);
224
    }
225
226
}
227