Completed
Push — master ( abc246...138cdf )
by Jean-Christophe
03:36
created

FormInstanceViewer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 58
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A _beforeAddProperty() 0 9 4
A addSeparatorAfter() 0 5 2
A getSeparators() 0 3 1
A removeSeparator() 0 3 1
A removeField() 0 11 3
A setSeparators() 0 4 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($identifier,$instance=NULL, $captions=NULL) {
13
		parent::__construct($identifier,$instance=NULL, $captions=NULL);
14
		$this->separators=[-1];
15
		$this->defaultValueFunction=function($name,$value,$index){
16
			$caption=$this->getCaption($index);
17
			$input=new HtmlFormInput($this->widgetIdentifier."-".$name,$caption,"text",$value);
18
			$input->setName($name);
19
			return $input;
20
		};
21
	}
22
23
	protected function _beforeAddProperty($index,&$field){
24
		if(JString::endswith($field, "\n")===true){
25
			$this->addSeparatorAfter($index);
26
		}
27
		if($index>1 && JString::startswith($field, "\n")===true){
28
			$this->addSeparatorAfter($index-1);
29
		}
30
		$field=\str_replace("\n", "", $field);
31
	}
32
33
34
35
	public function addSeparatorAfter($fieldNum){
36
		if(\array_search($fieldNum, $this->separators)===false)
37
			$this->separators[]=$fieldNum;
38
		return $this;
39
	}
40
41
	public function getSeparators() {
42
		return $this->separators;
43
	}
44
45
	public function removeSeparator($index){
46
		\array_splice($this->separators,$index,1);
47
	}
48
49
	public function removeField($index){
50
		parent::removeField($index);
51
		$pos=\array_search($index, $this->separators);
52
		if($pos!==false){
53
			for($i=$pos+1;$i<\sizeof($this->separators);$i++){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
54
				$this->separators[$i]--;
55
			}
56
			\array_splice($this->separators, $pos, 1);
57
		}
58
		return $this;
59
	}
60
61
	public function setSeparators($separators) {
62
		$this->separators=\array_merge([-1], $separators);
63
		return $this;
64
	}
65
66
}