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

Select::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
}