Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
VectorComponentVariants
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getDropdownLabel
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getDropdownData
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getMenuDropdownData
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getTemplateData
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace MediaWiki\Skins\Vector\Components;
3
4use MediaWiki\Language\Language;
5use MediaWiki\Languages\LanguageConverterFactory;
6use MediaWiki\StubObject\StubUserLang;
7
8/**
9 * VectorComponentVariants component
10 */
11class VectorComponentVariants implements VectorComponent {
12    private LanguageConverterFactory $languageConverterFactory;
13    /** @var array */
14    private $menuData;
15    /** @var Language|StubUserLang */
16    private $pageLang;
17    /** @var string */
18    private $ariaLabel;
19
20    /**
21     * @param LanguageConverterFactory $languageConverterFactory
22     * @param array $menuData
23     * @param Language|StubUserLang $pageLang
24     * @param string $ariaLabel
25     */
26    public function __construct(
27        LanguageConverterFactory $languageConverterFactory,
28        array $menuData,
29        $pageLang,
30        string $ariaLabel
31    ) {
32        $this->languageConverterFactory = $languageConverterFactory;
33        $this->menuData = $menuData;
34        $this->pageLang = $pageLang;
35        $this->ariaLabel = $ariaLabel;
36    }
37
38    /**
39     * Use the selected variant for the dropdown label
40     * @return string
41     */
42    private function getDropdownLabel(): string {
43        $converter = $this->languageConverterFactory->getLanguageConverter( $this->pageLang );
44        return $this->pageLang->getVariantname(
45            $converter->getPreferredVariant()
46        );
47    }
48
49    /**
50     * Get the variants dropdown data
51     * @return array
52     */
53    private function getDropdownData() {
54        $dropdown = new VectorComponentDropdown(
55            'vector-variants-dropdown',
56            $this->getDropdownLabel(),
57            // Hide dropdown if menu is empty
58            $this->menuData[ 'is-empty' ] ? 'emptyPortlet' : ''
59        );
60        $dropdownData = $dropdown->getTemplateData();
61        $dropdownData['aria-label'] = $this->ariaLabel;
62        return $dropdownData;
63    }
64
65    /**
66     * Get the variants menu data
67     * @return array
68     */
69    private function getMenuDropdownData() {
70        // Remove label from variants menu
71        $this->menuData['label'] = null;
72        $menu = new VectorComponentMenu( $this->menuData );
73        return $menu->getTemplateData();
74    }
75
76    /**
77     * @inheritDoc
78     */
79    public function getTemplateData(): array {
80        return [
81            'data-variants-dropdown' => $this->getDropdownData(),
82            'data-variants-menu' => $this->getMenuDropdownData()
83        ];
84    }
85}