summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/html/src/FormElement/SelectOption.php
blob: 3d799a2cf5c436072985005f88cf2a13e1995c83 (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
<?php

namespace ipl\Html\FormElement;

use ipl\Html\BaseHtmlElement;

class SelectOption extends BaseHtmlElement
{
    protected $tag = 'option';

    /** @var string|int|null Value of the option */
    protected $value;

    /** @var string Label of the option */
    protected $label;

    /**
     * SelectOption constructor.
     *
     * @param string|int|null $value
     * @param string $label
     */
    public function __construct($value, string $label)
    {
        $this->value = $value === '' ? null : $value;
        $this->label = $label;

        $this->getAttributes()->registerAttributeCallback('value', [$this, 'getValueAttribute']);
    }

    /**
     * Set the label of the option
     *
     * @param string $label
     *
     * @return $this
     */
    public function setLabel(string $label): self
    {
        $this->label = $label;

        return $this;
    }

    /**
     * Get the label of the option
     *
     * @return string
     */
    public function getLabel(): string
    {
        return $this->label;
    }

    /**
     * Get the value of the option
     *
     * @return string|int|null
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * Callback for the value attribute
     *
     * @return mixed
     */
    public function getValueAttribute()
    {
        return (string) $this->getValue();
    }

    protected function assemble()
    {
        $this->setContent($this->getLabel());
    }
}