Completed
Push — master ( afb9b7...05cc39 )
by Basil
02:14
created

PhoneNumberValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 65
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B validateAttribute() 0 27 7
1
<?php
2
3
namespace luya\validators;
4
5
use libphonenumber\NumberParseException;
6
use libphonenumber\PhoneNumberFormat;
7
use libphonenumber\PhoneNumberType;
8
use libphonenumber\PhoneNumberUtil;
9
use Yii;
10
use yii\validators\Validator;
11
12
/**
13
 * Phone Number Validator.
14
 * 
15
 * Validates a given phone number, and converts into standardized format by default. See {{PhoneNumberValidator::$autoFormat}}
16
 * 
17
 * @see https://github.com/giggsey/libphonenumber-for-php This library is used to parse, format and validate.
18
 * @author Basil Suter  <[email protected]>
19
 * @since 1.0.25
20
 */
21
class PhoneNumberValidator extends Validator
22
{
23
    /**
24
     * @var string If a phone number does not contain the country prefix (+41 f.e), define a default country format which then defines the 
25
     * country prefix.
26
     * 
27
     * ```php
28
     * 'country' => 'CH',
29
     * ```
30
     */
31
    public $country;
32
33
    /**
34
     * @var boolean Whether the phone number value should be standardized with formating function and write back to the model. This is usefull
35
     * in order to have all phone numbers save in the correct format in the database.
36
     */
37
    public $autoFormat = true;
38
39
    /**
40
     * @var inter The format which should be taken to auto format the phone number value.
41
     */
42
    public $autoFormatFormat = PhoneNumberFormat::E164;
43
44
    /**
45
     * @var integer If enabled, the validator will check the type of number. This can be usefull to test for mobile phone numbers. 
46
     * 
47
     * An example to check for mobile phone numbers:
48
     * 
49
     * ```php
50
     * 'type' => \libphonenumber\PhoneNumberType::MOBILE
51
     * ```
52
     */
53
    public $type;
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function validateAttribute($model, $attribute)
59
    {
60
        $phoneUtil = PhoneNumberUtil::getInstance();
61
62
        $value = $model->{$attribute};
63
64
        try {
65
            $number = $phoneUtil->parse($value, $this->country);
66
67
            if (!$number || !$phoneUtil->isValidNumber($number)) {
68
                return $this->addError($model, $attribute, Yii::t('luya', 'Invalid phone number.'));
69
            }
70
71
            if ($this->type !== null && ($phoneUtil->getNumberType($number) !== $this->type)) {
72
                $typeName = PhoneNumberType::values()[$this->type];
73
                return $this->addError($model, $attribute, Yii::t('luya', 'The phone number does not match the required type {name}.', ['name' => $typeName]));
74
            }
75
76
            // refactor the phone number
77
            if ($this->autoFormat) {
78
                $model->{$attribute} = $phoneUtil->format($number, $this->autoFormatFormat);
79
            }
80
81
        } catch (NumberParseException $exception) {
82
            $this->addError($model, $attribute, Yii::t('luya', 'Invalid phone number, ensure it starts with the correct country code.'));
83
        }
84
    }
85
}