bytic /
form
| 1 | <?php |
||
| 2 | |||
| 3 | class Nip_Form_Element_Select extends Nip\Form\Elements\AbstractElement |
||
| 4 | { |
||
| 5 | protected $_type = 'select'; |
||
| 6 | protected $_optionsElements = []; |
||
| 7 | protected $_values = []; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @return Nip_Form_Element_Select |
||
| 11 | */ |
||
| 12 | public function addOptionsArray($options, $valueKey, $labelKey) |
||
| 13 | { |
||
| 14 | foreach ($options as $key => $option) { |
||
| 15 | $option = (object) $option; |
||
| 16 | |||
| 17 | $oValue = $option->$valueKey; |
||
| 18 | $oLabel = $option->$labelKey; |
||
| 19 | $oDisabled = $option->disabled ?? false; |
||
| 20 | |||
| 21 | $atribs = [ |
||
| 22 | 'label' => $oLabel, |
||
| 23 | ]; |
||
| 24 | |||
| 25 | if ($oDisabled) { |
||
| 26 | $atribs['disabled'] = 'disabled'; |
||
| 27 | } |
||
| 28 | $this->addOption($oValue, $atribs); |
||
| 29 | } |
||
| 30 | |||
| 31 | return $this; |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @return Nip_Form_Element_Select |
||
| 36 | */ |
||
| 37 | 1 | public function addOption($value, $label) |
|
| 38 | { |
||
| 39 | 1 | if (is_array($label)) { |
|
| 40 | $option = $label; |
||
| 41 | } else { |
||
| 42 | 1 | $option['label'] = $label; |
|
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 43 | } |
||
| 44 | |||
| 45 | 1 | $this->_optionsElements[$value] = $option; |
|
| 46 | 1 | $this->_values[] = $value; |
|
| 47 | |||
| 48 | 1 | return $this; |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return Nip_Form_Element_Select |
||
| 53 | */ |
||
| 54 | public function addOptions(array $array) |
||
| 55 | { |
||
| 56 | foreach ($array as $value => $label) { |
||
| 57 | $this->addOption($value, $label); |
||
| 58 | } |
||
| 59 | |||
| 60 | return $this; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return Nip_Form_Element_Select |
||
| 65 | */ |
||
| 66 | public function appendOptgroupOption($optgroup, $value, $label) |
||
| 67 | { |
||
| 68 | if (is_array($label)) { |
||
| 69 | $option = $label; |
||
| 70 | } else { |
||
| 71 | $option['label'] = $label; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
| 72 | } |
||
| 73 | |||
| 74 | $this->_optionsElements[$optgroup][$value] = $option; |
||
| 75 | $this->_values[] = $value; |
||
| 76 | |||
| 77 | return $this; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @deprecated to stop confusion from select options and element options |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | public function getOptions() |
||
| 85 | { |
||
| 86 | return $this->_optionsElements; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | public function getOptionsElements() |
||
| 93 | { |
||
| 94 | return $this->_optionsElements; |
||
| 95 | } |
||
| 96 | |||
| 97 | 1 | public function setValue($value) |
|
| 98 | { |
||
| 99 | 1 | if (in_array($value, $this->_values)) { |
|
| 100 | 1 | return parent::setValue($value); |
|
| 101 | } |
||
| 102 | |||
| 103 | return false; |
||
| 104 | } |
||
| 105 | } |
||
| 106 |