-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDescriptionDecorator.php
More file actions
110 lines (92 loc) · 3.28 KB
/
DescriptionDecorator.php
File metadata and controls
110 lines (92 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace ipl\Html\FormDecoration;
use ipl\Html\Attributes;
use ipl\Html\Contract\DecorationResult;
use ipl\Html\Contract\DecoratorOptions;
use ipl\Html\Contract\DecoratorOptionsInterface;
use ipl\Html\Contract\FormElement;
use ipl\Html\Contract\FormElementDecoration;
use ipl\Html\Contract\HtmlElementInterface;
use ipl\Html\FormElement\RadioElement;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\Html\ValidHtml;
/**
* Decorates the description of the form element
*/
class DescriptionDecorator implements FormElementDecoration, DecoratorOptionsInterface
{
use DecoratorOptions;
/** @var string|string[] CSS classes to apply */
protected string|array $class = 'form-element-description';
/** @var callable A callback used to generate a unique ID based on the element name */
private $uniqueName = 'uniqid';
/**
* Get the css class(es)
*
* @return string|string[]
*/
public function getClass(): string|array
{
return $this->class;
}
/**
* Set the css class(es)
*
* @param string|string[] $class
*
* @return $this
*/
public function setClass(string|array $class): static
{
$this->class = $class;
return $this;
}
public function decorateFormElement(DecorationResult $result, FormElement $formElement): void
{
$isHtmlElement = $formElement instanceof HtmlElementInterface;
if ($formElement->getDescription() === null || ($isHtmlElement && $formElement->getTag() === 'fieldset')) {
return;
}
$elementDescription = $this->getElementDescription($formElement);
if ($isHtmlElement) {
if ($formElement->getAttributes()->has('id')) {
$elementId = $formElement->getAttributes()->get('id')->getValue();
} else {
$elementId = call_user_func($this->uniqueName, $formElement->getName());
// RadioElement applies all its attributes to each of its options, so we cannot set a fallback
// id attribute here.
if (! $formElement instanceof RadioElement) {
$formElement->getAttributes()->set('id', $elementId);
}
}
$descriptionId = 'desc_' . $elementId;
$formElement->getAttributes()->add('aria-describedby', $descriptionId);
$elementDescription->getAttributes()->set('id', $descriptionId);
}
$elementDescription->getAttributes()->add('class', $this->getClass());
$result->append($elementDescription);
}
/**
* Get the element description as HTML
*
* @param FormElement $formElement
*
* @return HtmlElementInterface & ValidHtml
*/
protected function getElementDescription(FormElement $formElement): HtmlElementInterface & ValidHtml
{
return new HtmlElement('p', content: new Text($formElement->getDescription()));
}
protected function registerAttributeCallbacks(Attributes $attributes): void
{
$attributes->registerAttributeCallback('class', null, $this->setClass(...));
$attributes->registerAttributeCallback(
'uniqueName',
null,
function ($callback) {
$this->uniqueName = $callback;
}
);
}
}