Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.75% covered (warning)
68.75%
33 / 48
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
VectorComponentButton
68.75% covered (warning)
68.75%
33 / 48
33.33% covered (danger)
33.33%
1 / 3
23.81
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 getClasses
90.91% covered (success)
90.91%
20 / 22
0.00% covered (danger)
0.00%
0 / 1
8.05
 getTemplateData
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2namespace MediaWiki\Skins\Vector\Components;
3
4/**
5 * VectorSearchBox component
6 */
7class VectorComponentButton implements VectorComponent {
8    /** @var string */
9    private $label;
10    /** @var string|null */
11    private $icon;
12    /** @var string|null */
13    private $id;
14    /** @var string|null */
15    private $class;
16    /** @var array|null */
17    private $attributes;
18    /** @var string|null */
19    private $weight;
20    /** @var string|null */
21    private $action;
22    /** @var bool|null */
23    private $iconOnly;
24    /** @var string|null */
25    private $href;
26
27    /**
28     * @param string $label
29     * @param string|null $icon
30     * @param string|null $id
31     * @param string|null $class
32     * @param array|null $attributes
33     * @param string|null $weight
34     * @param string|null $action
35     * @param bool|null $iconOnly
36     * @param string|null $href
37     */
38    public function __construct(
39        string $label,
40        $icon = null,
41        $id = null,
42        $class = null,
43        $attributes = [],
44        $weight = 'normal',
45        $action = 'default',
46        $iconOnly = false,
47        $href = null
48    ) {
49        $this->label = $label;
50        $this->icon = $icon;
51        $this->id = $id;
52        $this->class = $class;
53        $this->attributes = $attributes;
54        $this->weight = $weight;
55        $this->action = $action;
56        $this->iconOnly = $iconOnly;
57        $this->href = $href;
58
59        // Weight can only be normal, primary, or quiet
60        if ( $this->weight != 'primary' && $this->weight != 'quiet' ) {
61            $this->weight = 'normal';
62        }
63        // Action can only be default, progressive or destructive
64        if ( $this->action != 'progressive' && $this->action != 'destructive' ) {
65            $this->action = 'default';
66        }
67    }
68
69    /**
70     * Constructs button classes based on the props
71     */
72    private function getClasses(): string {
73        $classes = 'cdx-button';
74        if ( $this->href ) {
75            $classes .= ' cdx-button--fake-button cdx-button--fake-button--enabled';
76        }
77        switch ( $this->weight ) {
78            case 'primary':
79                $classes .= ' cdx-button--weight-primary';
80                break;
81            case 'quiet':
82                $classes .= ' cdx-button--weight-quiet';
83                break;
84        }
85        switch ( $this->action ) {
86            case 'progressive':
87                $classes .= ' cdx-button--action-progressive';
88                break;
89            case 'destructive':
90                $classes .= ' cdx-button--action-destructive';
91                break;
92        }
93        if ( $this->iconOnly ) {
94            $classes .= ' cdx-button--icon-only';
95        }
96        if ( $this->class ) {
97            $classes .= ' ' . $this->class;
98        }
99        return $classes;
100    }
101
102    /**
103     * @inheritDoc
104     */
105    public function getTemplateData(): array {
106        $arrayAttributes = [];
107        foreach ( $this->attributes as $key => $value ) {
108            if ( $value === null ) {
109                continue;
110            }
111            $arrayAttributes[] = [ 'key' => $key, 'value' => $value ];
112        }
113        return [
114            'label' => $this->label,
115            'icon' => $this->icon,
116            'id' => $this->id,
117            'class' => $this->getClasses(),
118            'href' => $this->href,
119            'array-attributes' => $arrayAttributes
120        ];
121    }
122}