Debian Codegraph @

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

raw
1 <?php
2
3 namespace OOUI;
4
5 /**
6 * ToggleSwitch widget.
7 */
8 class ToggleSwitchWidget extends ToggleWidget {
9 use TabIndexedElement;
10
11 /**
12 * Hyperlink to visit when clicked.
13 *
14 * @var string|null
15 */
16 protected $href = null;
17
18 /**
19 * @var Tag|null
20 */
21 private $link = null;
22
23 /**
24 * @var Tag
25 */
26 private $glow;
27
28 /**
29 * @var Tag
30 */
31 private $grip;
32
33 /**
34 * @param array $config Configuration options
35 * - string $config['href'] href to link toggle to (default: null)
36 */
37 public function __construct( array $config = [] ) {
38 $this->href = $config['href'] ?? null;
39 $this->link = new Tag( 'a' );
40
41 // Parent constructor
42 parent::__construct( $config );
43
44 // Traits
45 $this->initializeTabIndexedElement( $config );
46
47 $this->glow = ( new Tag( 'span' ) )->setAttributes( [ 'class' => 'oo-ui-toggleSwitchWidget-glow' ] );
48 $this->grip = ( new Tag( 'span' ) )->setAttributes( [ 'class' => 'oo-ui-toggleSwitchWidget-grip' ] );
49
50 $this->addClasses( [ 'oo-ui-toggleSwitchWidget' ] );
51 $this->setAttributes( [ 'role' => 'switch' ] );
52
53 $this->link->appendContent( $this->grip );
54 $this->appendContent( $this->glow, $this->link );
55 }
56
57 /** @inheritDoc */
58 public function setValue( $value = null ) {
59 parent::setValue( $value );
60 $this->setAttributes( [
61 'aria-checked' => $this->getValue() ? 'true' : 'false'
62 ] );
63 return $this;
64 }
65
66 /**
67 * Set hyperlink location.
68 *
69 * @param string|null $href Hyperlink location, null to remove
70 * @return $this
71 */
72 public function setHref( ?string $href ): ToggleSwitchWidget {
73 $this->href = is_string( $href ) ? $href : null;
74
75 $this->updateHref();
76
77 return $this;
78 }
79
80 /**
81 * Update the href attribute, in case of changes to href or disabled
82 * state.
83 *
84 * @return $this
85 */
86 public function updateHref() {
87 if ( $this->href !== null && !$this->isDisabled() ) {
88 $this->link->setAttributes( [ 'href' => $this->href ] );
89 }
90 return $this;
91 }
92
93 /** @inheritDoc */
94 public function getConfig( &$config ) {
95 if ( $this->href !== null && !$this->isDisabled() ) {
96 $config['href'] = $this->href;
97 }
98 return parent::getConfig( $config );
99 }
100
101 /** @inheritDoc */
102 public function setDisabled( $disabled ) {
103 $this->disabled = (bool)$disabled;
104
105 if ( $this->href !== null && !$this->isDisabled() ) {
106 $this->updateHref();
107 }
108
109 return parent::setDisabled( $disabled );
110 }
111 }