Completed
Push — master ( 71a3fd...24b2b8 )
by Jean-Christophe
03:16
created

FormInstanceViewer::setWrappers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 3
nc 1
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
	protected $headers;
12
	protected $wrappers;
13
14
	public function __construct($identifier,$instance=NULL, $captions=NULL) {
15
		parent::__construct($identifier,$instance=NULL, $captions=NULL);
16
		$this->separators=[-1];
17
		$this->headers=[];
18
		$this->wrappers=[];
19
		$this->defaultValueFunction=function($name,$value,$index){
20
			$caption=$this->getCaption($index);
21
			$input=new HtmlFormInput($this->widgetIdentifier."-".$name,$caption,"text",$value);
22
			$input->setName($name);
23
			return $input;
24
		};
25
	}
26
27
	protected function _beforeAddProperty($index,&$field){
28
		if(JString::endswith($field, "\n")===true){
29
			$this->addSeparatorAfter($index);
30
		}
31
		if($index>1 && JString::startswith($field, "\n")===true){
32
			$this->addSeparatorAfter($index-1);
33
		}
34
		$field=\str_replace("\n", "", $field);
35
		if(($header=$this->hasHeader($field))!==false){
36
			$this->addHeaderDividerBefore($index, $header);
37
		}
38
	}
39
40
	protected function hasHeader(&$field){
41
		$matches=[];$result=false;
42
		if(\preg_match('/\{(.*?)\}/s', $field, $matches)===1){
43
			$result=$matches[1];
44
			$field=\str_replace("{".$result."}","", $field);
45
		}
46
		return $result;
47
	}
48
49
50
51
	public function addSeparatorAfter($fieldNum){
52
		if(\array_search($fieldNum, $this->separators)===false)
53
			$this->separators[]=$fieldNum;
54
		return $this;
55
	}
56
57
	public function addHeaderDividerBefore($fieldNum,$header){
58
		$this->headers[$fieldNum]=$header;
59
		if($fieldNum>0)
60
			$this->addSeparatorAfter($fieldNum-1);
61
		return $this;
62
	}
63
64
	public function addWrapper($fieldNum,$contentBefore,$contentAfter=null){
65
		$this->wrappers[$fieldNum]=[$contentBefore,$contentAfter];
66
			return $this;
67
	}
68
69
	public function getSeparators() {
70
		return $this->separators;
71
	}
72
73
	public function removeSeparator($index){
74
		\array_splice($this->separators,$index,1);
75
	}
76
77
	public function removeField($index){
78
		parent::removeField($index);
79
		$pos=\array_search($index, $this->separators);
80
		if($pos!==false){
81
			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...
82
				$this->separators[$i]--;
83
			}
84
			\array_splice($this->separators, $pos, 1);
85
		}
86
		return $this;
87
	}
88
89
	public function setSeparators($separators) {
90
		$this->separators=\array_merge([-1], $separators);
91
		return $this;
92
	}
93
94
	public function getHeaders() {
95
		return $this->headers;
96
	}
97
98
	public function getWrappers() {
99
		return $this->wrappers;
100
	}
101
102
	public function setWrappers($wrappers) {
103
		$this->wrappers=$wrappers;
104
		return $this;
105
	}
106
107
108
109
}