transformFromHttp()   A
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 6
c 1
b 0
f 1
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.2222
cc 6
nc 5
nop 2
crap 6
1
<?php
2
3
4
namespace Bdf\Form\Phone\Transformer;
5
6
use Attribute;
7
use Bdf\Form\ElementInterface;
8
use Bdf\Form\Phone\PhoneElement;
9
use Bdf\Form\Transformer\TransformerInterface;
10
use libphonenumber\PhoneNumber;
11
use libphonenumber\PhoneNumberFormat;
12
use libphonenumber\PhoneNumberUtil;
13
14
/**
15
 * Transformer PhoneNumber instance to string with a format
16
 */
17
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
18
final class PhoneNumberToStringTransformer implements TransformerInterface
19
{
20
    /**
21
     * @var PhoneNumberFormat::*
22
     */
23
    private $format;
24
25
    /**
26
     * @var bool
27
     */
28
    private $formatIfInvalid;
29
30
    /**
31
     * @var PhoneNumberUtil|null
32
     */
33
    private $formatter;
34
35
    /**
36
     * PhoneNumberToStringTransformer constructor.
37
     *
38
     * @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...
39
     * @param bool $formatIfInvalid
40
     * @param PhoneNumberUtil|null $formatter
41
     */
42 12
    public function __construct(int $format = PhoneNumberFormat::E164, bool $formatIfInvalid = false, ?PhoneNumberUtil $formatter = null)
43
    {
44 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...
45 12
        $this->formatIfInvalid = $formatIfInvalid;
46 12
        $this->formatter = $formatter;
47 12
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 7
    public function transformToHttp($value, ElementInterface $input): ?PhoneNumber
53
    {
54 7
        if ($value === null) {
55 1
            return null;
56
        }
57
58 6
        if ($input instanceof PhoneElement) {
59 6
            return $input->parseValue($value);
60
        }
61
62
        $formatter = $this->formatter ?? PhoneNumberUtil::getInstance();
63
64
        return $formatter->parse($value, null, null, true);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 10
    public function transformFromHttp($value, ElementInterface $input): ?string
71
    {
72 10
        if (!$value instanceof PhoneNumber) {
73 1
            return null;
74
        }
75
76 9
        $formatter = $this->formatter ?? ($input instanceof PhoneElement ? $input->getFormatter() : PhoneNumberUtil::getInstance());
77
78 9
        if ((!$this->formatIfInvalid && !$formatter->isValidNumber($value)) || !$value->getNationalNumber()) {
79 3
            return $value->getRawInput();
80
        }
81
82 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

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