Completed
Push — master ( b90c12...235574 )
by Jean-Christophe
03:37
created

FormInstanceViewer::getFieldName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
namespace Ajax\semantic\widgets\dataform;
4
5
use Ajax\semantic\widgets\base\InstanceViewer;
6
use Ajax\service\JString;
7
use Ajax\semantic\html\collections\form\HtmlFormInput;
8
9
class FormInstanceViewer extends InstanceViewer {
10
	protected $separators;
11
12
	public function __construct($instance=NULL, $captions=NULL) {
13
		parent::__construct($instance=NULL, $captions=NULL);
14
		$this->separators=[-1];
15
	}
16
17
	protected function _beforeAddProperty($index,&$field){
18
		if(JString::endswith($field, "\n")===true){
19
			$this->addSeparatorAfter($index);
20
		}
21
		if($index>1 && JString::startswith($field, "\n")===true){
22
			$this->addSeparatorAfter($index-1);
23
		}
24
		$field=\str_replace("\n", "", $field);
25
	}
26
27
	protected function _getDefaultValue($name,$value,$index){
28
		$caption=$this->getCaption($index);
29
		$input=new HtmlFormInput($name,$caption,"text",$value);
30
		return $input;
31
	}
32
33
	public function getFieldName($index){
34
		$property=$this->getProperty($index);
35
		if($property instanceof \ReflectionProperty){
36
			$result=$property->getName();
37
		}elseif(\is_callable($property)){
38
			$result=$this->visibleProperties[$index];
39
		}else{
40
			$result=\strtolower($this->getCaption($index));
41
		}
42
		return $result;
43
	}
44
45
	public function addSeparatorAfter($fieldNum){
46
		if(\array_search($fieldNum, $this->separators)===false)
47
			$this->separators[]=$fieldNum;
48
			return $this;
49
	}
50
51
	public function getSeparators() {
52
		return $this->separators;
53
	}
54
55
	public function setSeparators($separators) {
56
		$this->separators=\array_merge([-1], $separators);
57
		return $this;
58
	}
59
60
}