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 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
OverflowMenuItem
0.00% covered (danger)
0.00%
0 / 21
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 / 5
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 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    private string $icon;
22    private MessageSpecifier|string $labelMsg;
23    private array $data;
24    private string $id;
25    private int $weight;
26
27    /**
28     * @param string $id A unique identifier for the menu item, e.g. 'edit' or 'reportincident'
29     * @param string $icon An OOUI icon name.
30     * @param MessageSpecifier|string $labelMsg Message or message key to use as the label for the item.
31     *   If the message does not need params, pass a string, which will avoid parsing the message repeatedly.
32     * @param int $weight Sorting weight. Higher values will push the item further up the menu.
33     * @param array $data Data to include with the menu item. Will be accessible via getData() on the
34     *   OOUI MenuOptionWidget in client-side code.
35     */
36    public function __construct(
37        string $id,
38        string $icon,
39        MessageSpecifier|string $labelMsg,
40        int $weight = 0,
41        array $data = []
42    ) {
43        $this->id = $id;
44        $this->icon = $icon;
45        $this->labelMsg = $labelMsg;
46        $this->weight = $weight;
47        $this->data = $data;
48    }
49
50    public function parseLabel( MessageLocalizer $contextSource, array &$msgCache ): void {
51        $labelMsg = $this->labelMsg;
52        if ( is_string( $labelMsg ) ) {
53            if ( !isset( $msgCache[$labelMsg] ) ) {
54                $msgCache[$labelMsg] = $contextSource->msg( $labelMsg )->text();
55            }
56            $this->label = $msgCache[$labelMsg];
57        } else {
58            $this->label = $contextSource->msg( $labelMsg )->text();
59        }
60    }
61
62    public function jsonSerialize(): array {
63        $data = $this->data;
64        // Add 'id' into the 'data' array, for easier access with OOUI's getData() method
65        $data['id'] = $this->id;
66        return [
67            'id' => $this->id,
68            'data' => $data,
69            'icon' => $this->icon,
70            'label' => $this->label,
71        ];
72    }
73
74    public function getId(): string {
75        return $this->id;
76    }
77
78    public function getWeight(): int {
79        return $this->weight;
80    }
81}