| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Html\FormElement; |
| 4 |
|
| 5 |
use ipl\Html\BaseHtmlElement; |
| 6 |
|
| 7 |
class SelectOption extends BaseHtmlElement |
| 8 |
{ |
| 9 |
protected $tag = 'option'; |
| 10 |
|
| 11 |
/** @var string|int Value of the option */ |
| 12 |
protected $value; |
| 13 |
|
| 14 |
/** @var string Label of the option */ |
| 15 |
protected $label; |
| 16 |
|
| 17 |
/** |
| 18 |
* SelectOption constructor. |
| 19 |
* |
| 20 |
* @param string|int $value |
| 21 |
* @param string $label |
| 22 |
*/ |
| 23 |
public function __construct(string|int $value, string $label) |
| 24 |
{ |
| 25 |
$this->value = $value; |
| 26 |
$this->label = $label; |
| 27 |
|
| 28 |
$this->getAttributes()->registerAttributeCallback('value', [$this, 'getValueAttribute']); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Set the label of the option |
| 33 |
* |
| 34 |
* @param string $label |
| 35 |
* |
| 36 |
* @return $this |
| 37 |
*/ |
| 38 |
public function setLabel(string $label): self |
| 39 |
{ |
| 40 |
$this->label = $label; |
| 41 |
|
| 42 |
return $this; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get the label of the option |
| 47 |
* |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
public function getLabel(): string |
| 51 |
{ |
| 52 |
return $this->label; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Get the value of the option |
| 57 |
* |
| 58 |
* @return string|int |
| 59 |
*/ |
| 60 |
public function getValue() |
| 61 |
{ |
| 62 |
return $this->value; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Callback for the value attribute |
| 67 |
* |
| 68 |
* @return mixed |
| 69 |
*/ |
| 70 |
public function getValueAttribute() |
| 71 |
{ |
| 72 |
return (string) $this->getValue(); |
| 73 |
} |
| 74 |
|
| 75 |
protected function assemble() |
| 76 |
{ |
| 77 |
$this->setContent($this->getLabel()); |
| 78 |
} |
| 79 |
} |