Completed
Push — master ( 0d2867...0c4b8c )
by Propa
02:09
created

PhoneValidator::assignParameters()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

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