Completed
Push — master ( a79a4a...ec2f1c )
by Jean-Christophe
02:56
created

FieldsTrait::addRadio()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 4
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
	public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){
44
		$field=$this->getItem($index);
0 ignored issues
show
Bug introduced by
It seems like getItem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
		if(isset($field)){
46
			$field->addRule($type,$prompt,$value);
47
		}
48
		return $this;
49
	}
50
51
	public function addFieldRules($index,$rules){
52
		$field=$this->getItem($index);
0 ignored issues
show
Bug introduced by
It seems like getItem() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
53
		if(isset($field)){
54
			$field->addRules($rules);
55
		}
56
		return $this;
57
	}
58
59
	/**
60
	 * @param string $identifier
61
	 * @param array $items
62
	 * @param string $label
63
	 * @param string $value
64
	 * @param boolean $multiple
65
	 * @return \Ajax\common\html\HtmlDoubleElement
66
	 */
67
	public function addDropdown($identifier,$items=array(), $label=NULL,$value=NULL,$multiple=false){
68
		return $this->addItem(new HtmlFormDropdown($identifier,$items,$label,$value,$multiple));
69
	}
70
71
	/**
72
	 * @param string $identifier
73
	 * @param string $label
74
	 * @param string $type
75
	 * @param string $value
76
	 * @param string $placeholder
77
	 * @return HtmlFormInput
78
	 */
79
	public function addInput($identifier, $label=NULL,$type="text",$value=NULL,$placeholder=NULL){
80
		return $this->addItem(new HtmlFormInput($identifier,$label,$type,$value,$placeholder));
81
	}
82
83
	public function addPassword($identifier, $label=NULL){
84
		return $this->addItem(new HtmlFormInput($identifier,$label,"password","",""));
85
	}
86
87
	public function addButton($identifier,$value,$cssStyle=NULL,$onClick=NULL){
88
		return $this->addItem(new HtmlButton($identifier,$value,$cssStyle,$onClick));
89
	}
90
91
	public function addCheckbox($identifier, $label=NULL,$value=NULL,$type=NULL){
92
		return $this->addItem(new HtmlFormCheckbox($identifier,$label,$value,$type));
93
	}
94
95
	public function addRadio($identifier, $name,$label=NULL,$value=NULL){
96
		return $this->addItem(new HtmlFormRadio($identifier,$name,$label,$value));
97
	}
98
}