Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
LanguageSelectorEntry
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCSSClasses
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getComponents
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18namespace MediaWiki\Minerva\Menu\Entries;
19
20use MediaWiki\Title\Title;
21use MessageLocalizer;
22
23/**
24 * Model for a menu entry that represents a language selector for current title
25 */
26class LanguageSelectorEntry implements IMenuEntry {
27
28    private MessageLocalizer $messageLocalizer;
29    private Title $title;
30    private bool $doesPageHaveLanguages;
31    /** @var string Associated icon name */
32    private string $icon;
33    /** @var string A translatable label used as text and title */
34    private string $label;
35    /** @var string additional classes */
36    private string $classes;
37
38    /**
39     * LanguageSelectorEntry constructor.
40     * @param Title $title Current Title
41     * @param bool $doesPageHaveLanguages Whether the page is also available in other
42     * languages or variants
43     * @param MessageLocalizer $messageLocalizer Used for translation texts
44     * @param bool $isButton
45     * @param string $classes page classes
46     * @param string $label Menu entry label and title
47     */
48    public function __construct(
49        Title $title,
50        $doesPageHaveLanguages,
51        MessageLocalizer $messageLocalizer,
52        $isButton = false,
53        $classes = '',
54        $label = 'mobile-frontend-language-article-heading'
55    ) {
56        $this->title = $title;
57        $this->doesPageHaveLanguages = $doesPageHaveLanguages;
58        $this->messageLocalizer = $messageLocalizer;
59        $this->icon = $doesPageHaveLanguages ? 'language-base20' : 'language-disabled';
60        $this->label = $label;
61        $this->classes = $classes;
62    }
63
64    /**
65     * @inheritDoc
66     */
67    public function getName(): string {
68        return 'language-selector';
69    }
70
71    /**
72     * @inheritDoc
73     */
74    public function getCSSClasses(): array {
75        return [];
76    }
77
78    /**
79     * @inheritDoc
80     */
81    public function getComponents(): array {
82        $switcherLink = false;
83        $switcherClasses = ' language-selector';
84
85        if ( $this->doesPageHaveLanguages ) {
86            $switcherLink = '#p-lang';
87        } else {
88            $switcherClasses .= ' disabled';
89        }
90        $msg = $this->messageLocalizer->msg( $this->label );
91
92        return [
93            [
94                'tag-name' => 'a',
95                'classes' => $this->classes . ' ' . $switcherClasses,
96                'label' => $msg,
97                'data-icon' => [
98                    'icon' => $this->icon,
99                ],
100                'array-attributes' => [
101                    [
102                        'key' => 'href',
103                        'value' => $switcherLink,
104                    ],
105                    [
106                        'key' => 'data-mw',
107                        'value' => 'interface',
108                    ],
109                    [
110                        'key' => 'data-event-name',
111                        'value' => 'menu.languages',
112                    ],
113                    [
114                        'key' => 'title',
115                        'value' => $msg,
116                    ],
117                ],
118            ],
119        ];
120    }
121}