|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Form\Phone; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Form\Child\ChildBuilder; |
|
6
|
|
|
use Bdf\Form\ElementBuilderInterface; |
|
7
|
|
|
use Bdf\Form\Phone\Transformer\PhoneNumberToStringTransformer; |
|
8
|
|
|
use Bdf\Form\Registry\RegistryInterface; |
|
9
|
|
|
use libphonenumber\PhoneNumberFormat; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @extends ChildBuilder<PhoneElementBuilder> |
|
13
|
|
|
*/ |
|
14
|
|
|
class PhoneChildBuilder extends ChildBuilder |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var PhoneNumberFormat::*|null |
|
18
|
|
|
*/ |
|
19
|
|
|
private $saveFormat; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var bool |
|
23
|
|
|
*/ |
|
24
|
|
|
private $formatIfInvalid = false; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
* |
|
29
|
|
|
* @param PhoneElementBuilder $elementBuilder |
|
30
|
|
|
*/ |
|
31
|
13 |
|
public function __construct(string $name, ElementBuilderInterface $elementBuilder, RegistryInterface $registry = null) |
|
32
|
|
|
{ |
|
33
|
13 |
|
parent::__construct($name, $elementBuilder, $registry); |
|
34
|
|
|
|
|
35
|
13 |
|
$this->addTransformerProvider([$this, 'provideModelTransformer']); |
|
36
|
13 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Format the phone number even if it's invalid |
|
40
|
|
|
* If not, the raw input value will be used instead of the formatted one |
|
41
|
|
|
* |
|
42
|
|
|
* Note: Works only if `saveAsString()` is called |
|
43
|
|
|
* |
|
44
|
|
|
* @param bool $formatIfInvalid |
|
45
|
|
|
* |
|
46
|
|
|
* @return $this |
|
47
|
|
|
* |
|
48
|
|
|
* @see PhoneChildBuilder::saveAsString() To enable string formating when filling the entity |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function formatIfInvalid(bool $formatIfInvalid = true): self |
|
51
|
|
|
{ |
|
52
|
1 |
|
$this->formatIfInvalid = $formatIfInvalid; |
|
53
|
|
|
|
|
54
|
1 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* The model value of the input will be transformer to a formatted string |
|
59
|
|
|
* |
|
60
|
|
|
* <code> |
|
61
|
|
|
* // The entity : the phone is a simple string |
|
62
|
|
|
* class MyEntity { |
|
63
|
|
|
* public string $phone; |
|
64
|
|
|
* } |
|
65
|
|
|
* |
|
66
|
|
|
* // Build the element |
|
67
|
|
|
* $builder->phone('phone')->saveAsString()->getter()->setter(); |
|
68
|
|
|
* |
|
69
|
|
|
* $form->import(MyEntity::get($id)); |
|
70
|
|
|
* $form['phone']->element()->value(); // Value is an instance of PhoneNumber |
|
71
|
|
|
* |
|
72
|
|
|
* $entity = $form->value(); |
|
73
|
|
|
* $entity->phone; // phone is a string |
|
74
|
|
|
* </code> |
|
75
|
|
|
* |
|
76
|
|
|
* @param PhoneNumberFormat::*|null $format The phone number format. Must be one of the constant of PhoneNumberFormat. Set null to disable |
|
|
|
|
|
|
77
|
|
|
* |
|
78
|
|
|
* @return $this |
|
79
|
|
|
* |
|
80
|
|
|
* @see PhoneNumberToStringTransformer |
|
81
|
|
|
*/ |
|
82
|
6 |
|
public function saveAsString(?int $format = PhoneNumberFormat::E164): self |
|
83
|
|
|
{ |
|
84
|
6 |
|
$this->saveFormat = $format; |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
6 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return PhoneNumberToStringTransformer[] |
|
91
|
|
|
*/ |
|
92
|
12 |
|
protected function provideModelTransformer(): array |
|
93
|
|
|
{ |
|
94
|
12 |
|
if ($this->saveFormat === null) { |
|
95
|
7 |
|
return []; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
5 |
|
return [new PhoneNumberToStringTransformer($this->saveFormat, $this->formatIfInvalid)]; |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|