Passed
Branch tests1.5 (af713c)
by Wanderson
01:19
created

Select::selected()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Select
5
 * Auxilia ao utilizar <select>
6
 *
7
 */
8
9
namespace Win\Html\Form;
10
11
class Select {
12
13
	protected $options;
14
	private $current;
15
16
	/**
17
	 * Retorna "selected" se os valores são iguais
18
	 * @param mixed $value1
19
	 * @param mixed $value2
20
	 * @return string
21
	 */
22
	public static function selected($value1, $value2 = true) {
23
		return ($value1 == $value2) ? 'selected' : '';
24
	}
25
26
	/**
27
	 * Cria um <select> com <options>, selecionando automático
28
	 * @param string[] $options
29
	 * @param string $current
30
	 */
31
	public function __construct($options, $current = '') {
32
		$this->options = $options;
33
		$this->current = $current;
34
	}
35
36
	/**
37
	 * Exibe os <options> do <select>
38
	 * @return string
39
	 */
40
	public function __toString() {
41
		$html = '';
42
		foreach ($this->options as $option):
43
			$html .= '<option ' . static::selected($option, $this->current) . ' value="' . $option . '">' . $option . '</option>';
44
		endforeach;
45
		return $html;
46
	}
47
48
}
49