Passed
Push — master ( 7a99c7...b0c988 )
by Vincent
04:45
created

NotEmptyPhoneNumberValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 31
ccs 9
cts 9
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 11 3
1
<?php
2
3
namespace Bdf\Form\Phone;
4
5
use libphonenumber\PhoneNumber;
6
use libphonenumber\PhoneNumberUtil;
7
use Symfony\Component\Validator\Constraint;
8
use Symfony\Component\Validator\Constraints\NotBlankValidator;
9
10
/**
11
 * NotBlank implementation for PhoneNumber value
12
 */
13
class NotEmptyPhoneNumberValidator extends NotBlankValidator
14
{
15
    /**
16
     * @var PhoneNumberUtil
17
     */
18
    private $formatter;
19
20
21
    /**
22
     * PhoneNumberValidator constructor.
23
     * @param PhoneNumberUtil|null $formatter
24
     */
25 7
    public function __construct(?PhoneNumberUtil $formatter = null)
26
    {
27 7
        $this->formatter = $formatter ?? PhoneNumberUtil::getInstance();
28 7
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 7
    public function validate($value, Constraint $constraint)
34
    {
35 7
        if ($value instanceof PhoneNumber) {
36 3
            if ($value->hasRawInput()) {
37 3
                $value = $value->getRawInput();
38
            } else {
39 2
                $value = $value->getNationalNumber();
40
            }
41
        }
42
43 7
        parent::validate($value, $constraint);
44 7
    }
45
}
46