HtmlForm   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 80
c 1
b 0
f 0
dl 0
loc 124
rs 10
wmc 25

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addGroup() 0 8 2
A fromArray() 0 4 3
A __construct() 0 5 1
A addElement() 0 7 2
A getId() 0 8 2
B compile() 0 41 8
A getElement() 0 5 2
A getPart() 0 8 2
A getPrefix() 0 9 3
1
<?php
2
3
namespace Ajax\bootstrap\html;
4
5
use Ajax\bootstrap\html\base\HtmlBsDoubleElement;
6
use Ajax\JsUtils;
7
8
use Ajax\common\html\html5\HtmlSelect;
9
use Ajax\service\JString;
10
11
class HtmlForm extends HtmlBsDoubleElement {
12
	protected $formElementsPrefix;
13
	protected $futureElements;
14
	protected $formGroups;
15
16
	public function __construct($identifier) {
17
		parent::__construct($identifier, "form");
18
		$this->_template='<form id="%identifier%" name="%identifier%" %properties%>%content%</form>';
19
		$this->futureElements=array ();
20
		$this->formGroups=array ();
21
	}
22
23
	/*
24
	 * (non-PHPdoc)
25
	 * @see \Ajax\bootstrap\html\base\BaseHtml::compile()
26
	 */
27
	public function compile(JsUtils $js=NULL, &$view=NULL) {
28
		if (isset($js)) {
29
			$this->formElementsPrefix=$js->config()->getVar("formElementsPrefix");
30
			foreach ( $this->futureElements as $futureElement ) {
31
				$futureElementValue=$this->getPrefix($futureElement);
32
				$futureElementValues=explode("_", $futureElementValue);
33
				switch($futureElementValues [0]) {
34
					case "input":
35
						$control=new HtmlInput($futureElement);
36
						$control->setClass("form-control");
37
						$control->setLabel($this->getPart($futureElement));
38
						break;
39
					case "checkbox":
40
						$control=new HtmlInputCheckbox($futureElement);
41
						$control->setLabel($this->getPart($futureElement), false);
42
						break;
43
					case "radio":
44
						$name=$this->getPart($futureElement);
45
						$label=$this->getPart($futureElement, 2);
46
						$control=new HtmlInputRadio($futureElement);
47
						$control->setProperty("name", strtolower($name));
48
						$control->setLabel($label, false);
49
						break;
50
					case "select":
51
						$control=new HtmlSelect($futureElement,"");
52
						$control->setProperty("size", $futureElementValues [1]);
53
						$control->setClass("form-control");
54
						break;
55
					default:
56
						$control=new HtmlInput($futureElement);
57
						$control->setClass("form-control");
58
						$control->setLabel($this->getPart($futureElement));
59
						break;
60
				}
61
				$this->addElement($control);
62
			}
63
		}
64
		foreach ( $this->formGroups as $group ) {
65
			$this->addContent($group);
66
		}
67
		return parent::compile($js, $view);
68
	}
69
70
	private function getPart($str, $part=1) {
71
		$result=preg_split('/(?=[A-Z])/', $str);
72
		if (sizeof($result)>$part) {
73
			$result=$result [$part];
74
		} else {
75
			$result=$str;
76
		}
77
		return $result;
78
	}
79
80
	protected function getId($str) {
81
		$result=preg_split('/(?=[A-Z])/', $str);
82
		if (sizeof($result)>2) {
83
			$result=$result [2];
84
		} else {
85
			$result=$str;
86
		}
87
		return $result;
88
	}
89
90
	protected function getPrefix($element) {
91
		$result="input_text";
92
		foreach ( $this->formElementsPrefix as $k => $v ) {
93
			if (JString::startswith($element, $k)) {
94
				$result=$v;
95
				break;
96
			}
97
		}
98
		return $result;
99
	}
100
101
	public function addGroup($identifier="") {
102
		if ($identifier==="")
103
			$identifier="form-".$this->identifier;
104
		$group=new HtmlBsDoubleElement($identifier);
105
		$group->setTagName("div");
106
		$group->setClass("form-group");
107
		$this->formGroups []=$group;
108
		return $group;
109
	}
110
111
	public function addElement($element) {
112
		if (sizeof($this->formGroups)===0) {
113
			$this->addGroup();
114
		}
115
		$group=$this->formGroups [sizeof($this->formGroups)-1];
116
		$group->addContent($element);
117
		return $group;
118
	}
119
120
	public function getElement($name) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

120
	public function getElement(/** @scrutinizer ignore-unused */ $name) {

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

Loading history...
121
		$element=null;
122
		foreach ( $this->formGroups as $group ) {
123
		}
124
		return $element;
125
	}
126
127
	/*
128
	 * (non-PHPdoc)
129
	 * @see \Ajax\bootstrap\html\base\HtmlSingleElement::fromArray()
130
	 */
131
	public function fromArray($array) {
132
		foreach ( $array as $value ) {
133
			if (is_string($value)) {
134
				$this->futureElements []=$value;
135
			}
136
		}
137
	}
138
}
139