Completed
Push — master ( 9281ef...7089ac )
by Jean-Christophe
03:28
created

HtmlFormField   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
lcom 2
cbo 4
dl 0
loc 125
rs 10
c 1
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A addPointingLabel() 0 6 2
A setLabel() 0 9 2
A setField() 0 3 1
A getLabel() 0 4 2
A getField() 0 3 1
A swapLabel() 0 5 1
A setWidth() 0 10 3
A setError() 0 3 1
A setInline() 0 3 1
A jsState() 0 3 1
A setContainer() 0 4 1
A setReadonly() 0 3 1
A addRule() 0 10 3
A addRules() 0 6 2
A getValidation() 0 3 1
1
<?php
2
3
namespace Ajax\semantic\html\collections\form;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\base\constants\Wide;
7
use Ajax\semantic\html\base\constants\State;
8
use Ajax\semantic\html\base\constants\Direction;
9
use Ajax\semantic\html\elements\HtmlLabel;
10
use Ajax\semantic\components\validation\FieldValidation;
11
12
class HtmlFormField extends HtmlSemDoubleElement {
13
	protected $_container;
14
	protected $_validation;
15
	public function __construct($identifier, $field,$label=NULL) {
16
		parent::__construct($identifier, "div","field");
17
		$this->content=array();
18
		$this->_states=[State::ERROR,State::DISABLED];
19
		if(isset($label))
20
			$this->setLabel($label);
21
		$this->setField($field);
22
		$this->_validation=NULL;
23
	}
24
25
	public function addPointingLabel($label,$pointing=Direction::NONE){
26
		$labelO=new HtmlLabel("",$label);
27
		$labelO->setPointing($pointing);
28
		$this->addContent($labelO,$pointing==="below" || $pointing==="right");
29
		return $labelO;
30
	}
31
32
	public function setLabel($label){
33
		$labelO=$label;
34
		if(\is_string($label)){
35
			$labelO=new HtmlSemDoubleElement("","label","");
36
			$labelO->setContent($label);
37
			$labelO->setProperty("for", \str_replace("field-", "",$this->identifier));
38
		}
39
		$this->content["label"]=$labelO;
40
	}
41
42
	public function setField($field){
43
		$this->content["field"]=$field;
44
	}
45
46
	/**
47
	 * Returns the label or null
48
	 * @return mixed
49
	 */
50
	public function getLabel(){
51
		if(\array_key_exists("label", $this->content))
52
			return $this->content["label"];
53
	}
54
55
	/**
56
	 * Return the field
57
	 * @return mixed
58
	 */
59
	public function getField(){
60
		return $this->content["field"];
61
	}
62
63
	/**
64
	 * puts the label before or behind
65
	 */
66
	public function swapLabel(){
67
		$label=$this->getLabel();
68
		unset($this->content["label"]);
69
		$this->content["label"]=$label;
70
	}
71
72
	/**
73
	 * Defines the field width
74
	 * @param int $width
75
	 * @return \Ajax\semantic\html\collections\form\HtmlFormField
76
	 */
77
	public function setWidth($width){
78
		if(\is_int($width)){
79
			$width=Wide::getConstants()["W".$width];
80
		}
81
		$this->addToPropertyCtrl("class", $width, Wide::getConstants());
82
		if(isset($this->_container)){
83
			$this->_container->setEqualWidth(false);
84
		}
85
		return $this->addToPropertyCtrl("class", "wide",array("wide"));
86
	}
87
88
	/**
89
	 * Field displays an error state
90
	 * @return \Ajax\semantic\html\collections\form\HtmlFormField
91
	 */
92
	public function setError(){
93
		return $this->addToProperty("class", "error");
94
	}
95
96
	public function setInline(){
97
		return $this->addToProperty("class", "inline");
98
	}
99
100
	public function jsState($state){
101
		return $this->jsDoJquery("addClass",$state);
102
	}
103
104
	public function setContainer($_container) {
105
		$this->_container=$_container;
106
		return $this;
107
	}
108
109
	public function setReadonly(){
110
		$this->getField()->setProperty("readonly", "");
111
	}
112
113
	public function addRule($type,$prompt=NULL,$value=NULL){
114
		$field=$this->getField();
115
		if(isset($field)){
116
			if(!isset($this->_validation)){
117
				$this->_validation=new FieldValidation($field->getIdentifier());
118
			}
119
			$this->_validation->addRule($type,$prompt,$value);
120
		}
121
		return $this;
122
	}
123
124
	public function addRules(array $rules){
125
		foreach ($rules as $rule){
126
			$this->addRule($rule);
127
		}
128
		return $this;
129
	}
130
131
	public function getValidation() {
132
		return $this->_validation;
133
	}
134
135
136
}