Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
TextWithIconWidget
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3declare( strict_types=1 );
4
5namespace MediaWiki\Extension\CampaignEvents\Widget;
6
7use InvalidArgumentException;
8use OOUI\IconElement;
9use OOUI\LabelElement;
10use OOUI\Tag;
11use OOUI\Widget;
12
13/**
14 * A widget combining an icon and some labelled information. The label is invisible and used as `title`
15 * of the icon. Somewhat similar to MessageWidget, but with different semantics.
16 * @note When using this widget, you should load the `ext.campaignEvents.TextWithIconWidget.less` stylesheet.
17 */
18class TextWithIconWidget extends Widget {
19    use IconElement;
20    use LabelElement;
21
22    /**
23     * @inheritDoc
24     * CSS classes can be added to the icon via $config['icon_classes'].
25     */
26    public function __construct( array $config = [] ) {
27        if ( !isset( $config['content'] ) || !isset( $config['label'] ) ) {
28            throw new InvalidArgumentException( 'Must specify content and label' );
29        }
30        $content = new Tag( 'span' );
31        $content->appendContent( $config['content'] );
32        $content->addClasses( [ 'ext-campaignevents-textwithicon-widget-content' ] );
33        $config['content'] = $content;
34
35        $config['invisibleLabel'] = true;
36
37        parent::__construct( $config );
38
39        $this->initializeLabelElement( $config );
40        $this->initializeIconElement( $config );
41
42        $this->getIconElement()->setAttributes( [
43            'title' => $config['label'],
44        ] )->addClasses( $config['icon_classes'] ?? [] );
45
46        $this->addClasses( [ 'ext-campaignevents-textwithicon-widget' ] );
47        // Prepending because the parent constructor has already appended the content
48        $this->prependContent( [ $this->icon, $this->label ] );
49    }
50}