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

NotEmptyPhoneNumberValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 2
crap 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