summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/web/src/FormElement/ScheduleElement/FieldsRadio.php
blob: 31b77c3434066fe63da8c14ab26ab07927392f7f (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
<?php

namespace ipl\Web\FormElement\ScheduleElement;

use ipl\Html\Attributes;
use ipl\Html\FormElement\InputElement;
use ipl\Html\FormElement\RadioElement;
use ipl\Html\HtmlElement;
use ipl\Web\FormElement\ScheduleElement\Common\FieldsProtector;

class FieldsRadio extends RadioElement
{
    use FieldsProtector;

    protected function assemble()
    {
        $listItems = HtmlElement::create('ul', ['class' => ['schedule-element-fields', 'single-fields']]);
        foreach ($this->options as $option) {
            $radio = (new InputElement($this->getValueOfNameAttribute()))
                ->setValue($option->getValue())
                ->setType($this->type);

            $radio->setAttributes(clone $this->getAttributes());

            $htmlId = $this->protectId($option->getValue());
            $radio->getAttributes()
                ->set('id', $htmlId)
                ->registerAttributeCallback('checked', function () use ($option) {
                    return (string) $this->getValue() === (string) $option->getValue();
                })
                ->registerAttributeCallback('required', [$this, 'getRequiredAttribute'])
                ->registerAttributeCallback('disabled', function () use ($option) {
                    return $this->getAttributes()->get('disabled')->getValue() || $option->isDisabled();
                });

            $listItem = HtmlElement::create('li');
            $listItem->addHtml(
                $radio,
                HtmlElement::create('label', [
                    'for'      => $htmlId,
                    'class'    => $option->getLabelCssClass(),
                    'tabindex' => -1
                ], $option->getLabel())
            );
            $listItems->addHtml($listItem);
        }

        $this->addHtml($listItems);
    }

    protected function registerAttributeCallbacks(Attributes $attributes)
    {
        parent::registerAttributeCallbacks($attributes);

        $attributes
            ->registerAttributeCallback('protector', null, [$this, 'setIdProtector']);
    }
}