Passed
Push — master ( 3c6756...8b3b37 )
by Michael
20:43 queued 12:59
created

SelectField::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 2
b 0
f 0
nc 2
nop 4
dl 0
loc 9
rs 10
1
<?php
2
	/**
3
	 * @package Demos
4
	 */
5
	class SelectField extends Field
6
	{
7
		public $options;
8
		
9
		function __construct($name, $options, $default = null, $hint = null)
10
		{
11
			parent::__construct($name, $default, $hint);
12
			$this->name = $name;
13
			$this->options = $options;
14
			if ($default === null)
15
				$this->default = $options[0];
16
			else
17
				$this->default = $default;
18
		}
19
		
20
		function init($request)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
21
		{
22
			$this->value = $this->default;
23
			$v = str_replace('+', ' ', $request->get($this->name));
24
			if (in_array($v, $this->options))
25
				$this->value = $v;
26
		}
27
		
28
		function renderBody($name, $id)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
29
		{
30
			echo '<select id="' . $id . '" name="' . $name . '">';
31
			foreach ($this->options as $option)
32
			{
33
				if ($this->value == $option)
34
					$sel = 'selected="selected"';
35
				else
36
					$sel = '';
37
				
38
				echo '<option ' . $sel . ' value="' . $option . '">' . $option . '</option>';
39
			}
40
			echo '</select>';
41
		}
42
	}
43