summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/html/src/FormDecorator/DivDecorator.php
blob: 4935f0108b70e15933e99c938976eb2a55ef2b86 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

namespace ipl\Html\FormDecorator;

use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Contract\FormElement;
use ipl\Html\Contract\FormElementDecorator;
use ipl\Html\Contract\FormSubmitElement;
use ipl\Html\FormElement\FieldsetElement;
use ipl\Html\FormElement\HiddenElement;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
use ipl\Html\Text;

/**
 * Form element decorator based on div elements
 */
class DivDecorator extends BaseHtmlElement implements FormElementDecorator
{
    /** @var string CSS class to use for submit elements */
    public const SUBMIT_ELEMENT_CLASS = 'form-control';

    /** @var string CSS class to use for all input elements */
    public const INPUT_ELEMENT_CLASS = 'form-element';

    /** @var string CSS class to use for form descriptions */
    public const DESCRIPTION_CLASS = 'form-element-description';

    /** @var string CSS class to use for form errors */
    public const ERROR_CLASS = 'form-element-errors';

    /** @var string CSS class to set on the decorator if the element has errors */
    public const ERROR_HINT_CLASS = 'has-error';

    /** @var FormElement The decorated form element */
    protected $formElement;

    protected $tag = 'div';

    public function decorate(FormElement $formElement)
    {
        if ($formElement instanceof HiddenElement) {
            return;
        }

        $decorator = clone $this;

        /**
         * Wrapper logic can be overridden to propagate the decorator.
         * So here we make sure that a yet unbound decorator is passed.
         *
         * {@see FieldsetElement::setWrapper()}
         */
        $formElement->prependWrapper($decorator);

        $decorator->formElement = $formElement;

        $classes = [static::INPUT_ELEMENT_CLASS];
        if ($formElement instanceof FormSubmitElement) {
            $classes[] = static::SUBMIT_ELEMENT_CLASS;
        }

        $decorator->getAttributes()->add('class', $classes);
    }

    protected function assembleDescription()
    {
        $description = $this->formElement->getDescription();

        if ($description !== null) {
            $descriptionId = null;
            if ($this->formElement->getAttributes()->has('id')) {
                $descriptionId = 'desc_' . $this->formElement->getAttributes()->get('id')->getValue();
                $this->formElement->getAttributes()->set('aria-describedby', $descriptionId);
            }

            return Html::tag('p', ['id' => $descriptionId, 'class' => static::DESCRIPTION_CLASS], $description);
        }

        return null;
    }

    protected function assembleElement()
    {
        if ($this->formElement->isRequired()) {
            $this->formElement->getAttributes()->set('aria-required', 'true');
        }

        return $this->formElement->ensureAssembled();
    }

    protected function assembleErrors()
    {
        $errors = new HtmlElement('ul', Attributes::create(['class' => static::ERROR_CLASS]));

        foreach ($this->formElement->getMessages() as $message) {
            $errors->addHtml(
                new HtmlElement('li', Attributes::create(['class' => static::ERROR_CLASS]), Text::create($message))
            );
        }

        if (! $errors->isEmpty()) {
            return $errors;
        }

        return null;
    }

    protected function assembleLabel()
    {
        $label = $this->formElement->getLabel();

        if ($label !== null) {
            if ($this->formElement instanceof FieldsetElement) {
                return new HtmlElement('legend', null, Text::create($label));
            } else {
                $attributes = null;
                if ($this->formElement->getAttributes()->has('id')) {
                    $attributes = new Attributes(['for' => $this->formElement->getAttributes()->get('id')->getValue()]);
                }

                return Html::tag('label', $attributes, $label);
            }
        }

        return null;
    }

    protected function assemble()
    {
        if ($this->formElement->hasBeenValidated() && ! $this->formElement->isValid()) {
            $this->getAttributes()->add('class', static::ERROR_HINT_CLASS);
        }

        if ($this->formElement instanceof FieldsetElement) {
            $element = $this->assembleElement();
            $element->prependHtml(...Html::wantHtmlList([
                $this->assembleLabel(),
                $this->assembleDescription()
            ]));

            $this->addHtml(...Html::wantHtmlList([
                $element,
                $this->assembleErrors()
            ]));
        } else {
            $this->addHtml(...Html::wantHtmlList([
                $this->assembleLabel(),
                $this->assembleElement(),
                $this->assembleDescription(),
                $this->assembleErrors()
            ]));
        }
    }
}