Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
ToggleSwitch
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 17
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getInputId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLabel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isChecked
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isDisabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getInputAttributes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getWrapperAttributes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setInputId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setName
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setValue
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setLabel
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setChecked
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setDisabled
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setInputAttributes
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setWrapperAttributes
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4/**
5 * ToggleSwitch.php
6 *
7 * This file is part of the Codex design system, the official design system for Wikimedia projects.
8 * It contains the definition and implementation of the `ToggleSwitch` class, responsible for managing
9 * the behavior and properties of the corresponding component.
10 *
11 * @category Component
12 * @package  Codex\Component
13 * @since    0.1.0
14 * @author   Doğu Abaris <abaris@null.net>
15 * @license  https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later
16 * @link     https://doc.wikimedia.org/codex/main/ Codex Documentation
17 */
18
19namespace Wikimedia\Codex\Component;
20
21use Wikimedia\Codex\Contract\Component;
22use Wikimedia\Codex\Renderer\ToggleSwitchRenderer;
23
24/**
25 * ToggleSwitch
26 *
27 * @category Component
28 * @package  Codex\Component
29 * @since    0.1.0
30 * @author   Doğu Abaris <abaris@null.net>
31 * @license  https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later
32 * @link     https://doc.wikimedia.org/codex/main/ Codex Documentation
33 */
34class ToggleSwitch extends Component {
35    public function __construct(
36        ToggleSwitchRenderer $renderer,
37        private string $inputId,
38        private string $name,
39        private ?Label $label,
40        private string $value,
41        private bool $checked,
42        private bool $disabled,
43        private array $inputAttributes,
44        private array $wrapperAttributes
45    ) {
46        parent::__construct( $renderer );
47    }
48
49    /**
50     * Get the ID for the toggle switch input.
51     *
52     * This method returns the unique identifier for the toggle switch input element.
53     * The ID is used to associate the input with its corresponding label and for any JavaScript or CSS targeting.
54     *
55     * @since 0.1.0
56     * @return string The ID for the toggle switch input.
57     */
58    public function getInputId(): string {
59        return $this->inputId;
60    }
61
62    /**
63     * Get the name attribute of the toggle switch input.
64     *
65     * This method returns the name attribute, which is used to identify the toggle switch when the form is submitted.
66     *
67     * @since 0.1.0
68     * @return string The name attribute of the toggle switch input.
69     */
70    public function getName(): string {
71        return $this->name;
72    }
73
74    /**
75     * Get the value associated with the toggle switch input.
76     *
77     * This method returns the value submitted when the toggle switch is checked and the form is submitted.
78     *
79     * @since 0.1.0
80     * @return string The value of the toggle switch input.
81     */
82    public function getValue(): string {
83        return $this->value;
84    }
85
86    /**
87     * Get the label object for the toggle switch.
88     *
89     * This method returns the label object that provides a descriptive label for the toggle switch.
90     * The label is crucial for accessibility and usability.
91     *
92     * @since 0.1.0
93     * @return ?Label The label object for the toggle switch.
94     */
95    public function getLabel(): ?Label {
96        return $this->label;
97    }
98
99    /**
100     * Check if the toggle switch is checked by default.
101     *
102     * This method returns a boolean value indicating whether the toggle switch is checked by default.
103     * If true, the toggle switch is rendered in a checked state.
104     *
105     * @since 0.1.0
106     * @return bool True if the toggle switch is checked, false otherwise.
107     */
108    public function isChecked(): bool {
109        return $this->checked;
110    }
111
112    /**
113     * Check if the toggle switch is disabled.
114     *
115     * This method returns a boolean value indicating whether the toggle switch is disabled,
116     * preventing user interaction. A disabled toggle switch cannot be checked or unchecked by the user.
117     *
118     * @since 0.1.0
119     * @return bool True if the toggle switch is disabled, false otherwise.
120     */
121    public function isDisabled(): bool {
122        return $this->disabled;
123    }
124
125    /**
126     * Get the additional HTML attributes for the toggle switch input.
127     *
128     * This method returns an associative array of custom HTML attributes for the toggle switch input element,
129     * such as `id`, `data-*`, `aria-*`, or any other valid attributes.
130     *
131     * @since 0.1.0
132     * @return array The additional attributes as an array.
133     */
134    public function getInputAttributes(): array {
135        return $this->inputAttributes;
136    }
137
138    /**
139     * Get additional HTML attributes for the outer wrapper element.
140     *
141     * This method returns an associative array of custom HTML attributes that are applied to the outer wrapper element,
142     * enhancing its behavior or styling.
143     *
144     * @since 0.1.0
145     * @return array The associative array of HTML attributes for the wrapper element.
146     */
147    public function getWrapperAttributes(): array {
148        return $this->wrapperAttributes;
149    }
150
151    /**
152     * Set the ID for the toggle switch input.
153     *
154     * @param string $inputId The ID for the toggle switch input.
155     * @return $this Returns the ToggleSwitch instance for method chaining.
156     */
157    public function setInputId( string $inputId ): self {
158        $this->inputId = $inputId;
159        return $this;
160    }
161
162    /**
163     * Set the name for the toggle switch input.
164     *
165     * @param string $name The name attribute for the toggle switch input.
166     * @return $this Returns the ToggleSwitch instance for method chaining.
167     */
168    public function setName( string $name ): self {
169        $this->name = $name;
170        return $this;
171    }
172
173    /**
174     * Set the value for the toggle switch input.
175     *
176     * @param string $value The value associated with the toggle switch input.
177     * @return $this Returns the ToggleSwitch instance for method chaining.
178     */
179    public function setValue( string $value ): self {
180        $this->value = $value;
181        return $this;
182    }
183
184    /**
185     * Set the label for the toggle switch.
186     *
187     * @param Label $label The label object for the toggle switch.
188     * @return $this Returns the ToggleSwitch instance for method chaining.
189     */
190    public function setLabel( Label $label ): self {
191        $this->label = $label;
192        return $this;
193    }
194
195    /**
196     * Set whether the toggle switch should be checked by default.
197     *
198     * @param bool $checked Whether the toggle switch should be checked by default.
199     * @return $this Returns the ToggleSwitch instance for method chaining.
200     */
201    public function setChecked( bool $checked ): self {
202        $this->checked = $checked;
203        return $this;
204    }
205
206    /**
207     * Set whether the toggle switch should be disabled.
208     *
209     * @param bool $disabled Whether the toggle switch should be disabled.
210     * @return $this Returns the ToggleSwitch instance for method chaining.
211     */
212    public function setDisabled( bool $disabled ): self {
213        $this->disabled = $disabled;
214        return $this;
215    }
216
217    /**
218     * Set additional HTML attributes for the input element.
219     *
220     * @param array $inputAttributes An associative array of HTML attributes for the input element.
221     * @return $this Returns the ToggleSwitch instance for method chaining.
222     */
223    public function setInputAttributes( array $inputAttributes ): self {
224        $this->inputAttributes = $inputAttributes;
225        return $this;
226    }
227
228    /**
229     * Set additional HTML attributes for the wrapper element.
230     *
231     * @param array $wrapperAttributes An associative array of HTML attributes for the wrapper element.
232     * @return $this Returns the ToggleSwitch instance for method chaining.
233     */
234    public function setWrapperAttributes( array $wrapperAttributes ): self {
235        $this->wrapperAttributes = $wrapperAttributes;
236        return $this;
237    }
238}