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

FormInstanceViewer   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B _beforeAddProperty() 0 12 5
A hasHeader() 0 8 2
A addSeparatorAfter() 0 5 2
A addHeaderDividerBefore() 0 6 2
A getSeparators() 0 3 1
A removeSeparator() 0 3 1
A removeField() 0 11 3
A setSeparators() 0 4 1
A getHeaders() 0 3 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
	protected $headers;
12
13
	public function __construct($identifier,$instance=NULL, $captions=NULL) {
14
		parent::__construct($identifier,$instance=NULL, $captions=NULL);
15
		$this->separators=[-1];
16
		$this->headers=[];
17
		$this->defaultValueFunction=function($name,$value,$index){
18
			$caption=$this->getCaption($index);
19
			$input=new HtmlFormInput($this->widgetIdentifier."-".$name,$caption,"text",$value);
20
			$input->setName($name);
21
			return $input;
22
		};
23
	}
24
25
	protected function _beforeAddProperty($index,&$field){
26
		if(JString::endswith($field, "\n")===true){
27
			$this->addSeparatorAfter($index);
28
		}
29
		if($index>1 && JString::startswith($field, "\n")===true){
30
			$this->addSeparatorAfter($index-1);
31
		}
32
		$field=\str_replace("\n", "", $field);
33
		if(($header=$this->hasHeader($field))!==false){
34
			$this->addHeaderDividerBefore($index, $header);
35
		}
36
	}
37
38
	protected function hasHeader(&$field){
39
		$matches=[];$result=false;
40
		if(\preg_match('/\{(.*?)\}/s', $field, $matches)===1){
41
			$result=$matches[1];
42
			$field=\str_replace("{".$result."}","", $field);
43
		}
44
		return $result;
45
	}
46
47
48
49
	public function addSeparatorAfter($fieldNum){
50
		if(\array_search($fieldNum, $this->separators)===false)
51
			$this->separators[]=$fieldNum;
52
		return $this;
53
	}
54
55
	public function addHeaderDividerBefore($fieldNum,$header){
56
		$this->headers[$fieldNum]=$header;
57
		if($fieldNum>0)
58
			$this->addSeparatorAfter($fieldNum-1);
59
		return $this;
60
	}
61
62
	public function getSeparators() {
63
		return $this->separators;
64
	}
65
66
	public function removeSeparator($index){
67
		\array_splice($this->separators,$index,1);
68
	}
69
70
	public function removeField($index){
71
		parent::removeField($index);
72
		$pos=\array_search($index, $this->separators);
73
		if($pos!==false){
74
			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...
75
				$this->separators[$i]--;
76
			}
77
			\array_splice($this->separators, $pos, 1);
78
		}
79
		return $this;
80
	}
81
82
	public function setSeparators($separators) {
83
		$this->separators=\array_merge([-1], $separators);
84
		return $this;
85
	}
86
87
	public function getHeaders() {
88
		return $this->headers;
89
	}
90
91
92
}