Passed
Push — master ( edc244...0d4dfe )
by Vincent
05:40
created

PhoneNumberToStringTransformer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
crap 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 bool
25
     */
26
    private $formatIfInvalid;
27
28
    /**
29
     * @var PhoneNumberUtil|null
30
     */
31
    private $formatter;
32
33
    /**
34
     * PhoneNumberToStringTransformer constructor.
35
     *
36
     * @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...
37
     * @param bool $formatIfInvalid
38
     * @param PhoneNumberUtil|null $formatter
39
     */
40 12
    public function __construct(int $format = PhoneNumberFormat::E164, bool $formatIfInvalid = false, ?PhoneNumberUtil $formatter = null)
41
    {
42 12
        $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...
43 12
        $this->formatIfInvalid = $formatIfInvalid;
44 12
        $this->formatter = $formatter;
45 12
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 7
    public function transformToHttp($value, ElementInterface $input): ?PhoneNumber
51
    {
52 7
        if ($value === null) {
53 1
            return null;
54
        }
55
56 6
        if ($input instanceof PhoneElement) {
57 6
            return $input->parseValue($value);
58
        }
59
60
        $formatter = $this->formatter ?? PhoneNumberUtil::getInstance();
61
62
        return $formatter->parse($value, null, null, true);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 10
    public function transformFromHttp($value, ElementInterface $input): ?string
69
    {
70 10
        if (!$value instanceof PhoneNumber) {
71 1
            return null;
72
        }
73
74 9
        $formatter = $this->formatter ?? ($input instanceof PhoneElement ? $input->getFormatter() : PhoneNumberUtil::getInstance());
75
76 9
        if ((!$this->formatIfInvalid && !$formatter->isValidNumber($value)) || !$value->getNationalNumber()) {
77 3
            return $value->getRawInput();
78
        }
79
80 7
        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

80
        return $formatter->format($value, /** @scrutinizer ignore-type */ $this->format);
Loading history...
81
    }
82
}
83