summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/html/src/FormDecorator/DdDtDecorator.php
blob: 9cfec205a7fa1f89faaac215c52657a38f42afb1 (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
<?php

namespace ipl\Html\FormDecorator;

use ipl\Html\BaseHtmlElement;
use ipl\Html\FormElement\BaseFormElement;
use ipl\Html\Html;
use ipl\Html\ValidHtml;

class DdDtDecorator extends BaseHtmlElement implements DecoratorInterface
{
    protected $tag = 'dl';

    protected $dt;

    protected $dd;

    /** @var BaseFormElement */
    protected $wrappedElement;

    protected $ready = false;

    /**
     * @param BaseFormElement $element
     * @return static
     */
    public function decorate(BaseFormElement $element)
    {
        // TODO: ignore hidden?
        $newWrapper = clone($this);
        $newWrapper->wrappedElement = $element;
        $element->prependWrapper($newWrapper);

        return $newWrapper;
    }

    protected function renderLabel()
    {
        if ($this->wrappedElement instanceof BaseFormElement) {
            $label = $this->wrappedElement->getLabel();
            if ($label) {
                return Html::tag('label', null, $label);
            }
        }

        return null;
    }

    public function getAttributes()
    {
        $attributes = parent::getAttributes();

        if ($this->wrappedElement->hasBeenValidated() && ! $this->wrappedElement->isValid()) {
            $classes = $attributes->get('class')->getValue();
            if (
                empty($classes)
                || (is_array($classes) && ! in_array('errors', $classes))
                || (is_string($classes) && $classes !== 'errors')
            ) {
                $attributes->add('class', 'errors');
            }
        }

        return $attributes;
    }

    protected function renderDescription()
    {
        if ($this->wrappedElement instanceof BaseFormElement) {
            $description = $this->wrappedElement->getDescription();
            if ($description) {
                return Html::tag('p', ['class' => 'description'], $description);
            }
        }

        return null;
    }

    protected function renderErrors()
    {
        if ($this->wrappedElement instanceof BaseFormElement) {
            $errors = [];
            foreach ($this->wrappedElement->getMessages() as $message) {
                $errors[] = Html::tag('p', ['class' => 'error'], $message);
            }

            if (! empty($errors)) {
                return $errors;
            }
        }

        return null;
    }

    public function addHtml(ValidHtml ...$content)
    {
        // TODO: is this required?
        if (! in_array($this->wrappedElement, $content, true)) {
            parent::addHtml(...$content);
        }

        return $this;
    }

    protected function assemble()
    {
        $this->addHtml($this->dt(), $this->dd());
        $this->ready = true;
    }

    protected function dt()
    {
        if ($this->dt === null) {
            $this->dt = Html::tag('dt', null, $this->renderLabel());
        }

        return $this->dt;
    }

    /**
     * @return \ipl\Html\HtmlElement
     */
    protected function dd()
    {
        if ($this->dd === null) {
            $this->dd = Html::tag('dd', null, [
                $this->wrappedElement,
                $this->renderErrors(),
                $this->renderDescription()
            ]);
        }

        return $this->dd;
    }

    public function __destruct()
    {
        $this->wrapper = null;
    }
}