Passed
Push — master ( 779d84...3d5507 )
by Vincent
05:31
created

FormattedPhoneElementBuilder::getElementBuilder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bdf\Form\Phone;
4
5
use Bdf\Form\Aggregate\FormBuilder;
6
use Bdf\Form\ElementBuilderInterface;
7
use Bdf\Form\ElementInterface;
8
use Bdf\Form\Util\DelegateElementBuilderTrait;
9
use libphonenumber\PhoneNumberFormat;
10
11
/**
12
 * Build a formatted phone element
13
 * Delegates all builder methods to the PhoneElementBuilder
14
 *
15
 * Note: Because the built element simply decorate a PhoneElement, all validation and transformation are performed by this
16
 *       element, so, it'll take as parameter a PhoneNumber object instead of a string
17
 *
18
 * <code>
19
 * $builder->formattedPhone('contact')
20
 *     ->regionInput('address/country') // All PhoneElementBuilder methods are available
21
 *     ->format(PhoneNumberFormat::INTERNATIONAL) // Define the phone format for the model (i.e. PHP) value
22
 *     ->satisfy(function (PhoneNumber $phone) { // For adding constraints (or transformers), use PhoneNumber as paramerer
23
 *         // Validation
24
 *     })
25
 * ;
26
 * </code>
27
 *
28
 * @see FormattedPhoneElement The built element
29
 * @see FormBuilder::formattedPhone() The create the builder
30
 *
31
 * @implements ElementBuilderInterface<FormattedPhoneElement>
32
 * @mixin PhoneElementBuilder
33
 */
34
class FormattedPhoneElementBuilder implements ElementBuilderInterface
35
{
36
    use DelegateElementBuilderTrait;
37
38
    /**
39
     * @var PhoneElementBuilder
40
     */
41
    private $builder;
42
43
    /**
44
     * @var PhoneNumberFormat::*
45
     */
46
    private $format = PhoneNumberFormat::E164;
47
48
    /**
49
     * @var string|null
50
     */
51
    private $value;
52
53
54
    /**
55
     * FormattedPhoneElementBuilder constructor.
56
     *
57
     * @param PhoneElementBuilder $builder
58
     */
59 15
    public function __construct(PhoneElementBuilder $builder)
60
    {
61 15
        $this->builder = $builder;
62 15
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function value($value)
68
    {
69 1
        $this->value = $value;
70
71 1
        return $this;
72
    }
73
74
    /**
75
     * Define the model (i.e. PHP) value format
76
     * The format is one of the PhoneNumberFormat::* constants
77
     *
78
     * <code>
79
     * $builder->formattedPhone('phone')->format(PhoneNumberFormat::INTERNATIONAL); // To handle format "+XX X XX XX XX XX"
80
     * $builder->formattedPhone('phone')->format(PhoneNumberFormat::NATIONAL); // To handle format "0X XX XX XX XX"
81
     * </code>
82
     *
83
     * @param PhoneNumberFormat::* $format The 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...
84
     *
85
     * @return $this
86
     *
87
     * @see PhoneNumberFormat::E164
88
     * @see PhoneNumberFormat::INTERNATIONAL
89
     * @see PhoneNumberFormat::NATIONAL
90
     * @see PhoneNumberFormat::RFC3966
91
     */
92 1
    public function format(int $format): self
93
    {
94 1
        $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...
95
96 1
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 14
    public function buildElement(): ElementInterface
103
    {
104 14
        $element = new FormattedPhoneElement($this->builder->buildElement(), $this->format);
0 ignored issues
show
Bug introduced by
$this->format of type libphonenumber\PhoneNumberFormat is incompatible with the type integer expected by parameter $format of Bdf\Form\Phone\Formatted...eElement::__construct(). ( Ignorable by Annotation )

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

104
        $element = new FormattedPhoneElement($this->builder->buildElement(), /** @scrutinizer ignore-type */ $this->format);
Loading history...
105
106 14
        if ($this->value !== null) {
107 1
            $element->import($this->value);
108
        }
109
110 14
        return $element;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 10
    protected function getElementBuilder(): ElementBuilderInterface
117
    {
118 10
        return $this->builder;
119
    }
120
}
121