Debian Codegraph @

vendor/oojs/oojs-ui/php/widgets/CheckboxMultiselectInputWidget.php

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * Multiple checkbox input widget. Intended to be used within a OO.ui.FormLayout.
7 *
8 * @phan-suppress-next-line PhanParamSignatureMismatch Parent returns a string
9 * @method string[] getValue()
10 */
11 class CheckboxMultiselectInputWidget extends InputWidget {
12
13 /* Properties */
14
15 /**
16 * @var string|null
17 */
18 protected $name = null;
19
20 /**
21 * Input value.
22 *
23 * @var string[]
24 */
25 protected $value = [];
26
27 /**
28 * Layouts for this input, as FieldLayouts.
29 *
30 * @var FieldLayout[]
31 */
32 protected $fields = [];
33
34 /**
35 * @param array $config Configuration options
36 * - array[] $config['options'] Array of menu options in the format
37 * `[ 'data' => …, 'label' => …, 'disabled' => … ]`
38 */
39 public function __construct( array $config = [] ) {
40 // Parent constructor
41 parent::__construct( $config );
42
43 if ( isset( $config['name'] ) ) {
44 $this->name = $config['name'];
45 }
46
47 // Initialization
48 $this->setOptions( $config['options'] ?? [] );
49 // Have to repeat this from parent, as we need options to be set up for this to make sense
50 $this->setValue( $config['value'] ?? null );
51 $this->addClasses( [ 'oo-ui-checkboxMultiselectInputWidget' ] );
52 }
53
54 /** @inheritDoc */
55 protected function getInputElement( $config ) {
56 // Actually unused
57 return new Tag( 'unused' );
58 }
59
60 /**
61 * Set the value of the input.
62 *
63 * @param mixed $value New value should be an array of strings
64 * @return $this
65 */
66 public function setValue( $value ) {
67 $this->value = $this->cleanUpValue( $value );
68 // Deselect all options
69 foreach ( $this->fields as $field ) {
70 $widget = $field->getField();
71 '@phan-var CheckboxInputWidget $widget';
72 $widget->setSelected( false );
73 }
74 // Select the requested ones
75 foreach ( $this->value as $key ) {
76 $widget = $this->fields[ $key ]->getField();
77 '@phan-var CheckboxInputWidget $widget';
78 $widget->setSelected( true );
79 }
80 return $this;
81 }
82
83 /**
84 * Clean up incoming value.
85 *
86 * @param mixed $value Original value
87 * @return string[] Cleaned up value
88 * @suppress PhanParamSignatureMismatch Parent has 'string' instead of 'string[]'
89 */
90 protected function cleanUpValue( $value ) {
91 $cleanValue = [];
92 if ( !is_array( $value ) ) {
93 return $cleanValue;
94 }
95 foreach ( $value as $singleValue ) {
96 $singleValue = parent::cleanUpValue( $singleValue );
97 // Remove options that we don't have here
98 if ( !isset( $this->fields[ $singleValue ] ) ) {
99 continue;
100 }
101 $cleanValue[] = $singleValue;
102 }
103 return $cleanValue;
104 }
105
106 /**
107 * Set the options available for this input.
108 *
109 * @param array[] $options Array of menu options in the format
110 * `[ 'data' => …, 'label' => …, 'disabled' => … ]`
111 * @return $this
112 */
113 public function setOptions( $options ) {
114 $this->fields = [];
115
116 // Rebuild the checkboxes
117 $this->clearContent();
118 $name = $this->name;
119 foreach ( $options as $opt ) {
120 $optValue = parent::cleanUpValue( $opt['data'] );
121 $optDisabled = $opt['disabled'] ?? false;
122 $field = new FieldLayout(
123 new CheckboxInputWidget( [
124 'name' => $name,
125 'value' => $optValue,
126 'disabled' => $this->isDisabled() || $optDisabled,
127 ] ),
128 [
129 'label' => $opt['label'] ?? $optValue,
130 'align' => 'inline',
131 ]
132 );
133
134 $this->fields[ $optValue ] = $field;
135 $this->appendContent( $field );
136 }
137
138 // Re-set the value, checking the checkboxes as needed.
139 // This will also get rid of any stale options that we just removed.
140 $this->setValue( $this->getValue() );
141
142 return $this;
143 }
144
145 /** @inheritDoc */
146 public function setDisabled( $disabled ) {
147 parent::setDisabled( $disabled );
148 foreach ( $this->fields as $field ) {
149 $field->getField()->setDisabled( $this->isDisabled() );
150 }
151 return $this;
152 }
153
154 /** @inheritDoc */
155 public function getConfig( &$config ) {
156 $options = [];
157 foreach ( $this->fields as $field ) {
158 $widget = $field->getField();
159 '@phan-var CheckboxInputWidget $widget';
160 $options[] = [
161 'data' => $widget->getValue(),
162 'label' => $field->getLabel(),
163 'disabled' => $widget->isDisabled(),
164 ];
165 }
166 $config['options'] = $options;
167 return parent::getConfig( $config );
168 }
169 }