Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
Accordion | |
0.00% |
0 / 14 |
|
0.00% |
0 / 8 |
72 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDescription | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isOpen | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAttributes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getHtml | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Accordion.php |
4 | * |
5 | * This file is part of the Codex design system, the official design system for Wikimedia projects. |
6 | * It contains the definition and implementation of the `Accordion` class, responsible for managing |
7 | * the behavior and properties of the corresponding component. |
8 | * |
9 | * @category Component |
10 | * @package Codex\Component |
11 | * @since 0.1.0 |
12 | * @author Doğu Abaris <abaris@null.net> |
13 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
14 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
15 | */ |
16 | |
17 | namespace Wikimedia\Codex\Component; |
18 | |
19 | use Wikimedia\Codex\Renderer\AccordionRenderer; |
20 | |
21 | /** |
22 | * Accordion |
23 | * |
24 | * This class is part of the Codex PHP library and is responsible for |
25 | * representing an immutable object. It is primarily intended for use |
26 | * with a builder class to construct its instances. |
27 | * |
28 | * @category Component |
29 | * @package Codex\Component |
30 | * @since 0.1.0 |
31 | * @author Doğu Abaris <abaris@null.net> |
32 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
33 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
34 | */ |
35 | class Accordion { |
36 | |
37 | /** |
38 | * The ID for the accordion. |
39 | */ |
40 | private string $id; |
41 | |
42 | /** |
43 | * The accordion's header title. |
44 | */ |
45 | private string $title; |
46 | |
47 | /** |
48 | * Additional text under the title. |
49 | */ |
50 | private string $description; |
51 | |
52 | /** |
53 | * The content shown when the accordion is expanded. |
54 | */ |
55 | private string $content; |
56 | |
57 | /** |
58 | * Determines if the accordion is expanded by default. |
59 | */ |
60 | private bool $isOpen; |
61 | |
62 | /** |
63 | * Additional HTML attributes for the <details> element. |
64 | */ |
65 | private array $attributes; |
66 | |
67 | /** |
68 | * The renderer instance used to render the accordion. |
69 | */ |
70 | private AccordionRenderer $renderer; |
71 | |
72 | /** |
73 | * Constructor for the Accordion component. |
74 | * |
75 | * Initializes an Accordion instance with the specified properties. |
76 | * |
77 | * @param string $id The ID for the accordion. |
78 | * @param string $title The accordion's header title. |
79 | * @param string $description Additional text under the title. |
80 | * @param string $content The content shown when the accordion is expanded. |
81 | * @param bool $isOpen Determines if the accordion is expanded by default. |
82 | * @param array $attributes Additional HTML attributes for the <details> element. |
83 | * @param AccordionRenderer $renderer The renderer to use for rendering the accordion. |
84 | */ |
85 | public function __construct( |
86 | string $id, |
87 | string $title, |
88 | string $description, |
89 | string $content, |
90 | bool $isOpen, |
91 | array $attributes, |
92 | AccordionRenderer $renderer |
93 | ) { |
94 | $this->id = $id; |
95 | $this->title = $title; |
96 | $this->description = $description; |
97 | $this->content = $content; |
98 | $this->isOpen = $isOpen; |
99 | $this->attributes = $attributes; |
100 | $this->renderer = $renderer; |
101 | } |
102 | |
103 | /** |
104 | * Get the accordion's HTML ID attribute. |
105 | * |
106 | * This method returns the ID that is assigned to the accordion element. |
107 | * The ID is useful for targeting the accordion with JavaScript, CSS, or accessibility features. |
108 | * |
109 | * @since 0.1.0 |
110 | * @return string The ID of the accordion element. |
111 | */ |
112 | public function getId(): string { |
113 | return $this->id; |
114 | } |
115 | |
116 | /** |
117 | * Get the accordion's title. |
118 | * |
119 | * This method returns the title that is displayed in the header of the accordion. |
120 | * The title is the main clickable element that users interact with to expand or collapse |
121 | * the accordion's content. |
122 | * |
123 | * @since 0.1.0 |
124 | * @return string The title of the accordion. |
125 | */ |
126 | public function getTitle(): string { |
127 | return $this->title; |
128 | } |
129 | |
130 | /** |
131 | * Get the accordion's description. |
132 | * |
133 | * This method returns the description text that appears below the title in the accordion's header. |
134 | * The description provides additional context or details about the accordion's content. |
135 | * |
136 | * @since 0.1.0 |
137 | * @return string The description of the accordion. |
138 | */ |
139 | public function getDescription(): string { |
140 | return $this->description; |
141 | } |
142 | |
143 | /** |
144 | * Get the accordion's content. |
145 | * |
146 | * This method returns the content that is displayed when the accordion is expanded. |
147 | * The content can include various HTML elements such as text, images, and more. |
148 | * |
149 | * @since 0.1.0 |
150 | * @return string The content of the accordion. |
151 | */ |
152 | public function getContent(): string { |
153 | return $this->content; |
154 | } |
155 | |
156 | /** |
157 | * Check if the accordion is open by default. |
158 | * |
159 | * This method indicates whether the accordion is set to be expanded by default when the page loads. |
160 | * If true, the accordion is displayed in an expanded state. |
161 | * |
162 | * @since 0.1.0 |
163 | * @return bool True if the accordion is open by default, false otherwise. |
164 | */ |
165 | public function isOpen(): bool { |
166 | return $this->isOpen; |
167 | } |
168 | |
169 | /** |
170 | * Retrieve additional HTML attributes for the <details> element. |
171 | * |
172 | * This method returns an array of additional HTML attributes that will be applied |
173 | * to the `<details>` element of the accordion. The attributes are properly escaped |
174 | * to ensure security and prevent XSS vulnerabilities. |
175 | * |
176 | * @since 0.1.0 |
177 | * @return array The additional attributes as an array. |
178 | */ |
179 | public function getAttributes(): array { |
180 | return $this->attributes; |
181 | } |
182 | |
183 | /** |
184 | * Get the component's HTML representation. |
185 | * |
186 | * This method generates the HTML markup for the component, incorporating relevant properties |
187 | * and any additional attributes. The component is structured using appropriate HTML elements |
188 | * as defined by the implementation. |
189 | * |
190 | * @since 0.1.0 |
191 | * @return string The generated HTML string for the component. |
192 | */ |
193 | public function getHtml(): string { |
194 | return $this->renderer->render( $this ); |
195 | } |
196 | } |