Completed
Push — master ( d8452d...fcbb8e )
by Jean-Christophe
02:56
created

FieldsTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 7
dl 0
loc 62
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
addFields() 0 1 ?
addItem() 0 1 ?
A createItem() 0 10 3
A createCondition() 0 3 2
A addInputs() 0 10 2
A addDropdown() 0 3 1
A addInput() 0 3 1
A addPassword() 0 3 1
A addButton() 0 3 1
A addCheckbox() 0 3 1
A addRadio() 0 3 1
1
<?php
2
3
namespace Ajax\semantic\html\collections\form\traits;
4
5
use Ajax\service\JArray;
6
use Ajax\semantic\html\collections\form\HtmlFormInput;
7
use Ajax\semantic\html\collections\form\HtmlFormDropdown;
8
use Ajax\semantic\html\elements\HtmlButton;
9
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
10
use Ajax\semantic\html\collections\form\HtmlFormRadio;
11
use Ajax\semantic\html\collections\form\HtmlFormField;
12
13
trait FieldsTrait {
14
	public abstract function addFields($fields=NULL,$label=NULL);
15
	public abstract function addItem($item);
16
17
	protected function createItem($value){
18
		if(\is_array($value)){
19
			$itemO=new HtmlFormInput(JArray::getDefaultValue($value, "id",""),JArray::getDefaultValue($value, "label",null),JArray::getDefaultValue($value, "type", "text"),JArray::getDefaultValue($value, "value",""),JArray::getDefaultValue($value, "placeholder",JArray::getDefaultValue($value, "label",null)));
20
			return $itemO;
21
		}elseif(\is_object($value)){
22
			$itemO=new HtmlFormField("field-".$this->identifier, $value);
0 ignored issues
show
Bug introduced by
The property identifier does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
			return $itemO;
24
		}else
25
			return new HtmlFormInput($value);
26
	}
27
28
	protected function createCondition($value){
29
		return \is_object($value)===false || $value instanceof \Ajax\semantic\html\elements\HtmlInput;
30
	}
31
32
	public function addInputs($inputs,$fieldslabel=null){
33
		$fields=array();
34
		foreach ($inputs as $input){
35
			\extract($input);
36
			$f=new HtmlFormInput("","");
37
			$f->fromArray($input);
38
			$fields[]=$f;
39
		}
40
		return $this->addFields($fields,$fieldslabel);
41
	}
42
43
	/**
44
	 * @param string $identifier
45
	 * @param array $items
46
	 * @param string $label
47
	 * @param string $value
48
	 * @param boolean $multiple
49
	 * @return \Ajax\common\html\HtmlDoubleElement
50
	 */
51
	public function addDropdown($identifier,$items=array(), $label=NULL,$value=NULL,$multiple=false){
52
		return $this->addItem(new HtmlFormDropdown($identifier,$items,$label,$value,$multiple));
53
	}
54
55
	public function addInput($identifier, $label=NULL,$type="text",$value=NULL,$placeholder=NULL){
56
		return $this->addItem(new HtmlFormInput($identifier,$label,$type,$value,$placeholder));
57
	}
58
59
	public function addPassword($identifier, $label=NULL){
60
		return $this->addItem(new HtmlFormInput($identifier,$label,"password","",""));
61
	}
62
63
	public function addButton($identifier,$value,$cssStyle=NULL,$onClick=NULL){
64
		return $this->addItem(new HtmlButton($identifier,$value,$cssStyle,$onClick));
65
	}
66
67
	public function addCheckbox($identifier, $label=NULL,$value=NULL,$type=NULL){
68
		return $this->addItem(new HtmlFormCheckbox($identifier,$label,$value,$type));
69
	}
70
71
	public function addRadio($identifier, $name,$label=NULL,$value=NULL){
72
		return $this->addItem(new HtmlFormRadio($identifier,$name,$label,$value));
73
	}
74
}