|
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 |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|