Completed
Push — master ( b569b1...d88325 )
by Jean-Christophe
03:46
created

HtmlMenu::afterInsert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Ajax\semantic\html\collections\menus;
4
5
use Ajax\bootstrap\html\HtmlLink;
6
use Ajax\common\html\HtmlDoubleElement;
7
use Ajax\semantic\html\base\constants\Direction;
8
use Ajax\semantic\html\base\HtmlSemCollection;
9
use Ajax\semantic\html\base\HtmlSemDoubleElement;
10
use Ajax\semantic\html\base\constants\Wide;
11
use Ajax\common\html\html5\HtmlImg;
12
use Ajax\bootstrap\html\HtmlInput;
13
use Ajax\semantic\html\modules\HtmlDropdown;
14
use Ajax\common\html\BaseHtml;
15
use Ajax\semantic\html\modules\HtmlPopup;
16
use Ajax\semantic\html\elements\HtmlIcon;
17
18
/**
19
 * Semantic Menu component
20
 * @see http://semantic-ui.com/collections/menu.html
21
 * @author jc
22
 * @version 1.001
23
 */
24
class HtmlMenu extends HtmlSemCollection {
25
26
	public function __construct($identifier, $items=array()) {
27
		parent::__construct($identifier, "div","ui menu");
28
		$this->addItems($items);
29
	}
30
31
	/**
32
	 * Sets the menu type
33
	 * @param string $type one of text,item
34
	 * @return \Ajax\semantic\html\collections\HtmlMenu
35
	 */
36
	public function setType($type=""){
37
		return $this->addToPropertyCtrl("class", $type, array("","item","text"));
38
	}
39
40
	public function setActiveItem($index){
41
		$item=$this->getItem($index);
42
		if($item!==null){
43
			$item->addToProperty("class", "active");
44
		}
45
		return $this;
46
	}
47
	private function getItemToInsert($item){
48
		if($item instanceof HtmlInput || $item instanceof HtmlImg || $item instanceof HtmlIcon){
49
			$itemO=new HtmlSemDoubleElement("item-".$this->identifier,"div","");
50
			$itemO->setContent($item);
51
			$item=$itemO;
52
		}
53
		return $item;
54
	}
55
56
	private function afterInsert($item){
57
		if(!$item instanceof HtmlMenu)
58
			$item->addToPropertyCtrl("class", "item",array("item"));
59
		else{
60
			$this->setSecondary();
61
		}
62
		return $item;
63
	}
64
	/**
65
	 * {@inheritDoc}
66
	 * @see \Ajax\common\html\html5\HtmlCollection::addItem()
67
	 */
68
	public function addItem($item){
69
		$item=parent::addItem($this->getItemToInsert($item));
70
		return $this->afterInsert($item);
71
	}
72
73
	/**
74
	 * {@inheritDoc}
75
	 * @see \Ajax\common\html\HtmlCollection::insertItem()
76
	 */
77
	public function insertItem($item,$position=0){
78
		$item=parent::insertItem($this->getItemToInsert($item),$position);
79
		return $this->afterInsert($item);
80
	}
81
82
	public function generateMenuAsItem($menu,$header=null){
83
		$count=$this->count();
84
		$item=new HtmlSemDoubleElement("item-".$this->identifier."-".$count,"div");
85 View Code Duplication
		if(isset($header)){
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...
86
			$headerItem=new HtmlSemDoubleElement("item-header-".$this->identifier."-".$count,"div","header");
87
			$headerItem->setContent($header);
88
			$item->addContent($headerItem);
89
		}
90
		$menu->setClass("menu");
91
		$item->addContent($menu);
92
		return $item;
93
	}
94
	public function addMenuAsItem($menu,$header=null){
95
		return $this->addItem($this->generateMenuAsItem($menu,$header));
96
	}
97
98
	public function addPopupAsItem($value,$identifier, $content=""){
99
		$value=new HtmlSemDoubleElement($identifier,"a","browse item",$value);
100
		$value->addContent(new HtmlIcon("", "dropdown"));
101
		$value=$this->addItem($value);
102
		$popup=new HtmlPopup($value,"popup-".$this->identifier."-".$this->count(),$content);
103
		$popup->setFlowing()->setPosition("bottom left")->setOn("click");
104
		$this->wrap("",$popup);
105
		return $popup;
106
	}
107
108
	public function addDropdownAsItem($value,$items=NULL){
109
		$dd=$value;
110
		if(\is_string($value)){
111
			$dd=new HtmlDropdown("dropdown-".$this->identifier."-".$this->count(),$value,$items);
112
		}
113
		return $this->addItem($dd);
114
	}
115
	/**
116
	 * {@inheritDoc}
117
	 * @see \Ajax\common\html\html5\HtmlCollection::createItem()
118
	 */
119
	protected function createItem($value) {
120
		$itemO=new HtmlLink("item-".\sizeof($this->content),"",$value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 119 can also be of type object<Ajax\common\html\HtmlDoubleElement>; however, Ajax\bootstrap\html\HtmlLink::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
121
		return $itemO->setClass("item");
122
	}
123
124
	public function setInverted(){
125
		return $this->addToProperty("class", "inverted");
126
	}
127
128
	public function setSecondary(){
129
		return $this->addToProperty("class", "secondary");
130
	}
131
132
	public function setVertical(){
133
		return $this->addToPropertyCtrl("class", "vertical",array("vertical"));
134
	}
135
136
	public function setPosition($value="right"){
137
		return $this->addToPropertyCtrl("class", $value,array("right","left"));
138
	}
139
140
	public function setPointing($value=Direction::NONE){
141
		return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing"));
142
	}
143
144
	public function asTab($vertical=false){
145
		$this->apply(function(HtmlDoubleElement &$item){$item->setTagName("a");});
146
		if($vertical===true)
147
			$this->setVertical();
148
		return $this->addToProperty("class", "tabular");
149
	}
150
151
	public function asPagination(){
152
		$this->apply(function(HtmlDoubleElement &$item){$item->setTagName("a");});
153
			return $this->addToProperty("class", "pagination");
154
	}
155
156
	public function setFixed(){
157
		return $this->addToProperty("class", "fixed");
158
	}
159
160
	public function setFluid(){
161
		return $this->addToProperty("class", "fluid");
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->addToProperty('class', 'fluid'); (Ajax\semantic\html\collections\menus\HtmlMenu) is incompatible with the return type of the parent method Ajax\semantic\html\base\...SemCollection::setFluid of type Ajax\semantic\html\base\HtmlSemDoubleElement.

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...
162
	}
163
164
	public function setCompact(){
165
		return $this->addToProperty("class", "compact");
166
	}
167
168
	/* (non-PHPdoc)
169
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
170
	 */
171
	public function fromDatabaseObject($object, $function) {
172
		$return=$function($object);
173
		if(\is_array($return))
174
			$this->addItems($return);
175
		else
176
		$this->addItem($return);
177
	}
178
179
	/**
180
	 * Defines the menu width
181
	 * @param int $width
182
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
183
	 */
184 View Code Duplication
	public function setWidth($width){
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...
185
		if(\is_int($width)){
186
			$width=Wide::getConstants()["W".$width];
187
		}
188
		$this->addToPropertyCtrl("class", $width, Wide::getConstants());
189
		return $this->addToPropertyCtrl("class", "item",array("item"));
190
	}
191
192
	public function addImage($identifier, $src="", $alt=""){
193
		return $this->addItem(new HtmlImg($identifier,$src,$alt));
194
	}
195
196
	public static function vertical($identifier,$items=array()){
197
		return (new HtmlMenu($identifier,$items))->setVertical();
198
	}
199
}