Completed
Push — master ( 15db70...750821 )
by Dennis
01:29
created

Select   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A render() 0 3 1
1
<?php
2
3
namespace lloc\Msls\Component\Input;
4
5
use lloc\Msls\Component\InputInterface;
6
7
class Select implements InputInterface {
8
9
	/**
10
	 * @var string
11
	 */
12
	protected $key;
13
14
	/**
15
	 * @var Group
16
	 */
17
	protected $options;
18
19
	/**
20
	 * @param string $key Name and ID of the form-element
21
	 * @param array $arr Options as associative array
22
	 * @param string|null $selected Values which should be selected
23
	 */
24
	public function __construct( string $key, array $arr, $selected = null ) {
25
		$this->key = esc_attr( $key );
26
27
		$this->options = new Group( '' );
28
		foreach ( $arr as $key => $value ) {
29
			$this->options->add( new Option( $key, $value, $selected ) );
30
		}
31
	}
32
33
	/**
34
	 * @return string
35
	 */
36
	public function render(): string {
37
		return sprintf( '<select id="%1$s" name="msls[%1$s]">%2$s</select>', $this->key, $this->options->render() );
38
	}
39
40
}