Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
27 / 30
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
MapLinkAttributeGenerator
90.00% covered (success)
90.00%
27 / 30
50.00% covered (danger)
50.00%
1 / 2
10.10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 prepareAttrs
89.66% covered (warning)
89.66%
26 / 29
0.00% covered (danger)
0.00%
0 / 1
9.09
1<?php
2
3namespace Kartographer\Tag;
4
5use FormatJson;
6use Kartographer\Special\SpecialMap;
7
8/**
9 * @license MIT
10 */
11class MapLinkAttributeGenerator {
12
13    private MapTagArgumentValidator $args;
14
15    public function __construct( MapTagArgumentValidator $args ) {
16        $this->args = $args;
17    }
18
19    /**
20     * Prepare MapLink array of attributes to be passed to the Node element
21     * @return array
22     */
23    public function prepareAttrs(): array {
24        $attrs = [
25            'class' => [ 'mw-kartographer-maplink' ],
26            // Attributes starting with "data-mw" are banned from user content in Sanitizer;
27            // we add such an attribute here (by default empty) so that its presence can be
28            // checked later to guarantee that they were generated by Kartographer
29            'data-mw-kartographer' => 'maplink',
30            'data-style' => $this->args->mapStyle,
31            'href' => SpecialMap::link(
32                    $this->args->lat,
33                    $this->args->lon,
34                    $this->args->zoom,
35                    $this->args->getLanguageCodeWithDefaultFallback()
36                )
37        ];
38
39        if ( $this->args->zoom !== null ) {
40            $attrs['data-zoom'] = (string)$this->args->zoom;
41        }
42
43        if ( $this->args->hasCoordinates() ) {
44            $attrs['data-lat'] = (string)$this->args->lat;
45            $attrs['data-lon'] = (string)$this->args->lon;
46        }
47
48        if ( $this->args->specifiedLangCode !== null ) {
49            $attrs['data-lang'] = $this->args->specifiedLangCode;
50        }
51
52        if ( $this->args->firstMarkerColor ) {
53            $attrs['class'][] = 'mw-kartographer-autostyled';
54            $attrs['style'] = "background: {$this->args->firstMarkerColor};";
55        }
56
57        if ( $this->args->cssClass !== '' ) {
58            $attrs['class'][] = $this->args->cssClass;
59        }
60
61        if ( !$this->args->hasCoordinates() && $this->args->getTextWithFallback() === null ) {
62            $attrs['class'][] = 'error';
63        }
64
65        if ( $this->args->showGroups ) {
66            $attrs['data-overlays'] = FormatJson::encode( $this->args->showGroups, false,
67                FormatJson::ALL_OK );
68        }
69
70        return $attrs;
71    }
72
73}