Completed
Push — master ( 195df7...541276 )
by Jean-Christophe
03:05
created

HtmlCollection::fromDatabaseObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Ajax\common\html\html5;
4
5
use Ajax\common\html\HtmlDoubleElement;
6
7
abstract class HtmlCollection extends HtmlDoubleElement {
8
9
	public function __construct($identifier,$tagName="div"){
10
		parent::__construct($identifier,$tagName);
11
		$this->content=array();
12
	}
13
14
	public function addItems($items){
15
		foreach ($items as $item){
16
			$this->addItem($item);
17
		}
18
	}
19
20
	public function setItems($items){
21
		$this->content=$items;
22
		return $this;
23
	}
24
25
	public function getItems(){
26
		return $this->content;
27
	}
28
29
	/**
30
	 * adds and returns an item
31
	 * @param HtmlDoubleElement|string $item
32
	 * @return \Ajax\common\html\HtmlDoubleElement
33
	 */
34
	public function addItem($item){
35
		$itemO=$item;
36
		if(\is_string($item)){
37
			$itemO=$this->createItem($item);
38
		}
39
		$this->addContent($itemO);
40
		return $itemO;
41
	}
42
43
	/**
44
	 * Return the item at index
45
	 * @param int|string $index the index or the item identifier
46
	 * @return \Ajax\common\html\HtmlDoubleElement
47
	 */
48 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...
49
		if (is_int($index))
50
			return $this->content[$index];
51
		else {
52
			$elm=$this->getElementById($index, $this->content);
53
			return $elm;
54
		}
55
	}
56
57
	public function setItem($index, $value) {
58
		$this->content[$index]=$value;
59
		return $this;
60
	}
61
62
	public function count(){
63
		return \sizeof($this->content);
64
	}
65
66
	/* (non-PHPdoc)
67
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
68
	 */
69
	public function fromDatabaseObject($object, $function) {
70
		$this->addItems($function($object));
71
	}
72
73
	/*
74
	 * (non-PHPdoc)
75
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
76
	 */
77
	public function fromArray($array) {
78
		$this->addItems($array);
79
	}
80
	/**
81
	 * The item factory
82
	 * @param string|HtmlDoubleElement $value
83
	 */
84
	protected abstract function createItem($value);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
85
}