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

SelectField   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 36
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A renderBody() 0 13 3
A init() 0 6 2
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