Completed
Push — master ( 22ba15...552305 )
by Jean-Christophe
03:10
created

FormFieldAsTrait::fieldAsRadios()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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
use Ajax\semantic\html\collections\form\HtmlFormFields;
11
use Ajax\semantic\html\collections\HtmlMessage;
12
13
/**
14
 * @author jc
15
 * @property FormInstanceViewer $_instanceViewer
16
 * @property object $_modelInstance;
17
 */
18
19
trait FormFieldAsTrait{
20
21
	abstract protected function _getFieldIdentifier($prefix);
22
	abstract public function setValueFunction($index,$callback);
23
24
	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...
25
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
26
		return $label;
27
	}
28
29
	/**
30
	 * @param HtmlFormField $element
31
	 * @param array $attributes
32
	 */
33
	protected function _applyAttributes($element,&$attributes,$index){
34
		$this->_addRules($element, $attributes);
35
		if(isset($attributes["callback"])){
36
			$callback=$attributes["callback"];
37
			if(\is_callable($callback)){
38
				$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...
39
				unset($attributes["callback"]);
40
			}
41
		}
42
		$element->fromArray($attributes);
43
	}
44
45
	protected function _addRules($element,$attributes){
46
		if(isset($attributes["rules"])){
47
			$rules=$attributes["rules"];
48
			if(\is_array($rules))
49
				$element->addRules($rules);
50
				else
51
					$element->addRule($rules);
52
				unset($attributes["rules"]);
53
		}
54
	}
55
56
	protected function _fieldAs($elementCallback,$index,$attributes=NULL,$identifier=null){
0 ignored issues
show
Unused Code introduced by
The parameter $identifier is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
		$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback){
58
			$caption=$this->_instanceViewer->getCaption($index);
59
			$name=$this->_instanceViewer->getFieldName($index);
60
			$element=$elementCallback($this->getIdentifier()."-".$name,$name,$value,$caption);
0 ignored issues
show
Bug introduced by
It seems like getIdentifier() 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(\is_array($attributes))
62
				$this->_applyAttributes($element, $attributes,$index);
63
			return $element;
64
		});
65
			return $this;
66
	}
67
68
69
	public function fieldAsRadio($index,$attributes=NULL){
70
		return $this->_fieldAs(function($id,$name,$value,$caption){
71
			return new HtmlFormRadio($id,$name,$caption,$value);
72
		}, $index,$attributes);
73
	}
74
75
	public function fieldAsRadios($index,$elements=[],$attributes=NULL){
0 ignored issues
show
Unused Code introduced by
The parameter $elements is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
		return $this->_fieldAs(function($id,$name,$value,$caption,$elements){
77
			return HtmlFormFields::radios($name,$elements,$caption,$value);
78
		}, $index,$attributes);
79
	}
80
81
	public function fieldAsTextarea($index,$attributes=NULL){
82
		return $this->_fieldAs(function($id,$name,$value,$caption){
83
			$textarea=new HtmlFormTextarea($id,$caption,$value);
84
			$textarea->setName($name);
85
			return $textarea;
86
		}, $index,$attributes);
87
	}
88
89 View Code Duplication
	public function fieldAsInput($index,$attributes=NULL){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
		return $this->_fieldAs(function($id,$name,$value,$caption){
91
			$input= new HtmlFormInput($id,$caption,"text",$value);
92
			$input->setName($name);
93
			return $input;
94
		}, $index,$attributes);
95
	}
96
97
	public function fieldAsCheckbox($index,$attributes=NULL){
98
		return $this->_fieldAs(function($id,$name,$value,$caption){
99
			return new HtmlFormCheckbox($id,$caption,$value);
100
		}, $index,$attributes);
101
	}
102
103 View Code Duplication
	public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=NULL){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($elements,$multiple){
105
			$dd=new HtmlFormDropdown($id,$elements,$caption,$value,$multiple);
106
			$dd->setName($name);
107
			return $dd;
108
		}, $index,$attributes);
109
	}
110
111
	public function fieldAsMessage($index,$attributes=NULL){
112
		return $this->_fieldAs(function($id,$name,$value,$caption){
113
			$mess= new HtmlMessage($id,$value);
114
			$mess->addHeader($caption);
115
			return $mess;
116
		}, $index,$attributes,"message");
117
	}
118
}