Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
20 / 25 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| VectorComponentLink | |
80.00% |
20 / 25 |
|
50.00% |
1 / 2 |
6.29 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getTemplateData | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | namespace MediaWiki\Skins\Vector\Components; |
| 3 | |
| 4 | use MediaWiki\Html\Html; |
| 5 | use MediaWiki\Linker\Linker; |
| 6 | use MessageLocalizer; |
| 7 | |
| 8 | /** |
| 9 | * VectorComponentLink component |
| 10 | */ |
| 11 | class VectorComponentLink implements VectorComponent { |
| 12 | /** @var MessageLocalizer */ |
| 13 | private $localizer; |
| 14 | /** @var string */ |
| 15 | private $icon; |
| 16 | /** @var string */ |
| 17 | private $href; |
| 18 | /** @var string */ |
| 19 | private $text; |
| 20 | /** @var string */ |
| 21 | private $accessKeyHint; |
| 22 | |
| 23 | /** |
| 24 | * @param string $href |
| 25 | * @param string $text |
| 26 | * @param null|string $icon |
| 27 | * @param null|MessageLocalizer $localizer for generation of tooltip and access keys |
| 28 | * @param null|string $accessKeyHint will be used to derive HTML attributes such as title, accesskey |
| 29 | * and aria-label ("$accessKeyHint-label") |
| 30 | */ |
| 31 | public function __construct( string $href, string $text, $icon = null, $localizer = null, $accessKeyHint = null ) { |
| 32 | $this->href = $href; |
| 33 | $this->text = $text; |
| 34 | $this->icon = $icon; |
| 35 | $this->localizer = $localizer; |
| 36 | $this->accessKeyHint = $accessKeyHint; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @inheritDoc |
| 41 | */ |
| 42 | public function getTemplateData(): array { |
| 43 | $localizer = $this->localizer; |
| 44 | $accessKeyHint = $this->accessKeyHint; |
| 45 | $additionalAttributes = []; |
| 46 | if ( $localizer ) { |
| 47 | $msg = $localizer->msg( $accessKeyHint . '-label' ); |
| 48 | if ( $msg->exists() ) { |
| 49 | $additionalAttributes[ 'aria-label' ] = $msg->text(); |
| 50 | } |
| 51 | } |
| 52 | return [ |
| 53 | 'icon' => $this->icon, |
| 54 | 'text' => $this->text, |
| 55 | 'href' => $this->href, |
| 56 | 'html-attributes' => $localizer && $accessKeyHint ? Html::expandAttributes( |
| 57 | Linker::tooltipAndAccesskeyAttribs( |
| 58 | $accessKeyHint, |
| 59 | [], |
| 60 | [], |
| 61 | $localizer |
| 62 | ) + $additionalAttributes |
| 63 | ) : '', |
| 64 | ]; |
| 65 | } |
| 66 | } |