|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Form\Leaf\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Form\ElementInterface; |
|
6
|
|
|
use Bdf\Form\Leaf\StringElementBuilder; |
|
7
|
|
|
use Bdf\Form\Registry\RegistryInterface; |
|
8
|
|
|
use Bdf\Form\Transformer\TransformerInterface; |
|
9
|
|
|
use Bdf\Form\Validator\ValueValidatorInterface; |
|
10
|
|
|
use Symfony\Component\Validator\Constraints\Url; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Provide URL constraint builder for a StringElementBuilder |
|
14
|
|
|
* |
|
15
|
|
|
* <code> |
|
16
|
|
|
* $builder->url('home')->protocols('https'); |
|
17
|
|
|
* </code> |
|
18
|
|
|
* |
|
19
|
|
|
* @see UrlElement the built element |
|
20
|
|
|
*/ |
|
21
|
|
|
class UrlElementBuilder extends StringElementBuilder |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var bool |
|
25
|
|
|
*/ |
|
26
|
|
|
private $useConstraint = true; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array{message?:string,protocols?:string[],relativeProtocol?:bool,normalizer?:callable(string):string} |
|
|
|
|
|
|
30
|
|
|
*/ |
|
31
|
|
|
private $constraintOptions = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* UrlElementBuilder constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param RegistryInterface|null $registry |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(?RegistryInterface $registry = null) |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct($registry); |
|
41
|
|
|
|
|
42
|
|
|
$this->addConstraintsProvider([$this, 'createUrlConstraint']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Define authorized protocols list |
|
47
|
|
|
* |
|
48
|
|
|
* <code> |
|
49
|
|
|
* $builder->url('home')->protocols('http', 'https'); |
|
50
|
|
|
* </code> |
|
51
|
|
|
* |
|
52
|
|
|
* @param string ...$protocols |
|
53
|
|
|
* |
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
|
|
public function protocols(string ...$protocols): self |
|
57
|
|
|
{ |
|
58
|
|
|
$this->constraintOptions['protocols'] = $protocols; |
|
59
|
|
|
|
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Enable relative protocol handling |
|
65
|
|
|
* URL without protocol like '//example.com' are accepted |
|
66
|
|
|
* |
|
67
|
|
|
* @param bool $enable |
|
68
|
|
|
* |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
|
|
public function relativeProtocol(bool $enable = true): self |
|
72
|
|
|
{ |
|
73
|
|
|
$this->constraintOptions['relativeProtocol'] = $enable; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Define the invalid URL error message |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $message |
|
82
|
|
|
* |
|
83
|
|
|
* @return $this |
|
84
|
|
|
*/ |
|
85
|
|
|
public function errorMessage(string $message): self |
|
86
|
|
|
{ |
|
87
|
|
|
$this->constraintOptions['message'] = $message; |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Define the normalizer for the URL value |
|
94
|
|
|
* |
|
95
|
|
|
* <code> |
|
96
|
|
|
* // Add normalizer handle relative URL |
|
97
|
|
|
* $builder->email('contact')->normalizer(function (string $value) { |
|
98
|
|
|
* if (strpos($value, '://') === false) { |
|
99
|
|
|
* $value = 'http://example.com/'.$value; |
|
100
|
|
|
* } |
|
101
|
|
|
* |
|
102
|
|
|
* return $value; |
|
103
|
|
|
* }); |
|
104
|
|
|
* </code> |
|
105
|
|
|
* |
|
106
|
|
|
* @param callable(string):string $normalizer |
|
107
|
|
|
* |
|
108
|
|
|
* @return $this |
|
109
|
|
|
*/ |
|
110
|
|
|
public function normalizer(callable $normalizer): self |
|
111
|
|
|
{ |
|
112
|
|
|
$this->constraintOptions['normalizer'] = $normalizer; |
|
113
|
|
|
|
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Disable the url verification constraint |
|
119
|
|
|
* |
|
120
|
|
|
* @return $this |
|
121
|
|
|
*/ |
|
122
|
|
|
public function disableConstraint(): self |
|
123
|
|
|
{ |
|
124
|
|
|
$this->useConstraint = false; |
|
125
|
|
|
|
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Define the email validation constraint options |
|
131
|
|
|
* |
|
132
|
|
|
* <code> |
|
133
|
|
|
* $builder->email('contact')->useConstraint(['protocols' => ['ssh', 'sftp'], 'message' => 'my error']); |
|
134
|
|
|
* </code> |
|
135
|
|
|
* |
|
136
|
|
|
* @param array{message?:string,protocols?:string[],relativeProtocol?:bool,normalizer?:callable(string):string} $options |
|
|
|
|
|
|
137
|
|
|
* |
|
138
|
|
|
* @return $this |
|
139
|
|
|
* |
|
140
|
|
|
* @see Url for list of options |
|
141
|
|
|
*/ |
|
142
|
|
|
public function useConstraint(array $options = []): self |
|
143
|
|
|
{ |
|
144
|
|
|
$this->useConstraint = true; |
|
145
|
|
|
$this->constraintOptions = $options; |
|
146
|
|
|
|
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return \Symfony\Component\Validator\Constraint[] |
|
152
|
|
|
*/ |
|
153
|
|
|
protected function createUrlConstraint(): array |
|
154
|
|
|
{ |
|
155
|
|
|
if (!$this->useConstraint) { |
|
156
|
|
|
return []; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return [new Url($this->constraintOptions)]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function createElement(ValueValidatorInterface $validator, TransformerInterface $transformer): ElementInterface |
|
166
|
|
|
{ |
|
167
|
|
|
return new UrlElement($validator, $transformer, $this->getChoices()); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|