Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Tabs
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 7
110
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
 getId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTabs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAttributes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setId
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setTab
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 setAttributes
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types = 1 );
3
4/**
5 * Tabs.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 `Tabs` 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\TabsRenderer;
23
24/**
25 * Tabs
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 Tabs extends Component {
35    private string $id = '';
36
37    public function __construct(
38        TabsRenderer $renderer,
39        private array $tabs,
40        private array $attributes
41    ) {
42        parent::__construct( $renderer );
43    }
44
45    /**
46     * Get the HTML ID for the tabs.
47     *
48     * This method returns the HTML `id` attribute value for the tabs element.
49     *
50     * @since 0.1.0
51     * @return string The ID for the tabs.
52     */
53    public function getId(): string {
54        return $this->id;
55    }
56
57    /**
58     * Get the array of tabs in the component.
59     *
60     * This method returns an array of `Tab` objects that represent each tab in the component.
61     * Each tab contains properties such as label, content, selected state, and disabled state.
62     *
63     * @since 0.1.0
64     * @return array The array of `Tab` objects representing the tabs in the component.
65     */
66    public function getTabs(): array {
67        return $this->tabs;
68    }
69
70    /**
71     * Get the additional HTML attributes for the `<form>` element.
72     *
73     * This method returns an associative array of custom HTML attributes applied to the `<form>` element
74     * that wraps the tabs. These attributes can be used to enhance accessibility or integrate with JavaScript.
75     *
76     * @since 0.1.0
77     * @return array The additional attributes as an array.
78     */
79    public function getAttributes(): array {
80        return $this->attributes;
81    }
82
83    /**
84     * Set the Tabs HTML ID attribute.
85     *
86     * @deprecated Use setAttributes() to set the ID
87     * @since 0.1.0
88     * @param string $id The ID for the Tabs element.
89     * @return $this
90     */
91    public function setId( string $id ): self {
92        $this->id = $id;
93
94        return $this;
95    }
96
97    /**
98     * Add one or more tabs to the tabs component.
99     *
100     * This method allows the addition of one or more `Tab` objects to the tabs component.
101     * Each tab's properties, such as label, content, selected state, and disabled state,
102     * are used to construct and configure the tabs.
103     *
104     * Example usage:
105     *
106     *     $tabs->setTab($tab1)
107     *         ->setTab([$tab2, $tab3]);
108     *
109     * @since 0.1.0
110     * @param Tab|Tab[] $tab A `Tab` object or an array of `Tab` objects to add.
111     * @return $this Returns the Tabs instance for method chaining.
112     */
113    public function setTab( Tab|array $tab ): self {
114        if ( is_array( $tab ) ) {
115            foreach ( $tab as $t ) {
116                $this->tabs[] = $t;
117            }
118        } else {
119            $this->tabs[] = $tab;
120        }
121
122        return $this;
123    }
124
125    /**
126     * Set additional HTML attributes for the `<form>` element.
127     *
128     * This method allows custom HTML attributes to be added to the `<form>` element that wraps the tabs,
129     * such as `id`, `data-*`, `aria-*`, or any other valid attributes. These attributes can be used
130     * to enhance accessibility or integrate with JavaScript.
131     *
132     * Example usage:
133     *
134     *     $tabs->setAttributes([
135     *         'id' => 'tabs-form',
136     *         'data-category' => 'navigation',
137     *     ]);
138     *
139     * @since 0.1.0
140     * @param array $attributes An associative array of HTML attributes.
141     * @return $this Returns the Tabs instance for method chaining.
142     */
143    public function setAttributes( array $attributes ): self {
144        foreach ( $attributes as $key => $value ) {
145            $this->attributes[$key] = $value;
146        }
147        return $this;
148    }
149}