Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 6 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
HtmlSnippetBuilder | |
0.00% |
0 / 6 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
setContent | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setAttributes | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
build | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * HtmlSnippetBuilder.php |
4 | * |
5 | * This file is part of the Codex design system, the official design system |
6 | * for Wikimedia projects. It provides the `HtmlSnippet` class for handling safe HTML content |
7 | * within the Codex design system. |
8 | * |
9 | * This class ensures that only trusted HTML content is passed and rendered directly, |
10 | * without any escaping, ensuring safe handling of HTML snippets. |
11 | * |
12 | * @category Builder |
13 | * @package Codex\Builder |
14 | * @since 0.1.0 |
15 | * @author Doğu Abaris <abaris@null.net> |
16 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
17 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
18 | */ |
19 | |
20 | namespace Wikimedia\Codex\Builder; |
21 | |
22 | use Wikimedia\Codex\Component\HtmlSnippet; |
23 | |
24 | /** |
25 | * HtmlSnippetBuilder |
26 | * |
27 | * This class implements the builder pattern to construct instances of HtmlSnippet. |
28 | * It provides a fluent interface for setting various properties and building the |
29 | * final immutable object with predefined configurations and immutability. |
30 | * |
31 | * @category Builder |
32 | * @package Codex\Builder |
33 | * @since 0.1.0 |
34 | * @author Doğu Abaris <abaris@null.net> |
35 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
36 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
37 | */ |
38 | class HtmlSnippetBuilder { |
39 | |
40 | /** |
41 | * The safe HTML content to be rendered. |
42 | */ |
43 | protected string $content; |
44 | |
45 | /** |
46 | * Additional HTML attributes for the container element. |
47 | */ |
48 | protected array $attributes = []; |
49 | |
50 | /** |
51 | * Set the HTML content. |
52 | * |
53 | * This method allows updating the HTML content of the snippet after the object has been instantiated. |
54 | * |
55 | * @since 0.1.0 |
56 | * @param string $content The new HTML content to set. |
57 | * @param-taint $content exec_html Callers are responsible for escaping. |
58 | * @return $this |
59 | */ |
60 | public function setContent( string $content ): self { |
61 | $this->content = $content; |
62 | |
63 | return $this; |
64 | } |
65 | |
66 | /** |
67 | * Set additional HTML attributes for the container element. |
68 | * |
69 | * This method allows custom HTML attributes to be added to the container element, |
70 | * such as `class`, `id`, `data-*`, or any other valid attributes. |
71 | * |
72 | * The values of these attributes are automatically escaped to prevent XSS vulnerabilities. |
73 | * |
74 | * @since 0.1.0 |
75 | * @param array $attributes An associative array of HTML attributes. |
76 | * @return $this |
77 | */ |
78 | public function setAttributes( array $attributes ): self { |
79 | foreach ( $attributes as $key => $value ) { |
80 | $this->attributes[$key] = $value; |
81 | } |
82 | return $this; |
83 | } |
84 | |
85 | /** |
86 | * Build and return the HtmlSnippet component object. |
87 | * This method constructs the immutable HtmlSnippet object with all the properties set via the builder. |
88 | * |
89 | * @since 0.1.0 |
90 | * @return HtmlSnippet The constructed HtmlSnippet. |
91 | */ |
92 | public function build(): HtmlSnippet { |
93 | return new HtmlSnippet( $this->content, $this->attributes ); |
94 | } |
95 | } |