Passed
Push — master ( 9d312b...da6698 )
by Vincent
04:40
created

PhoneNumberToStringTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 13
c 1
b 0
f 1
dl 0
loc 50
ccs 11
cts 14
cp 0.7856
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transformToHttp() 0 9 2
A transformFromHttp() 0 9 3
A __construct() 0 4 1
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 6
    public function __construct(int $format = PhoneNumberFormat::E164, ?PhoneNumberUtil $formatter = null)
35
    {
36 6
        $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 6
        $this->formatter = $formatter;
38 6
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 6
    public function transformToHttp($value, ElementInterface $input): ?PhoneNumber
44
    {
45 6
        if ($input instanceof PhoneElement) {
46 6
            return $input->parseValue($value);
47
        }
48
49
        $formatter = $this->formatter ?? PhoneNumberUtil::getInstance();
50
51
        return $formatter->parse($value, null, null, true);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 5
    public function transformFromHttp($value, ElementInterface $input): ?string
58
    {
59 5
        if (!$value instanceof PhoneNumber) {
60
            return null;
61
        }
62
63 5
        $formatter = $this->formatter ?? ($input instanceof PhoneElement ? $input->getFormatter() : PhoneNumberUtil::getInstance());
64
65 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

65
        return $formatter->format($value, /** @scrutinizer ignore-type */ $this->format);
Loading history...
66
    }
67
}
68