Completed
Push — master ( 2aad8b...141d55 )
by Jean-Christophe
03:21
created

HtmlCollection::setProperties()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 9

Duplication

Lines 7
Ratio 63.64 %

Importance

Changes 0
Metric Value
dl 7
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\common\html;
4
5
use Ajax\common\html\HtmlDoubleElement;
6
use Ajax\service\JArray;
7
8
/**
9
 * Base class for Html collections
10
 * @author jc
11
 * @version 1.001
12
 */
13
abstract class HtmlCollection extends HtmlDoubleElement {
14
15
	public function __construct($identifier,$tagName="div"){
16
		parent::__construct($identifier,$tagName);
17
		$this->content=array();
18
	}
19
20 View Code Duplication
	public function addItems($items){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
		if(JArray::isAssociative($items)){
22
			foreach ($items as $k=>$v){
23
				$this->addItem([$k,$v]);
0 ignored issues
show
Documentation introduced by
array($k, $v) is of type array<integer,?,{"0":"integer|string","1":"?"}>, but the function expects a object<Ajax\common\html\HtmlDoubleElement>|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
24
			}
25
		}else{
26
			foreach ($items as $item){
27
				$this->addItem($item);
28
			}
29
		}
30
		return $this;
31
	}
32
33
	public function setItems($items){
34
		$this->content=$items;
35
		return $this;
36
	}
37
38
	public function getItems(){
39
		return $this->content;
40
	}
41
42
	protected function getItemToAdd($item){
43
		$itemO=$item;
44
		if($this->createCondition($item)===true){
45
			$itemO=$this->createItem($item);
46
		}
47
		return $itemO;
48
	}
49
50
	/**
51
	 * adds and returns an item
52
	 * @param HtmlDoubleElement|string $item
53
	 * @return \Ajax\common\html\HtmlDoubleElement
54
	 */
55
	public function addItem($item){
56
		$itemO=$this->getItemToAdd($item);
57
		$this->addContent($itemO);
58
		return $itemO;
59
	}
60
61
	public function insertItem($item,$position=0){
62
		$itemO=$this->getItemToAdd($item);
63
		\array_splice( $this->content, $position, 0, array($itemO));
64
		return $itemO;
65
	}
66
67
	/**
68
	 * Return the item at index
69
	 * @param int|string $index the index or the item identifier
70
	 * @return \Ajax\common\html\HtmlDoubleElement
71
	 */
72
	public function getItem($index) {
73
		if (is_int($index))
74
			return $this->content[$index];
75
		else {
76
			$elm=$this->getElementById($index, $this->content);
77
			return $elm;
78
		}
79
	}
80
81
	public function setItem($index, $value) {
82
		$this->content[$index]=$value;
83
		return $this;
84
	}
85
86
	public function removeItem($index){
87
		return array_splice($this->content, $index, 1);
88
	}
89
90
	public function count(){
91
		return \sizeof($this->content);
92
	}
93
94
	/* (non-PHPdoc)
95
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
96
	 */
97
	public function fromDatabaseObject($object, $function) {
98
		return $this->addItem($function($object));
99
	}
100
101
	public function apply($callBack){
102
		foreach ($this->content as $item){
103
			$callBack($item);
104
		}
105
		return $this;
106
	}
107
108
	/*
109
	 * (non-PHPdoc)
110
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
111
	 */
112
	public function fromArray($array) {
113
		$this->addItems($array);
114
		return $this;
115
	}
116
	/**
117
	 * The item factory
118
	 * @param mixed $value
119
	 */
120
	protected abstract function createItem($value);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
121
122
	protected function createCondition($value){
123
		return \is_object($value)===false;
124
	}
125
126
	protected function contentAs($tagName){
127
		foreach ($this->content as $item){
128
			$item->setTagName($tagName);
129
		}
130
		return $this;
131
	}
132
133
	public function setProperties($properties){
134
		$i=0;
135 View Code Duplication
		foreach ($properties as $k=>$v){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
			$c=$this->content[$i++];
137
			if(isset($c))
138
				$c->setProperty($k,$v);
139
			else
140
				return $this;
141
		}
142
		return $this;
143
	}
144
145
	public function setPropertyValues($property,$values){
146
		$i=0;
147
		if(\is_array($values)===false){
148
			$values=\array_fill(0, $this->count(),$values);
149
		}
150 View Code Duplication
		foreach ($values as $value){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
			$c=$this->content[$i++];
152
			if(isset($c)===true){
153
				$c->setProperty($property,$value);
154
			}
155
			else{
156
				return $this;
157
			}
158
		}
159
		return $this;
160
	}
161
}