Completed
Push — master ( 89eac8...9d9655 )
by Jean-Christophe
02:45
created

HtmlAbsractItem::initContent()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Ajax\semantic\html\content;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\elements\HtmlIcon;
7
use Ajax\JsUtils;
8
9
use Ajax\service\JArray;
10
use Ajax\semantic\html\elements\html5\HtmlImg;
11
12
abstract class HtmlAbsractItem extends HtmlSemDoubleElement {
13
14
	public function __construct($identifier, $baseClass,$content=NULL) {
15
		parent::__construct($identifier, "div", $baseClass);
16
		$this->content=array();
17
		$this->initContent($content);
18
	}
19
20
	abstract protected function initContent($content);
21
22
	public function setIcon($icon){
23
		$this->content["icon"]=new HtmlIcon("icon-".$this->identifier, $icon);
24
	}
25
26
	public function removeIcon(){
27
		if(isset($this->content["icon"]))
28
			unset($this->content["icon"]);
29
		return $this;
30
	}
31
32
	public function setImage($image){
33
		$image=new HtmlImg("icon-".$this->identifier, $image);
34
		$image->asAvatar();
35
		$this->content["image"]=$image;
36
	}
37
38
	private function createContent(){
39
		$this->content["content"]=new HtmlSemDoubleElement("content-".$this->identifier,"div","content");
40
		return $this->content["content"];
41
	}
42
43
	public function setTitle($title,$description=NULL,$baseClass="title"){
44
		$title=new HtmlSemDoubleElement("","div",$baseClass,$title);
45
		if(\array_key_exists("content", $this->content)===false){
46
			$this->createContent();
47
		}
48
		$this->content["content"]->addContent($title);
49
		if(isset($description)){
50
			$description=new HtmlSemDoubleElement("","div","description",$description);
51
			$this->content["content"]->addContent($description);
52
		}
53
		return $this;
54
	}
55
56
	public function getPart($partName="header"){
57
		$content=\array_merge($this->content["content"]->getContent(),array(@$this->content["icon"],@$this->content["image"]));
58
		return $this->getElementByPropertyValue("class", $partName, $content);
59
	}
60
61
	public function setActive($value=true){
62
		if($value){
63
			$this->setTagName("div");
64
			$this->removeProperty("href");
65
			$this->addToPropertyCtrl("class", "active", array("active"));
66
		}else{
67
			$this->removePropertyValue("class", "active");
68
		}
69
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Ajax\semantic\html\content\HtmlAbsractItem) is incompatible with the return type of the parent method Ajax\semantic\html\base\...oubleElement::setActive of type Ajax\semantic\html\base\traits\BaseTrait.

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...
70
	}
71
72
	public function asLink($href=NULL,$part=NULL){
73
		$this->setTagName("a");
74
		if(isset($href))
75
			$this->setProperty("href", $href);
76
		return $this;
77
	}
78
79
	/**
80
	 *
81
	 * {@inheritDoc}
82
	 *
83
	 * @see \Ajax\semantic\html\base\HtmlSemDoubleElement::compile()
84
	 */
85
	public function compile(JsUtils $js=NULL, &$view=NULL) {
86
		if(\is_array($this->content) && JArray::isAssociative($this->content))
87
			$this->content=JArray::sortAssociative($this->content, [ "icon","image","content" ]);
88
		return parent::compile($js, $view);
89
	}
90
}
91