|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Form\Leaf\View; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Form\Csrf\CsrfElement; |
|
6
|
|
|
use Bdf\Form\Leaf\Helper\EmailElement; |
|
7
|
|
|
use Bdf\Form\Leaf\IntegerElement; |
|
8
|
|
|
use Bdf\Form\Phone\PhoneElement; |
|
9
|
|
|
use Bdf\Form\View\FieldViewInterface; |
|
10
|
|
|
use Bdf\Form\View\FieldViewRendererInterface; |
|
11
|
|
|
use Bdf\Form\View\HtmlRenderer; |
|
12
|
|
|
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual; |
|
13
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
|
14
|
|
|
use Symfony\Component\Validator\Constraints\LessThanOrEqual; |
|
15
|
|
|
use Symfony\Component\Validator\Constraints\PositiveOrZero; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Default render for @see SimpleElementView |
|
19
|
|
|
*/ |
|
20
|
|
|
final class SimpleFieldHtmlRenderer implements FieldViewRendererInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var SimpleFieldHtmlRenderer|null |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $instance; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Map constraint class name to mapped attributes in form : |
|
29
|
|
|
* $constraintMapping[$constraintClassName][$constraintAttributeName] = $htmlAttribute |
|
30
|
|
|
* |
|
31
|
|
|
* @var string[][] |
|
32
|
|
|
*/ |
|
33
|
|
|
private $constraintMapping = [ |
|
34
|
|
|
Length::class => ['min' => 'minlength', 'max' => 'maxlength'], |
|
35
|
|
|
LessThanOrEqual::class => ['value' => 'max'], |
|
36
|
|
|
GreaterThanOrEqual::class => ['value' => 'min'], |
|
37
|
|
|
PositiveOrZero::class => ['value' => 'min'], |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Map element type to html5 input type |
|
42
|
|
|
* |
|
43
|
|
|
* @var string[] |
|
44
|
|
|
*/ |
|
45
|
|
|
private $typesMapping = [ |
|
46
|
|
|
IntegerElement::class => 'number', |
|
47
|
|
|
PhoneElement::class => 'tel', |
|
48
|
|
|
CsrfElement::class => 'hidden', |
|
49
|
|
|
EmailElement::class => 'email', |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
55 |
|
public function render(FieldViewInterface $view, array $attributes): string |
|
56
|
|
|
{ |
|
57
|
55 |
|
if (!isset($attributes['type'])) { |
|
58
|
55 |
|
$attributes['type'] = $this->typesMapping[$view->type()] ?? 'text'; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
55 |
|
$attributes['name'] = $view->name(); |
|
62
|
55 |
|
$attributes['value'] = $view->value(); |
|
63
|
55 |
|
$attributes['required'] = $view->required(); |
|
64
|
55 |
|
$attributes += $this->constraintsToAttributes($view->constraints()); |
|
65
|
|
|
|
|
66
|
55 |
|
return HtmlRenderer::element('input', $attributes); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
55 |
|
private function constraintsToAttributes(array $constraints): array |
|
70
|
|
|
{ |
|
71
|
55 |
|
$attributes = []; |
|
72
|
|
|
|
|
73
|
55 |
|
foreach ($constraints as $type => $values) { |
|
74
|
25 |
|
if (!isset($this->constraintMapping[$type])) { |
|
75
|
13 |
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
19 |
|
foreach ($this->constraintMapping[$type] as $from => $to) { |
|
79
|
19 |
|
if (isset($values[$from]) && $values[$from] !== '') { |
|
80
|
19 |
|
$attributes[$to] = $values[$from]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
55 |
|
return $attributes; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get the renderer instance |
|
90
|
|
|
* |
|
91
|
|
|
* @return self |
|
92
|
|
|
*/ |
|
93
|
41 |
|
public static function instance(): self |
|
94
|
|
|
{ |
|
95
|
41 |
|
if (self::$instance) { |
|
96
|
40 |
|
return self::$instance; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
return self::$instance = new self; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|