Debian Codegraph @

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

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * Radio input widget.
7 */
8 class RadioInputWidget extends InputWidget {
9 use RequiredElement;
10
11 /* Static Properties */
12
13 /** @var string */
14 public static $tagName = 'span';
15
16 /**
17 * @param array $config Configuration options
18 * - bool $config['selected'] Whether the radio button is initially selected
19 * (default: false)
20 */
21 public function __construct( array $config = [] ) {
22 // Parent constructor
23 parent::__construct( $config );
24
25 // Traits
26 $this->initializeRequiredElement(
27 array_merge( [ 'indicatorElement' => null ], $config )
28 );
29
30 // Initialization
31 $this->addClasses( [ 'oo-ui-radioInputWidget' ] );
32 // Required for pretty styling in WikimediaUI theme
33 $this->appendContent( new Tag( 'span' ) );
34 $this->setSelected( $config['selected'] ?? false );
35 }
36
37 /** @inheritDoc */
38 protected function getInputElement( $config ) {
39 return ( new Tag( 'input' ) )->setAttributes( [ 'type' => 'radio' ] );
40 }
41
42 /**
43 * Set selection state of this radio button.
44 *
45 * @param bool $state Whether the button is selected
46 * @return $this
47 */
48 public function setSelected( $state ) {
49 // RadioInputWidget doesn't track its state.
50 if ( $state ) {
51 $this->input->setAttributes( [ 'checked' => 'checked' ] );
52 } else {
53 $this->input->removeAttributes( [ 'checked' ] );
54 }
55 return $this;
56 }
57
58 /**
59 * Check if this radio button is selected.
60 *
61 * @return bool Radio is selected
62 */
63 public function isSelected() {
64 return $this->input->getAttribute( 'checked' ) === 'checked';
65 }
66
67 /** @inheritDoc */
68 public function getConfig( &$config ) {
69 if ( $this->isSelected() ) {
70 $config['selected'] = true;
71 }
72 return parent::getConfig( $config );
73 }
74 }