Completed
Push — master ( 3cf661...c0f044 )
by Jean-Christophe
03:28
created

HtmlCollection::createItem()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 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
	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 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...
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
		$this->addItem($function($object));
99
	}
100
101
	public function apply($callBack){
102
		foreach ($this->content as $item){
103
			$callBack($item);
104
		}
105
	}
106
107
	/*
108
	 * (non-PHPdoc)
109
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
110
	 */
111
	public function fromArray($array) {
112
		$this->addItems($array);
113
	}
114
	/**
115
	 * The item factory
116
	 * @param string|HtmlDoubleElement $value
117
	 */
118
	protected abstract function createItem($value);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
119
120
	protected function createCondition($value){
121
		return \is_object($value)===false;
122
	}
123
124
	/*
125
	 * (non-PHPdoc)
126
	 * @see \Ajax\bootstrap\html\base\HtmlSingleElement::run()
127
	 */
128 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...
129
		parent::run($js);
130
		$this->_bsComponent=$js->bootstrap()->generic("#".$this->identifier);
131
		$this->addEventsOnRun($js);
132
		return $this->_bsComponent;
133
	}
134
135
	protected function contentAs($tagName){
136
		foreach ($this->content as $item){
137
			$item->setTagName($tagName);
138
		}
139
	}
140
}