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

PhoneNumberToStringTransformer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 17
c 1
b 0
f 1
dl 0
loc 58
ccs 15
cts 18
cp 0.8333
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A transformToHttp() 0 13 3
A transformFromHttp() 0 13 4
1
<?php
2
3
4
namespace Bdf\Form\Phone\Transformer;
5
6
use Bdf\Form\ElementInterface;
7
use Bdf\Form\Phone\PhoneElement;
8
use Bdf\Form\Transformer\TransformerInterface;
9
use libphonenumber\PhoneNumber;
10
use libphonenumber\PhoneNumberFormat;
11
use libphonenumber\PhoneNumberUtil;
12
13
/**
14
 * Transformer PhoneNumber instance to string with a format
15
 */
16
final class PhoneNumberToStringTransformer implements TransformerInterface
17
{
18
    /**
19
     * @var PhoneNumberFormat::*
20
     */
21
    private $format;
22
23
    /**
24
     * @var PhoneNumberUtil|null
25
     */
26
    private $formatter;
27
28
    /**
29
     * PhoneNumberToStringTransformer constructor.
30
     *
31
     * @param PhoneNumberFormat::* $format
0 ignored issues
show
Documentation Bug introduced by
The doc comment $format at position 0 could not be parsed: Unknown type name '$format' at position 0 in $format.
Loading history...
32
     * @param PhoneNumberUtil|null $formatter
33
     */
34 8
    public function __construct(int $format = PhoneNumberFormat::E164, ?PhoneNumberUtil $formatter = null)
35
    {
36 8
        $this->format = $format;
0 ignored issues
show
Documentation Bug introduced by
It seems like $format of type integer is incompatible with the declared type libphonenumber\PhoneNumberFormat of property $format.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37 8
        $this->formatter = $formatter;
38 8
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 7
    public function transformToHttp($value, ElementInterface $input): ?PhoneNumber
44
    {
45 7
        if ($value === null) {
46 1
            return null;
47
        }
48
49 6
        if ($input instanceof PhoneElement) {
50 6
            return $input->parseValue($value);
51
        }
52
53
        $formatter = $this->formatter ?? PhoneNumberUtil::getInstance();
54
55
        return $formatter->parse($value, null, null, true);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 6
    public function transformFromHttp($value, ElementInterface $input): ?string
62
    {
63 6
        if (!$value instanceof PhoneNumber) {
64
            return null;
65
        }
66
67 6
        $formatter = $this->formatter ?? ($input instanceof PhoneElement ? $input->getFormatter() : PhoneNumberUtil::getInstance());
68
69 6
        if (!$formatter->isValidNumber($value)) {
70 1
            return $value->getRawInput();
71
        }
72
73 5
        return $formatter->format($value, $this->format);
0 ignored issues
show
Bug introduced by
$this->format of type libphonenumber\PhoneNumberFormat is incompatible with the type integer expected by parameter $numberFormat of libphonenumber\PhoneNumberUtil::format(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        return $formatter->format($value, /** @scrutinizer ignore-type */ $this->format);
Loading history...
74
    }
75
}
76