Completed
Push — master ( 94aea7...5914b8 )
by Jean-Christophe
04:29
created

HtmlCollection::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 6
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 6
loc 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Ajax\common\html;
4
5
use Ajax\common\html\HtmlDoubleElement;
6
use Ajax\service\JArray;
7
use Ajax\JsUtils;
8
9
/**
10
 * Base class for Html collections
11
 * @author jc
12
 * @version 1.001
13
 */
14
abstract class HtmlCollection extends HtmlDoubleElement {
15
16
	public function __construct($identifier,$tagName="div"){
17
		parent::__construct($identifier,$tagName);
18
		$this->content=array();
19
	}
20
21 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...
22
		if(JArray::isAssociative($items)){
23
			foreach ($items as $k=>$v){
24
				$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...
25
			}
26
		}else{
27
			foreach ($items as $item){
28
				$this->addItem($item);
29
			}
30
		}
31
		return $this;
32
	}
33
34
	public function setItems($items){
35
		$this->content=$items;
36
		return $this;
37
	}
38
39
	public function getItems(){
40
		return $this->content;
41
	}
42
43
	protected function getItemToAdd($item){
44
		$itemO=$item;
45
		if($this->createCondition($item)===true){
46
			$itemO=$this->createItem($item);
47
		}
48
		return $itemO;
49
	}
50
51
	/**
52
	 * adds and returns an item
53
	 * @param HtmlDoubleElement|string $item
54
	 * @return \Ajax\common\html\HtmlDoubleElement
55
	 */
56
	public function addItem($item){
57
		$itemO=$this->getItemToAdd($item);
58
		$this->addContent($itemO);
59
		return $itemO;
60
	}
61
62
	public function insertItem($item,$position=0){
63
		$itemO=$this->getItemToAdd($item);
64
		\array_splice( $this->content, $position, 0, array($itemO));
65
		return $itemO;
66
	}
67
68
	/**
69
	 * Return the item at index
70
	 * @param int|string $index the index or the item identifier
71
	 * @return \Ajax\common\html\HtmlDoubleElement
72
	 */
73 View Code Duplication
	public function getItem($index) {
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...
74
		if (is_int($index))
75
			return $this->content[$index];
76
		else {
77
			$elm=$this->getElementById($index, $this->content);
78
			return $elm;
79
		}
80
	}
81
82
	public function setItem($index, $value) {
83
		$this->content[$index]=$value;
84
		return $this;
85
	}
86
87
	public function removeItem($index){
88
		return array_splice($this->content, $index, 1);
89
	}
90
91
	public function count(){
92
		return \sizeof($this->content);
93
	}
94
95
	/* (non-PHPdoc)
96
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
97
	 */
98
	public function fromDatabaseObject($object, $function) {
99
		return $this->addItem($function($object));
100
	}
101
102
	public function apply($callBack){
103
		foreach ($this->content as $item){
104
			$callBack($item);
105
		}
106
		return $this;
107
	}
108
109
	/*
110
	 * (non-PHPdoc)
111
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
112
	 */
113
	public function fromArray($array) {
114
		$this->addItems($array);
115
		return $this;
116
	}
117
	/**
118
	 * The item factory
119
	 * @param mixed $value
120
	 */
121
	protected abstract function createItem($value);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
122
123
	protected function createCondition($value){
124
		return \is_object($value)===false;
125
	}
126
127
	protected function contentAs($tagName){
128
		foreach ($this->content as $item){
129
			$item->setTagName($tagName);
130
		}
131
		return $this;
132
	}
133
}