Completed
Push — master ( 552173...c8702c )
by Tobias
12:00 queued 43s
created

Alert::renderDismissButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 14
cts 14
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Contains the component class for rendering an alert.
4
 *
5
 * @copyright (C) 2018, Tobias Oetterer, Paderborn University
6
 * @license       https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later)
7
 *
8
 * This file is part of the MediaWiki extension BootstrapComponents.
9
 * The BootstrapComponents extension is free software: you can redistribute it
10
 * and/or modify it under the terms of the GNU General Public License as published
11
 * by the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * The BootstrapComponents extension is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 *
22
 * @file
23
 * @ingroup       BootstrapComponents
24
 * @author        Tobias Oetterer
25
 */
26
27
namespace BootstrapComponents\Components;
28
29
use BootstrapComponents\AbstractComponent;
30
use \Html;
31
32
/**
33
 * Class Alert
34
 *
35
 * Class for component 'alert'
36
 *
37
 * @see   https://github.com/oetterer/BootstrapComponents/blob/master/docs/components.md#Alert
38
 * @since 1.0
39
 */
40
class Alert extends AbstractComponent {
41
	/**
42
	 * Indicates, whether this alert is dismissible
43
	 *
44
	 * @var boolean $dismissible
45
	 */
46
	private $dismissible;
47
48
	/**
49
	 * @inheritdoc
50
	 *
51
	 * @param string $input
52
	 */
53 10
	public function placeMe( $input ) {
54
55 10
		$this->dismissible = (bool)$this->getValueFor( 'dismissible' );
56
57 10
		$class = $this->calculateAlertClassAttribute();
58 10
		$inside = $input;
59 10
		if ( $this->isDismissible() ) {
60 5
			$inside = $this->renderDismissButton() . $inside;
61 5
		}
62
63 10
		list ( $class, $style ) = $this->processCss( $class, [] );
64 10
		return Html::rawElement(
65 10
			'div',
66
			[
67 10
				'class' => $this->arrayToString( $class, ' ' ),
68 10
				'style' => $this->arrayToString( $style, ';' ),
69 10
				'id'    => $this->getId(),
70 10
				'role'  => 'alert',
71 10
			],
72
			$inside
73 10
		);
74
	}
75
76
	/**
77
	 * Calculates the class attribute value from the passed attributes
78
	 *
79
	 * @return string[]
80
	 */
81 10
	private function calculateAlertClassAttribute() {
82 10
		$class = [ 'alert' ];
83 10
		$class[] = 'alert-' . $this->getValueFor( 'color', 'info' );
84
85 10
		if ( $this->isDismissible() ) {
86 5
			if ( $this->getValueFor( 'dismissible' ) === 'fade' ) {
87 3
				$class = array_merge( $class, [ 'fade', 'in' ] );
88 3
			} else {
89 3
				$class[] = 'alert-dismissible';
90
			}
91 5
		}
92 10
		return $class;
93
	}
94
95
	/**
96
	 * Indicates, whether this alert is dismissible or not
97
	 *
98
	 * @return bool
99
	 */
100 10
	private function isDismissible() {
101 10
		return $this->dismissible;
102
	}
103
104
105
	/**
106
	 * Generates the button, that lets us dismiss this alert
107
	 *
108
	 * @return string
109
	 */
110 5
	private function renderDismissButton() {
111 5
		return Html::rawElement(
112 5
			'div',
113
			[
114 5
				'type'         => 'button',
115 5
				'class'        => 'close',
116 5
				'data-dismiss' => 'alert',
117 5
				'aria-label'   => wfMessage( 'bootstrap-components-close-element' )->inContentLanguage()->text(),
118 5
			],
119 5
			Html::rawElement(
120 5
				'span',
121
				[
122 5
					'aria-hidden' => 'true',
123 5
				],
124
				'&times;'
125 5
			)
126 5
		);
127
	}
128
}