Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
ToggleSwitchBuilder | |
0.00% |
0 / 32 |
|
0.00% |
0 / 10 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setInputId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setName | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setValue | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setLabel | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setChecked | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setDisabled | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setInputAttributes | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setWrapperAttributes | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
build | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** |
3 | * ToggleSwitchBuilder.php |
4 | * |
5 | * This file is part of the Codex design system, the official design system |
6 | * for Wikimedia projects. It provides the `ToggleSwitchBuilder` class, a builder for |
7 | * constructing toggle switch components using the Codex design system. |
8 | * |
9 | * A ToggleSwitch enables the user to instantly toggle between on and off states. |
10 | * |
11 | * @category Builder |
12 | * @package Codex\Builder |
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 | |
19 | namespace Wikimedia\Codex\Builder; |
20 | |
21 | use InvalidArgumentException; |
22 | use Wikimedia\Codex\Component\Label; |
23 | use Wikimedia\Codex\Component\ToggleSwitch; |
24 | use Wikimedia\Codex\Renderer\ToggleSwitchRenderer; |
25 | |
26 | /** |
27 | * ToggleSwitchBuilder |
28 | * |
29 | * This class implements the builder pattern to construct instances of ToggleSwitch. |
30 | * It provides a fluent interface for setting various properties and building the |
31 | * final immutable object with predefined configurations and immutability. |
32 | * |
33 | * @category Builder |
34 | * @package Codex\Builder |
35 | * @since 0.1.0 |
36 | * @author Doğu Abaris <abaris@null.net> |
37 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
38 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
39 | */ |
40 | class ToggleSwitchBuilder { |
41 | |
42 | /** |
43 | * The ID for the toggle switch input. |
44 | */ |
45 | protected string $inputId = ''; |
46 | |
47 | /** |
48 | * The name attribute for the toggle switch input. |
49 | */ |
50 | protected string $name = ''; |
51 | |
52 | /** |
53 | * The value associated with the toggle switch input. |
54 | */ |
55 | protected string $value = ''; |
56 | |
57 | /** |
58 | * The label for the toggle switch. |
59 | */ |
60 | protected ?Label $label = null; |
61 | |
62 | /** |
63 | * Whether the toggle is checked by default. |
64 | */ |
65 | protected bool $checked = false; |
66 | |
67 | /** |
68 | * Whether the toggle is disabled. |
69 | */ |
70 | protected bool $disabled = false; |
71 | |
72 | /** |
73 | * Additional HTML attributes for the input element. |
74 | */ |
75 | protected array $inputAttributes = []; |
76 | |
77 | /** |
78 | * Additional HTML attributes for the wrapper element. |
79 | */ |
80 | protected array $wrapperAttributes = []; |
81 | |
82 | /** |
83 | * The renderer instance used to render the toggle switch. |
84 | */ |
85 | protected ToggleSwitchRenderer $renderer; |
86 | |
87 | /** |
88 | * Constructor for the ToggleSwitchBuilder class. |
89 | * |
90 | * @param ToggleSwitchRenderer $renderer The renderer to use for rendering the toggle switch. |
91 | */ |
92 | public function __construct( ToggleSwitchRenderer $renderer ) { |
93 | $this->renderer = $renderer; |
94 | } |
95 | |
96 | /** |
97 | * Set the ID for the toggle switch input. |
98 | * |
99 | * @param string $inputId The ID for the toggle switch input. |
100 | * @return $this Returns the ToggleSwitch instance for method chaining. |
101 | */ |
102 | public function setInputId( string $inputId ): self { |
103 | $this->inputId = $inputId; |
104 | return $this; |
105 | } |
106 | |
107 | /** |
108 | * Set the name for the toggle switch input. |
109 | * |
110 | * @param string $name The name attribute for the toggle switch input. |
111 | * @return $this Returns the ToggleSwitch instance for method chaining. |
112 | */ |
113 | public function setName( string $name ): self { |
114 | $this->name = $name; |
115 | return $this; |
116 | } |
117 | |
118 | /** |
119 | * Set the value for the toggle switch input. |
120 | * |
121 | * @param string $value The value associated with the toggle switch input. |
122 | * @return $this Returns the ToggleSwitch instance for method chaining. |
123 | */ |
124 | public function setValue( string $value ): self { |
125 | $this->value = $value; |
126 | return $this; |
127 | } |
128 | |
129 | /** |
130 | * Set the label for the toggle switch. |
131 | * |
132 | * @param Label $label The label object for the toggle switch. |
133 | * @return $this Returns the ToggleSwitch instance for method chaining. |
134 | */ |
135 | public function setLabel( Label $label ): self { |
136 | $this->label = $label; |
137 | return $this; |
138 | } |
139 | |
140 | /** |
141 | * Set whether the toggle switch should be checked by default. |
142 | * |
143 | * @param bool $checked Whether the toggle switch should be checked by default. |
144 | * @return $this Returns the ToggleSwitch instance for method chaining. |
145 | */ |
146 | public function setChecked( bool $checked ): self { |
147 | $this->checked = $checked; |
148 | return $this; |
149 | } |
150 | |
151 | /** |
152 | * Set whether the toggle switch should be disabled. |
153 | * |
154 | * @param bool $disabled Whether the toggle switch should be disabled. |
155 | * @return $this Returns the ToggleSwitch instance for method chaining. |
156 | */ |
157 | public function setDisabled( bool $disabled ): self { |
158 | $this->disabled = $disabled; |
159 | return $this; |
160 | } |
161 | |
162 | /** |
163 | * Set additional HTML attributes for the input element. |
164 | * |
165 | * @param array $inputAttributes An associative array of HTML attributes for the input element. |
166 | * @return $this Returns the ToggleSwitch instance for method chaining. |
167 | */ |
168 | public function setInputAttributes( array $inputAttributes ): self { |
169 | $this->inputAttributes = $inputAttributes; |
170 | return $this; |
171 | } |
172 | |
173 | /** |
174 | * Set additional HTML attributes for the wrapper element. |
175 | * |
176 | * @param array $wrapperAttributes An associative array of HTML attributes for the wrapper element. |
177 | * @return $this Returns the ToggleSwitch instance for method chaining. |
178 | */ |
179 | public function setWrapperAttributes( array $wrapperAttributes ): self { |
180 | $this->wrapperAttributes = $wrapperAttributes; |
181 | return $this; |
182 | } |
183 | |
184 | /** |
185 | * Build and return the ToggleSwitch component object. |
186 | * |
187 | * @return ToggleSwitch The constructed ToggleSwitch component. |
188 | */ |
189 | public function build(): ToggleSwitch { |
190 | if ( !$this->inputId ) { |
191 | throw new InvalidArgumentException( "The 'id' is required for ToggleSwitch." ); |
192 | } |
193 | if ( !$this->label ) { |
194 | throw new InvalidArgumentException( "The 'label' is required for ToggleSwitch." ); |
195 | } |
196 | |
197 | return new ToggleSwitch( |
198 | $this->inputId, |
199 | $this->name, |
200 | $this->value, |
201 | $this->label, |
202 | $this->checked, |
203 | $this->disabled, |
204 | $this->inputAttributes, |
205 | $this->wrapperAttributes, |
206 | $this->renderer |
207 | ); |
208 | } |
209 | } |