Completed
Push — master ( b49918...3cf661 )
by Jean-Christophe
03:33
created

HtmlForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Ajax\semantic\html\collections\form;
4
5
use Ajax\semantic\html\base\HtmlSemCollection;
6
use Ajax\semantic\html\elements\HtmlHeader;
7
/**
8
 * Semantic Form component
9
 * @see http://semantic-ui.com/collections/form.html
10
 * @author jc
11
 * @version 1.001
12
 */
13
class HtmlForm extends HtmlSemCollection{
14
15
	use FieldsTrait;
16
17
	protected $_fields;
18
19
	public function __construct( $identifier, $elements=array()){
20
		parent::__construct( $identifier, "form", "ui form");
21
		$this->setProperty("name", $this->identifier);
22
		$this->_fields=array();
23
		$this->addItems($elements);
24
	}
25
26
	public function addHeader($title,$niveau=1,$dividing=true){
27
		$header=new HtmlHeader("",$niveau,$title);
28
		if($dividing)
29
			$header->setDividing();
30
		return $this->addItem($header);
31
	}
32
33
	public function addFields($fields,$label=NULL){
34
		if(\is_array($fields)===false){
35
			$fields = \func_get_args();
36
			$end=\end($fields);
37
			if(\is_string($end)){
38
				$label=$end;
39
				\array_pop($fields);
40
			}
41
		}
42
		$this->_fields=\array_merge($this->_fields,$fields);
43
		$field=new HtmlFormField("", new HtmlFormFields("fields-".$this->identifier."-".$this->count(),$fields),$label);
44
		return $this->addItem($field);
45
	}
46
47
	public function addItem($item){
48
		$item=parent::addItem($item);
49
		if(\is_subclass_of($item, HtmlFormField::class)===true){
50
			$this->_fields[]=$item;
51
		}
52
		return $item;
53
	}
54
55
	public function getField($index){
56
		if(\is_string($index)){
57
			$field=$this->getElementById($index, $this->_fields);
58
		}else{
59
			$field=$this->_fields[$index];
60
		}
61
		return $field;
62
	}
63
}