Completed
Push — master ( fd30d0...fda645 )
by Jean-Christophe
03:40
created

FieldsTrait::addPassword()   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 2
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
use Ajax\common\html\html5\HtmlTextarea;
13
use Ajax\common\html\HtmlDoubleElement;
14
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
15
use Ajax\semantic\html\base\HtmlSemDoubleElement;
16
17
/**
18
 * @author jc
19
 * @property string $identifier
20
 */
21
trait FieldsTrait {
22
	abstract public function addFields($fields=NULL,$label=NULL);
23
	abstract public function addItem($item);
24
25
	protected function createItem($value){
26
		if(\is_array($value)){
27
			$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)));
28
			return $itemO;
29
		}elseif(\is_object($value)){
30
			$itemO=new HtmlFormField("field-".$this->identifier, $value);
31
			return $itemO;
32
		}else
33
			return new HtmlFormInput($value);
34
	}
35
36
	protected function createCondition($value){
37
		return \is_object($value)===false || $value instanceof \Ajax\semantic\html\elements\HtmlInput;
38
	}
39
40
	public function addInputs($inputs,$fieldslabel=null){
41
		$fields=array();
42
		foreach ($inputs as $input){
43
			\extract($input);
44
			$f=new HtmlFormInput("","");
45
			$f->fromArray($input);
46
			$fields[]=$f;
47
		}
48
		return $this->addFields($fields,$fieldslabel);
49
	}
50
51
	public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){
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->addRule($type,$prompt,$value);
55
		}
56
		return $this;
57
	}
58
59
	public function addFieldRules($index,$rules){
60
		$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...
61
		if(isset($field)){
62
			$field->addRules($rules);
63
		}
64
		return $this;
65
	}
66
67
	/**
68
	 * @param string $identifier
69
	 * @param array $items
70
	 * @param string $label
71
	 * @param string $value
72
	 * @param boolean $multiple
73
	 * @return HtmlDoubleElement
74
	 */
75
	public function addDropdown($identifier,$items=array(), $label=NULL,$value=NULL,$multiple=false){
76
		return $this->addItem(new HtmlFormDropdown($identifier,$items,$label,$value,$multiple));
77
	}
78
79
	/**
80
	 * @param string $identifier
81
	 * @param string $label
82
	 * @param string $type
83
	 * @param string $value
84
	 * @param string $placeholder
85
	 * @return HtmlFormInput
86
	 */
87
	public function addInput($identifier, $label=NULL,$type="text",$value=NULL,$placeholder=NULL){
88
		return $this->addItem(new HtmlFormInput($identifier,$label,$type,$value,$placeholder));
89
	}
90
91
	/**
92
	 * @param string $identifier
93
	 * @param string $label
94
	 * @param string $value
95
	 * @param string $placeholder
96
	 * @param int $rows
97
	 * @return HtmlTextarea
98
	 */
99
	public function addTextarea($identifier, $label,$value=NULL,$placeholder=NULL,$rows=5){
100
		return $this->addItem(new HtmlFormTextarea($identifier,$label,$value,$placeholder,$rows));
101
	}
102
103
	public function addPassword($identifier, $label=NULL){
104
		return $this->addItem(new HtmlFormInput($identifier,$label,"password","",""));
105
	}
106
107
	public function addButton($identifier,$value,$cssStyle=NULL,$onClick=NULL){
108
		return $this->addItem(new HtmlButton($identifier,$value,$cssStyle,$onClick));
109
	}
110
111
	public function addCheckbox($identifier, $label=NULL,$value=NULL,$type=NULL){
112
		return $this->addItem(new HtmlFormCheckbox($identifier,$label,$value,$type));
113
	}
114
115
	public function addRadio($identifier, $name,$label=NULL,$value=NULL){
116
		return $this->addItem(new HtmlFormRadio($identifier,$name,$label,$value));
117
	}
118
119
	public function addElement($identifier,$content,$label,$tagName="div",$baseClass=""){
120
		$div=new HtmlSemDoubleElement($identifier,$tagName,$baseClass,$content);
121
		return $this->addItem(new HtmlFormField("field-".$identifier, $div,$label));
122
	}
123
}
124