Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
AttributeResolver | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
7.18 | |
0.00% |
0 / 1 |
resolve | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
7.18 |
1 | <?php |
2 | /** |
3 | * AttributeResolver.php |
4 | * |
5 | * This file is part of the Codex design system, which provides a standardized |
6 | * approach to rendering HTML attributes. The `AttributeResolver` trait |
7 | * is responsible for converting associative arrays of HTML attributes into |
8 | * a string format suitable for use in HTML tags. |
9 | * |
10 | * @category Traits |
11 | * @package Codex\Traits |
12 | * @since 0.1.0 |
13 | * @author Doğu Abaris <abaris@null.net> |
14 | * @license https://www.gnu.org/copyleft/gpl.html GPL-2.0-or-later |
15 | * @link https://doc.wikimedia.org/codex/main/ Codex Documentation |
16 | */ |
17 | |
18 | namespace Wikimedia\Codex\Traits; |
19 | |
20 | /** |
21 | * AttributeResolver is a utility trait responsible for converting an associative |
22 | * array of HTML attributes into a string format suitable for use in HTML tags. |
23 | * |
24 | * The `AttributeResolver` trait provides a method to resolve attributes, |
25 | * handling boolean attributes and concatenating array-based attributes. |
26 | * |
27 | * @category Traits |
28 | * @package Codex\Traits |
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 | trait AttributeResolver { |
35 | |
36 | /** |
37 | * Resolves an associative array of HTML attributes into a string for an HTML tag. |
38 | * Boolean attributes (like `disabled`) are rendered without a value. |
39 | * Array-based attributes (like `class`) are concatenated into a single string. |
40 | * |
41 | * @since 0.1.0 |
42 | * @param array $attributes Key-value pairs of HTML attributes. |
43 | * @return string The attributes as a string, ready to be included in an HTML tag. |
44 | */ |
45 | public function resolve( array $attributes ): string { |
46 | // Return an empty string if there are no attributes |
47 | if ( !$attributes ) { |
48 | return ''; |
49 | } |
50 | |
51 | $resolvedAttributes = []; |
52 | |
53 | foreach ( $attributes as $key => $value ) { |
54 | |
55 | // If the value is true, include the key as an attribute without a value. |
56 | if ( $value === true ) { |
57 | $resolvedAttributes[] = $key; |
58 | } elseif ( is_array( $value ) ) { |
59 | // If the value is an array (e.g., 'data' => ['toggle' => 'modal']), flatten it into a string |
60 | $attributeValue = implode( ' ', $value ); |
61 | $resolvedAttributes[] = "$key=\"$attributeValue\""; |
62 | } elseif ( $value !== false && $value !== null ) { |
63 | // Handle other scalar values |
64 | $attributeValue = (string)$value; |
65 | $resolvedAttributes[] = "$key=\"$attributeValue\""; |
66 | } |
67 | } |
68 | |
69 | return implode( ' ', $resolvedAttributes ); |
70 | } |
71 | } |