Completed
Push — master ( 4bf172...5f7aee )
by Jean-Christophe
03:35
created

HtmlCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
151
		}
152
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Ajax\common\html\HtmlCollection) is incompatible with the return type of the parent method Ajax\common\html\BaseHtml::setProperties of type Ajax\common\html\traits\BaseHtmlPropertiesTrait.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
153
	}
154
155
	/**
156
	 * Sets the values of a property for each item in the collection
157
	 * @param string $property
158
	 * @param array $values
159
	 * @return HtmlCollection
160
	 */
161
	public function setPropertyValues($property,$values){
162
		$i=0;
163
		if(\is_array($values)===false){
164
			$values=\array_fill(0, $this->count(),$values);
165
		}
166
		foreach ($values as $value){
167
			$c=$this->content[$i++];
168
			if(isset($c)){
169
				$c->setProperty($property,$value);
170
			}
171
			else{
172
				return $this;
173
			}
174
		}
175
		return $this;
176
	}
177
178
	public function compile(JsUtils $js=NULL, &$view=NULL) {
179
		$index=0;
180
		$classname=\strtolower(JReflection::shortClassName($this));
181
		foreach ($this->content as $item){
182
			$this->setItemIdentifier($item,$classname,$index++);
183
		}
184
		return parent::compile($js,$view);
185
	}
186
187
	public function getItemById($identifier){
188
		return $this->getElementById($identifier, $this->content);
189
	}
190
}