Completed
Push — master ( 72aa1a...163c98 )
by Jean-Christophe
03:34
created

FieldAsTrait::fieldAsDropDown()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
nc 1
cc 2
eloc 8
nop 4
1
<?php
2
namespace Ajax\semantic\widgets\base;
3
use Ajax\semantic\html\elements\HtmlInput;
4
use Ajax\semantic\html\modules\checkbox\HtmlCheckbox;
5
use Ajax\service\JString;
6
use Ajax\semantic\html\modules\HtmlDropdown;
7
use Ajax\semantic\html\elements\HtmlImage;
8
use Ajax\semantic\html\modules\checkbox\HtmlRadio;
9
use Ajax\semantic\html\base\constants\Size;
10
use Ajax\semantic\html\elements\HtmlLabel;
11
use Ajax\semantic\html\modules\HtmlProgress;
12
13
/**
14
 * @author jc
15
 * @property InstanceViewer $_instanceViewer
16
 */
17
18
trait FieldAsTrait{
19
20
	protected abstract function _getFieldIdentifier($prefix);
21
	public abstract function setValueFunction($index,$callback);
22
23
	private function _getLabelField($caption,$icon=NULL){
24
		$label=new HtmlLabel($this->_getFieldIdentifier("lbl"),$caption,$icon);
25
		return $label;
26
	}
27
28
29
	public function fieldAsProgress($index,$label=NULL, $attributes=array()){
30
		$this->setValueFunction($index,function($value) use($label,$attributes){
31
			$pb=new HtmlProgress($this->_getFieldIdentifier("pb"),$value,$label,$attributes);
32
			return $pb;
33
		});
34
			return $this;
35
	}
36
37
	public function fieldAsLabel($index,$icon=NULL){
38
		$this->setValueFunction($index,function($caption) use($icon){
39
			$lbl=$this->_getLabelField($caption,$icon);
40
			return $lbl;
41
		});
42
			return $this;
43
	}
44
45
	public function fieldAsImage($index,$size=Size::SMALL,$circular=false){
46
		$this->setValueFunction($index,function($img) use($size,$circular){
47
			$image=new HtmlImage($this->_getFieldIdentifier("image"),$img);$image->setSize($size);if($circular)$image->setCircular();
48
			return $image;
49
		});
50
			return $this;
51
	}
52
53
	public function fieldAsAvatar($index){
54
		$this->setValueFunction($index,function($img){return (new HtmlImage("",$img))->asAvatar();});
55
		return $this;
56
	}
57
58
	public function fieldAsRadio($index,$name=NULL){
59
		$this->setValueFunction($index,function($value)use ($index,$name){
60
			if(isset($name)===false){
61
				$name=$this->_instanceViewer->getCaption($index)."[]";
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $name, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
62
			}
63
			$radio=new HtmlRadio($this->_getFieldIdentifier("radio"),$name,$value,$value);
64
			return $radio;
65
		});
66
			return $this;
67
	}
68
69 View Code Duplication
	public function fieldAsInput($index,$name=NULL,$type="text",$placeholder=""){
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...
70
		$this->setValueFunction($index,function($value) use($index,$name,$type,$placeholder){
71
			$input=new HtmlInput($this->_getFieldIdentifier("input"),$type,$value,$placeholder);
72
			if(isset($name)===false){
73
				$name=$this->_instanceViewer->getCaption($index)."[]";
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $name, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
74
			}
75
			$input->getField()->setProperty("name", $name);
76
			return $input;
77
		});
78
			return $this;
79
	}
80
81 View Code Duplication
	public function fieldAsCheckbox($index,$name=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...
82
		$this->setValueFunction($index,function($value) use($index,$name){
83
			$checkbox=new HtmlCheckbox($this->_getFieldIdentifier("ck"),"",$value);
84
			$checkbox->setChecked(JString::isBooleanTrue($value));
85
			if(isset($name)===false){
86
				$name=$this->_instanceViewer->getCaption($index)."[]";
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $name, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
87
			}
88
			$checkbox->getField()->setProperty("name", $name);
89
			return $checkbox;
90
		});
91
			return $this;
92
	}
93
94
	public function fieldAsDropDown($index,$elements=[],$multiple=false,$name=NULL){
95
		$this->setValueFunction($index,function($value) use($index,$elements,$multiple,$name){
96
			$dd=new HtmlDropdown($this->_getFieldIdentifier("dd"),$value,$elements);
97
			if(isset($name)===false){
98
				$name=$this->_instanceViewer->getCaption($index)."[]";
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $name, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
99
			}
100
			$dd->asSelect($name,$multiple);
101
			return $dd;
102
		});
103
			return $this;
104
	}
105
}