Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
InfoChip
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 11
272
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
 getText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStatus
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getIcon
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
 setText
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 setStatus
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 setIcon
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
 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 * InfoChip.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 `InfoChip` 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 InvalidArgumentException;
22use Wikimedia\Codex\Contract\Component;
23use Wikimedia\Codex\Renderer\InfoChipRenderer;
24
25/**
26 * InfoChip
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 */
35class InfoChip extends Component {
36    private string $id = '';
37    /**
38     * Allowed values for the status type.
39     */
40    public const ALLOWED_STATUS_TYPES = [
41        'notice',
42        'warning',
43        'error',
44        'success',
45    ];
46
47    public function __construct(
48        InfoChipRenderer $renderer,
49        private string|HtmlSnippet $text,
50        private string $status,
51        private ?string $icon,
52        private array $attributes
53    ) {
54        parent::__construct( $renderer );
55    }
56
57    /**
58     * Get the InfoChip's HTML ID attribute.
59     *
60     * This method returns the ID assigned to the InfoChip element.
61     * The ID can be used for targeting the chip with JavaScript, CSS, or for accessibility purposes.
62     *
63     * @since 0.1.0
64     * @return string The ID of the InfoChip element.
65     */
66    public function getId(): string {
67        return $this->id;
68    }
69
70    /**
71     * Get the text content of the info chip.
72     *
73     * This method returns the text displayed inside the info chip.
74     * The text provides the primary information that the chip conveys.
75     *
76     * @since 0.1.0
77     * @return string|HtmlSnippet The text content of the info chip.
78     */
79    public function getText(): string|HtmlSnippet {
80        return $this->text;
81    }
82
83    /**
84     * Get the status type of the info chip.
85     *
86     * This method returns the status type of the info chip, which determines its visual style.
87     * The status can be one of the following: 'notice', 'warning', 'error', or 'success'.
88     *
89     * @since 0.1.0
90     * @return string The status type of the info chip.
91     */
92    public function getStatus(): string {
93        return $this->status;
94    }
95
96    /**
97     * Get the custom icon class for the info chip.
98     *
99     * This method returns the CSS class for a custom icon used in the info chip, if applicable.
100     * This option is only available for chips with the "notice" status.
101     *
102     * @since 0.1.0
103     * @return string|null The CSS class for the custom icon, or null if no icon is set.
104     */
105    public function getIcon(): ?string {
106        return $this->icon;
107    }
108
109    /**
110     * Retrieve additional HTML attributes for the outer `<div>` element.
111     *
112     * This method returns an associative array of additional HTML attributes that will be applied
113     * to the outer `<div>` element of the info chip. These attributes can be used to improve
114     * accessibility, customization, or to integrate with JavaScript.
115     *
116     * @since 0.1.0
117     * @return array The additional attributes as an array.
118     */
119    public function getAttributes(): array {
120        return $this->attributes;
121    }
122
123    /**
124     * Set the InfoChip's HTML ID attribute.
125     *
126     * @deprecated Use setAttributes() to set the ID
127     * @since 0.1.0
128     * @param string $id The ID for the InfoChip element.
129     * @return $this
130     */
131    public function setId( string $id ): self {
132        $this->id = $id;
133
134        return $this;
135    }
136
137    /**
138     * Set the text content for the info chip.
139     *
140     * This method specifies the text that will be displayed inside the info chip.
141     * The text provides the primary information that the chip conveys.
142     *
143     * @since 0.1.0
144     * @param string|HtmlSnippet $text The text to be displayed inside the info chip.
145     * @return $this Returns the InfoChip instance for method chaining.
146     */
147    public function setText( string|HtmlSnippet $text ): self {
148        $this->text = $text;
149
150        return $this;
151    }
152
153    /**
154     * Set the status type for the info chip.
155     *
156     * This method sets the visual style of the info chip based on its status.
157     * The status can be one of the following:
158     * - 'notice': For general information.
159     * - 'warning': For cautionary information.
160     * - 'error': For error messages.
161     * - 'success': For success messages.
162     *
163     * The status type is applied as a CSS class (`cdx-info-chip--{status}`) to the chip element.
164     *
165     * @since 0.1.0
166     * @param string $status The status type (e.g., 'notice', 'warning', 'error', 'success').
167     * @return $this Returns the InfoChip instance for method chaining.
168     */
169    public function setStatus( string $status ): self {
170        if ( !in_array( $status, self::ALLOWED_STATUS_TYPES, true ) ) {
171            throw new InvalidArgumentException( "Invalid status: $status" );
172        }
173        $this->status = $status;
174
175        return $this;
176    }
177
178    /**
179     * Set a custom icon for the "notice" status chip.
180     *
181     * This method specifies a CSS class for a custom icon to be displayed inside the chip.
182     * This option is applicable only for chips with the "notice" status.
183     * Chips with other status types (warning, error, success) do not support custom icons and will ignore this setting.
184     *
185     * @since 0.1.0
186     * @param string|null $icon The CSS class for the custom icon, or null to remove the icon.
187     * @return $this Returns the InfoChip instance for method chaining.
188     */
189    public function setIcon( ?string $icon ): self {
190        if ( $this->status === 'notice' && ( $icon !== null && trim( $icon ) === '' ) ) {
191            throw new InvalidArgumentException( 'Custom icons are only allowed for "notice" status.' );
192        }
193        $this->icon = $icon;
194
195        return $this;
196    }
197
198    /**
199     * Set additional HTML attributes for the outer `<div>` element.
200     *
201     * This method allows custom HTML attributes to be added to the outer `<div>` element of the info chip,
202     * such as `id`, `data-*`, `aria-*`, or any other valid attributes. These attributes can be used to
203     * enhance accessibility or integrate with JavaScript.
204     *
205     * The values of these attributes are automatically escaped to prevent XSS vulnerabilities.
206     *
207     * Example usage:
208     *
209     *     $infoChip->setAttributes([
210     *         'id' => 'info-chip-example',
211     *         'data-category' => 'info',
212     *     ]);
213     *
214     * @since 0.1.0
215     * @param array $attributes An associative array of HTML attributes.
216     * @return $this Returns the InfoChip instance for method chaining.
217     */
218    public function setAttributes( array $attributes ): self {
219        foreach ( $attributes as $key => $value ) {
220            $this->attributes[$key] = $value;
221        }
222        return $this;
223    }
224}