Completed
Push — master ( b90c12...235574 )
by Jean-Christophe
03:37
created

FormFieldAsTrait::_getLabelField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Ajax\semantic\widgets\dataform;
3
use Ajax\semantic\html\elements\HtmlLabel;
4
use Ajax\semantic\html\collections\form\HtmlFormRadio;
5
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
6
use Ajax\semantic\html\collections\form\HtmlFormField;
7
use Ajax\semantic\html\collections\form\HtmlFormInput;
8
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
9
use Ajax\semantic\html\collections\form\HtmlFormDropdown;
10
11
/**
12
 * @author jc
13
 * @property FormInstanceViewer $_instanceViewer
14
 * @property object $_modelInstance;
15
 */
16
17
trait FormFieldAsTrait{
18
19
	abstract protected function _getFieldIdentifier($prefix);
20
	abstract public function setValueFunction($index,$callback);
21
22
	private function _getLabelField($caption,$icon=NULL){
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
23
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
24
		return $label;
25
	}
26
27
	/**
28
	 * @param HtmlFormField $element
29
	 * @param array $attributes
30
	 */
31
	protected function _applyAttributes($element,&$attributes,$index){
32
		if(isset($attributes["rules"])){
33
			$rules=$attributes["rules"];
34
			if(\is_array($rules))
35
				$element->addRules($rules);
36
			else
37
				$element->addRule($rules);
38
			unset($attributes["rules"]);
39
		}
40
		if(isset($attributes["callback"])){
41
			$callback=$attributes["callback"];
42
			if(\is_callable($callback)){
43
				$callback($element,$this->_modelInstance,$index);
0 ignored issues
show
Bug introduced by
The property _modelInstance does not seem to exist. Did you mean _modelInstance;?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
44
				unset($attributes["callback"]);
45
			}
46
		}
47
		$element->fromArray($attributes);
48
	}
49
50
	protected function _fieldAs($elementCallback,$index,$attributes=NULL){
51
		$this->setValueFunction($index,function($value)use ($index,&$attributes,$elementCallback){
52
			$caption=$this->_instanceViewer->getCaption($index);
53
			$name=$this->_instanceViewer->getFieldName($index);
54
			$element=$elementCallback($name,$caption,$value);
55
			if(\is_array($attributes))
56
				$this->_applyAttributes($element, $attributes,$index);
57
			return $element;
58
		});
59
			return $this;
60
	}
61
62
63
	public function fieldAsRadio($index,$attributes=NULL){
64
		return $this->_fieldAs(function($name,$caption,$value){
65
			return new HtmlFormRadio($name,$name,$caption,$value);
66
		}, $index,$attributes);
67
	}
68
69
	public function fieldAsTextarea($index,$attributes=NULL){
70
		return $this->_fieldAs(function($name,$caption,$value){
71
			return new HtmlFormTextarea($name,$caption,$value);
72
		}, $index,$attributes);
73
	}
74
75
	public function fieldAsInput($index,$attributes=NULL){
76
		return $this->_fieldAs(function($name,$caption,$value){
77
			return new HtmlFormInput($name,$caption,"text",$value);
78
		}, $index,$attributes);
79
	}
80
81
	public function fieldAsCheckbox($index,$attributes=NULL){
82
		return $this->_fieldAs(function($name,$caption,$value){
83
			return new HtmlFormCheckbox($name,$caption,$value);
84
		}, $index,$attributes);
85
	}
86
87
	public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
88
		return $this->_fieldAs(function($name,$caption,$value) use ($elements,$multiple){
89
			return new HtmlFormDropdown($name,$elements,$caption,$value,$multiple);
90
		}, $index,$attributes);
91
	}
92
}