Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 14 |
CRAP | |
0.00% |
0 / 1 |
| Tab | |
0.00% |
0 / 24 |
|
0.00% |
0 / 14 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isSelected | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isDisabled | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setName | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| setLabel | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| setSelected | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setDisabled | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| setContent | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| build | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | /** |
| 5 | * Tab.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 `Tab` 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 | |
| 19 | namespace Wikimedia\Codex\Component; |
| 20 | |
| 21 | use InvalidArgumentException; |
| 22 | use Wikimedia\Codex\Traits\ContentSetter; |
| 23 | |
| 24 | /** |
| 25 | * Tab |
| 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 | */ |
| 34 | class Tab { |
| 35 | use ContentSetter; |
| 36 | |
| 37 | private string $id = ''; |
| 38 | |
| 39 | public function __construct( |
| 40 | private string $name, |
| 41 | private string $label, |
| 42 | private string|HtmlSnippet $content, |
| 43 | private bool $selected, |
| 44 | private bool $disabled |
| 45 | ) { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the Tab's HTML ID attribute. |
| 50 | * |
| 51 | * This method returns the ID assigned to the tab element, which is used |
| 52 | * for identifying the tab in the HTML document. |
| 53 | * |
| 54 | * @since 0.1.0 |
| 55 | * @return string The ID of the Tab element. |
| 56 | */ |
| 57 | public function getId(): string { |
| 58 | return $this->id; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the tab's name. |
| 63 | * |
| 64 | * @since 0.1.0 |
| 65 | * @return string The unique name of the tab. |
| 66 | */ |
| 67 | public function getName(): string { |
| 68 | return $this->name; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the tab's label. |
| 73 | * |
| 74 | * @since 0.1.0 |
| 75 | * @return string The label of the tab. |
| 76 | */ |
| 77 | public function getLabel(): string { |
| 78 | return $this->label; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the tab's content. |
| 83 | * |
| 84 | * @since 0.1.0 |
| 85 | * @return string|HtmlSnippet The content of the tab. |
| 86 | */ |
| 87 | public function getContent(): string|HtmlSnippet { |
| 88 | return $this->content; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the tab's selected state. |
| 93 | * |
| 94 | * @since 0.1.0 |
| 95 | * @return bool Whether the tab is selected. |
| 96 | */ |
| 97 | public function isSelected(): bool { |
| 98 | return $this->selected; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get the tab's disabled state. |
| 103 | * |
| 104 | * @since 0.1.0 |
| 105 | * @return bool Whether the tab is disabled. |
| 106 | */ |
| 107 | public function isDisabled(): bool { |
| 108 | return $this->disabled; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Set the tab HTML ID attribute. |
| 113 | * |
| 114 | * @deprecated Use setAttributes() to set the ID |
| 115 | * @since 0.1.0 |
| 116 | * @param string $id The ID for the tab element. |
| 117 | * @return $this |
| 118 | */ |
| 119 | public function setId( string $id ): self { |
| 120 | $this->id = $id; |
| 121 | |
| 122 | return $this; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Set the name for the tab. |
| 127 | * |
| 128 | * The name is used for programmatic selection, and it also serves as the default label if none is provided. |
| 129 | * |
| 130 | * @since 0.1.0 |
| 131 | * @param string $name The unique name of the tab, used for programmatic selection. |
| 132 | * @return $this Returns the Tab instance for method chaining. |
| 133 | */ |
| 134 | public function setName( string $name ): self { |
| 135 | if ( trim( $name ) === '' ) { |
| 136 | throw new InvalidArgumentException( 'Tab name cannot be empty.' ); |
| 137 | } |
| 138 | $this->name = $name; |
| 139 | |
| 140 | return $this; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Set the label for the tab. |
| 145 | * |
| 146 | * The label corresponds to the text displayed in the Tabs component's header for this tab. |
| 147 | * If not set, the label will default to the name of the tab. |
| 148 | * |
| 149 | * @since 0.1.0 |
| 150 | * @param string $label The label text to be displayed in the Tabs component's header. |
| 151 | * @return $this Returns the Tab instance for method chaining. |
| 152 | */ |
| 153 | public function setLabel( string $label ): self { |
| 154 | if ( trim( $label ) === '' ) { |
| 155 | throw new InvalidArgumentException( 'Tab label cannot be empty.' ); |
| 156 | } |
| 157 | $this->label = $label; |
| 158 | |
| 159 | return $this; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Set whether the tab should be selected by default. |
| 164 | * |
| 165 | * @since 0.1.0 |
| 166 | * @param bool $selected Whether this tab should be selected by default. |
| 167 | * @return $this Returns the Tab instance for method chaining. |
| 168 | */ |
| 169 | public function setSelected( bool $selected ): self { |
| 170 | $this->selected = $selected; |
| 171 | |
| 172 | return $this; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Set the disabled state for the tab. |
| 177 | * |
| 178 | * Disabled tabs cannot be accessed via label clicks or keyboard navigation. |
| 179 | * |
| 180 | * @since 0.1.0 |
| 181 | * @param bool $disabled Whether or not the tab is disabled. |
| 182 | * @return $this Returns the Tab instance for method chaining. |
| 183 | */ |
| 184 | public function setDisabled( bool $disabled ): self { |
| 185 | $this->disabled = $disabled; |
| 186 | |
| 187 | return $this; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Set the content of the tab. |
| 192 | * |
| 193 | * @param string|HtmlSnippet $content Text or HTML to be displayed when this tab is selected. |
| 194 | * @return $this Returns the Tab instance for method chaining. |
| 195 | */ |
| 196 | public function setContent( string|HtmlSnippet $content ): self { |
| 197 | $this->content = $content; |
| 198 | |
| 199 | return $this; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Backwards compatibility for the pre-0.8 API. |
| 204 | * @return static |
| 205 | */ |
| 206 | public function build() { |
| 207 | return $this; |
| 208 | } |
| 209 | } |