Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
OverflowMenuItem
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 5
56
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
 parseLabel
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 8
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
 getWeight
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\DiscussionTools;
4
5use JsonSerializable;
6use MediaWiki\Language\MessageLocalizer;
7use Wikimedia\Message\MessageSpecifier;
8
9/**
10 * Item to go into the DiscussionTools overflow menu, as an OO.ui.MenuOptionWidget object.
11 *
12 * You must call parseLabel() after constructing this object and before serializing it.
13 */
14class OverflowMenuItem implements JsonSerializable {
15
16    /**
17     * @var string A rendered string to use as the label for the item.
18     */
19    private string $label;
20
21    /**
22     * @param string $id A unique identifier for the menu item, e.g. 'edit' or 'reportincident'
23     * @param string $icon An OOUI icon name.
24     * @param MessageSpecifier|string $labelMsg Message or message key to use as the label for the item.
25     *   If the message does not need params, pass a string, which will avoid parsing the message repeatedly.
26     * @param int $weight Sorting weight. Higher values will push the item further up the menu.
27     * @param array $data Data to include with the menu item. Will be accessible via getData() on the
28     *   OOUI MenuOptionWidget in client-side code.
29     */
30    public function __construct(
31        private readonly string $id,
32        private readonly string $icon,
33        private readonly MessageSpecifier|string $labelMsg,
34        private readonly int $weight = 0,
35        private readonly array $data = [],
36    ) {
37    }
38
39    public function parseLabel( MessageLocalizer $contextSource, array &$msgCache ): void {
40        $labelMsg = $this->labelMsg;
41        if ( is_string( $labelMsg ) ) {
42            if ( !isset( $msgCache[$labelMsg] ) ) {
43                $msgCache[$labelMsg] = $contextSource->msg( $labelMsg )->text();
44            }
45            $this->label = $msgCache[$labelMsg];
46        } else {
47            $this->label = $contextSource->msg( $labelMsg )->text();
48        }
49    }
50
51    public function jsonSerialize(): array {
52        $data = $this->data;
53        // Add 'id' into the 'data' array, for easier access with OOUI's getData() method
54        $data['id'] = $this->id;
55        return [
56            'id' => $this->id,
57            'data' => $data,
58            'icon' => $this->icon,
59            'label' => $this->label,
60        ];
61    }
62
63    public function getId(): string {
64        return $this->id;
65    }
66
67    public function getWeight(): int {
68        return $this->weight;
69    }
70}