Completed
Push — master ( 424d4e...c1a616 )
by Jean-Christophe
03:24
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
	}
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
	/**
43
	 * adds and returns an item
44
	 * @param HtmlDoubleElement|string $item
45
	 * @return \Ajax\common\html\HtmlDoubleElement
46
	 */
47
	public function addItem($item){
48
		$itemO=$item;
49
		if($this->createCondition($item)===true){
50
			$itemO=$this->createItem($item);
51
		}
52
		$this->addContent($itemO);
53
		return $itemO;
54
	}
55
56
	/**
57
	 * Return the item at index
58
	 * @param int|string $index the index or the item identifier
59
	 * @return \Ajax\common\html\HtmlDoubleElement
60
	 */
61 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...
62
		if (is_int($index))
63
			return $this->content[$index];
64
		else {
65
			$elm=$this->getElementById($index, $this->content);
66
			return $elm;
67
		}
68
	}
69
70
	public function setItem($index, $value) {
71
		$this->content[$index]=$value;
72
		return $this;
73
	}
74
75
	public function removeItem($index){
76
		return array_splice($this->content, $index, 1);
77
	}
78
79
	public function count(){
80
		return \sizeof($this->content);
81
	}
82
83
	/* (non-PHPdoc)
84
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
85
	 */
86
	public function fromDatabaseObject($object, $function) {
87
		$this->addItem($function($object));
88
	}
89
90
	public function apply($callBack){
91
		foreach ($this->content as $item){
92
			$callBack($item);
93
		}
94
	}
95
96
	/*
97
	 * (non-PHPdoc)
98
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
99
	 */
100
	public function fromArray($array) {
101
		$this->addItems($array);
102
	}
103
	/**
104
	 * The item factory
105
	 * @param string|HtmlDoubleElement $value
106
	 */
107
	protected abstract function createItem($value);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
108
109
	protected function createCondition($value){
110
		return \is_object($value)===false;
111
	}
112
113
	/*
114
	 * (non-PHPdoc)
115
	 * @see \Ajax\bootstrap\html\base\HtmlSingleElement::run()
116
	 */
117 View Code Duplication
	public function run(JsUtils $js) {
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...
118
		parent::run($js);
119
		$this->_bsComponent=$js->bootstrap()->generic("#".$this->identifier);
120
		$this->addEventsOnRun($js);
121
		return $this->_bsComponent;
122
	}
123
124
	protected function contentAs($tagName){
125
		foreach ($this->content as $item){
126
			$item->setTagName($tagName);
127
		}
128
	}
129
}