|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Form\Phone; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Form\Child\ChildInterface; |
|
6
|
|
|
use Bdf\Form\Child\Http\HttpFieldPath; |
|
7
|
|
|
use Bdf\Form\ElementInterface; |
|
8
|
|
|
use Bdf\Form\Error\FormError; |
|
9
|
|
|
use Bdf\Form\Leaf\LeafRootElement; |
|
10
|
|
|
use Bdf\Form\RootElementInterface; |
|
11
|
|
|
use Bdf\Form\View\ElementViewInterface; |
|
12
|
|
|
use libphonenumber\PhoneNumber; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Decorate a PhoneElement to handle string phone number instead of instance of PhoneNumber |
|
16
|
|
|
* |
|
17
|
|
|
* @see FormattedPhoneElementBuilder For build the element |
|
18
|
|
|
* @see PhoneElement The decored element |
|
19
|
|
|
*/ |
|
20
|
|
|
class FormattedPhoneElement implements ElementInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var PhoneElement |
|
24
|
|
|
* @readonly |
|
25
|
|
|
*/ |
|
26
|
|
|
private $element; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
* @readonly |
|
31
|
|
|
*/ |
|
32
|
|
|
private $format; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ChildInterface|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private $container; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* FormattedPhoneElement constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param PhoneElement $element |
|
43
|
|
|
* @param \libphonenumber\PhoneNumberFormat::* $format The phone format. Must be one of the constant PhoneNumberFormat::* |
|
|
|
|
|
|
44
|
|
|
*/ |
|
45
|
33 |
|
public function __construct(PhoneElement $element, int $format) |
|
46
|
|
|
{ |
|
47
|
33 |
|
$this->element = $element; |
|
48
|
33 |
|
$this->format = $format; |
|
49
|
33 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
22 |
|
public function submit($data): ElementInterface |
|
55
|
|
|
{ |
|
56
|
22 |
|
$this->element->submit($data); |
|
57
|
|
|
|
|
58
|
22 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function patch($data): ElementInterface |
|
65
|
|
|
{ |
|
66
|
2 |
|
$this->element->patch($data); |
|
67
|
|
|
|
|
68
|
2 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* {@inheritdoc} |
|
73
|
|
|
*/ |
|
74
|
5 |
|
public function import($entity): ElementInterface |
|
75
|
|
|
{ |
|
76
|
5 |
|
$this->element->import($entity !== null ? $this->element->parseValue($entity) : null); |
|
77
|
|
|
|
|
78
|
5 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
20 |
|
public function value(): ?string |
|
85
|
|
|
{ |
|
86
|
20 |
|
$phone = $this->element->value(); |
|
87
|
|
|
|
|
88
|
20 |
|
if (!$phone instanceof PhoneNumber) { |
|
|
|
|
|
|
89
|
4 |
|
return $phone; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
17 |
|
return $this->element->getFormatter()->format($phone, $this->format); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
9 |
|
public function httpValue() |
|
99
|
|
|
{ |
|
100
|
9 |
|
return $this->element->httpValue(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
*/ |
|
106
|
16 |
|
public function valid(): bool |
|
107
|
|
|
{ |
|
108
|
16 |
|
return $this->element->valid(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
17 |
|
public function error(?HttpFieldPath $field = null): FormError |
|
115
|
|
|
{ |
|
116
|
17 |
|
return $this->element->error($field); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
*/ |
|
122
|
3 |
|
public function container(): ?ChildInterface |
|
123
|
|
|
{ |
|
124
|
3 |
|
return $this->container; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritdoc} |
|
129
|
|
|
*/ |
|
130
|
4 |
|
public function setContainer(ChildInterface $container): ElementInterface |
|
131
|
|
|
{ |
|
132
|
4 |
|
$newElement = clone $this; |
|
133
|
|
|
|
|
134
|
4 |
|
$newElement->element = $newElement->element->setContainer($container); |
|
135
|
4 |
|
$newElement->container = $container; |
|
136
|
|
|
|
|
137
|
4 |
|
return $newElement; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
2 |
|
public function root(): RootElementInterface |
|
144
|
|
|
{ |
|
145
|
|
|
// @todo save the root ? |
|
146
|
2 |
|
if (!$this->container) { |
|
147
|
1 |
|
return new LeafRootElement($this); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
return $this->container->parent()->root(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* {@inheritdoc} |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function view(?HttpFieldPath $field = null): ElementViewInterface |
|
157
|
|
|
{ |
|
158
|
1 |
|
return $this->element->view($field); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|