| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Html\FormElement; |
| 4 |
|
| 5 |
use ipl\Html\Attributes; |
| 6 |
|
| 7 |
class RadioOption |
| 8 |
{ |
| 9 |
/** @var string The default label class */ |
| 10 |
public const LABEL_CLASS = 'radio-label'; |
| 11 |
|
| 12 |
/** @var string|int Value of the option */ |
| 13 |
protected $value; |
| 14 |
|
| 15 |
/** @var string Label of the option */ |
| 16 |
protected $label; |
| 17 |
|
| 18 |
/** @var mixed Css class of the option's label */ |
| 19 |
protected $labelCssClass = self::LABEL_CLASS; |
| 20 |
|
| 21 |
/** @var bool Whether the radio option is disabled */ |
| 22 |
protected $disabled = false; |
| 23 |
|
| 24 |
/** @var Attributes */ |
| 25 |
protected $attributes; |
| 26 |
|
| 27 |
/** |
| 28 |
* RadioOption constructor. |
| 29 |
* |
| 30 |
* @param string|int $value |
| 31 |
* @param string $label |
| 32 |
*/ |
| 33 |
public function __construct(string|int $value, string $label) |
| 34 |
{ |
| 35 |
$this->value = $value; |
| 36 |
$this->label = $label; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Set the label of the option |
| 41 |
* |
| 42 |
* @param string $label |
| 43 |
* |
| 44 |
* @return $this |
| 45 |
*/ |
| 46 |
public function setLabel(string $label): self |
| 47 |
{ |
| 48 |
$this->label = $label; |
| 49 |
|
| 50 |
return $this; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get the label of the option |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function getLabel(): string |
| 59 |
{ |
| 60 |
return $this->label; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get the value of the option |
| 65 |
* |
| 66 |
* @return string|int |
| 67 |
*/ |
| 68 |
public function getValue(): string|int |
| 69 |
{ |
| 70 |
return $this->value; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Set css class to the option label |
| 75 |
* |
| 76 |
* @param string|string[] $labelCssClass |
| 77 |
* |
| 78 |
* @return $this |
| 79 |
*/ |
| 80 |
public function setLabelCssClass($labelCssClass): self |
| 81 |
{ |
| 82 |
$this->labelCssClass = $labelCssClass; |
| 83 |
|
| 84 |
return $this; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Get css class of the option label |
| 89 |
* |
| 90 |
* @return string|string[] |
| 91 |
*/ |
| 92 |
public function getLabelCssClass() |
| 93 |
{ |
| 94 |
return $this->labelCssClass; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Set whether to disable the option |
| 99 |
* |
| 100 |
* @param bool $disabled |
| 101 |
* |
| 102 |
* @return $this |
| 103 |
*/ |
| 104 |
public function setDisabled(bool $disabled = true): self |
| 105 |
{ |
| 106 |
$this->disabled = $disabled; |
| 107 |
|
| 108 |
return $this; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get whether the option is disabled |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
public function isDisabled(): bool |
| 117 |
{ |
| 118 |
return $this->disabled; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Add the attributes |
| 123 |
* |
| 124 |
* @param Attributes $attributes |
| 125 |
* |
| 126 |
* @return $this |
| 127 |
*/ |
| 128 |
public function addAttributes(Attributes $attributes): self |
| 129 |
{ |
| 130 |
$this->attributes = $attributes; |
| 131 |
|
| 132 |
return $this; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get the attributes |
| 137 |
* |
| 138 |
* @return Attributes |
| 139 |
*/ |
| 140 |
public function getAttributes(): Attributes |
| 141 |
{ |
| 142 |
if ($this->attributes === null) { |
| 143 |
$this->attributes = new Attributes(); |
| 144 |
} |
| 145 |
|
| 146 |
return $this->attributes; |
| 147 |
} |
| 148 |
} |