| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Html\Common; |
| 4 |
|
| 5 |
use ipl\Html\Attributes; |
| 6 |
use ipl\Html\Contract\FormElement; |
| 7 |
|
| 8 |
/** |
| 9 |
* Trait for form elements that can have the `multiple` attribute |
| 10 |
* |
| 11 |
* **Example usage:** |
| 12 |
* |
| 13 |
* ``` |
| 14 |
* namespace ipl\Html\FormElement; |
| 15 |
* |
| 16 |
* use ipl\Html\Common\MultipleAttribute; |
| 17 |
* |
| 18 |
* class SelectElement extends BaseFormElement |
| 19 |
* { |
| 20 |
* protected function registerAttributeCallbacks(Attributes $attributes) |
| 21 |
* { |
| 22 |
* // ... |
| 23 |
* $this->registerMultipleAttributeCallback($attributes); |
| 24 |
* } |
| 25 |
* } |
| 26 |
* ``` |
| 27 |
*/ |
| 28 |
trait MultipleAttribute |
| 29 |
{ |
| 30 |
/** @var bool Whether the attribute `multiple` is set to `true` */ |
| 31 |
protected $multiple = false; |
| 32 |
|
| 33 |
/** |
| 34 |
* Get whether the attribute `multiple` is set to `true` |
| 35 |
* |
| 36 |
* @return bool |
| 37 |
*/ |
| 38 |
public function isMultiple(): bool |
| 39 |
{ |
| 40 |
return $this->multiple; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Set the `multiple` attribute |
| 45 |
* |
| 46 |
* @param bool $multiple |
| 47 |
* |
| 48 |
* @return $this |
| 49 |
*/ |
| 50 |
public function setMultiple(bool $multiple): self |
| 51 |
{ |
| 52 |
$this->multiple = $multiple; |
| 53 |
|
| 54 |
return $this; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Register the callback for `multiple` Attribute |
| 59 |
* |
| 60 |
* @param Attributes $attributes |
| 61 |
*/ |
| 62 |
protected function registerMultipleAttributeCallback(Attributes $attributes): void |
| 63 |
{ |
| 64 |
$attributes->registerAttributeCallback( |
| 65 |
'multiple', |
| 66 |
[$this, 'isMultiple'], |
| 67 |
[$this, 'setMultiple'] |
| 68 |
); |
| 69 |
} |
| 70 |
} |