Completed
Push — master ( 0c3815...012895 )
by Jean-Christophe
03:40
created

HtmlMenu::setActiveItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
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");
28
		$this->setClass("ui menu");
29
		$this->addItems($items);
30
	}
31
32
	/**
33
	 * Sets the menu type
34
	 * @param string $type one of text,item
35
	 * @return \Ajax\semantic\html\collections\HtmlMenu
36
	 */
37
	public function setType($type=""){
38
		return $this->addToPropertyCtrl("class", $type, array("","item","text"));
39
	}
40
41
	public function setActiveItem($index){
42
		$item=$this->getItem($index);
43
		if($item!==null){
44
			$item->addToProperty("class", "active");
45
		}
46
		return $this;
47
	}
48
49
	/**
50
	 * {@inheritDoc}
51
	 * @see \Ajax\common\html\html5\HtmlCollection::addItem()
52
	 */
53
	public function addItem($item){
54
		if($item instanceof HtmlInput || $item instanceof HtmlImg){
55
			$itemO=new HtmlSemDoubleElement("item-".$this->identifier,"div","");
56
			$itemO->setContent($item);
57
			$item=$itemO;
58
		}
59
		$item=parent::addItem($item);
60
		if(!$item instanceof HtmlMenu)
61
			$item->addToPropertyCtrl("class", "item",array("item"));
62
		else{
63
			$this->setSecondary();
64
		}
65
		return $item;
66
	}
67
68
	public function generateMenuAsItem($menu,$header=null){
69
		$count=$this->count();
70
		$item=new HtmlSemDoubleElement("item-".$this->identifier."-".$count,"div");
71 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...
72
			$headerItem=new HtmlSemDoubleElement("item-header-".$this->identifier."-".$count,"div","header");
73
			$headerItem->setContent($header);
74
			$item->addContent($headerItem);
75
		}
76
		$menu->setClass("menu");
77
		$item->addContent($menu);
78
		return $item;
79
	}
80
	public function addMenuAsItem($menu,$header=null){
81
		return $this->addItem($this->generateMenuAsItem($menu,$header));
82
	}
83
84
	public function addPopupAsItem($value,$identifier, $content=""){
85
		$value=new HtmlSemDoubleElement($identifier,"a","browse item",$value);
86
		$value->addContent(new HtmlIcon("", "dropdown"));
87
		$value=$this->addItem($value);
88
		$popup=new HtmlPopup($value,"popup-".$this->identifier."-".$this->count(),$content);
89
		$popup->setFlowing()->setPosition("bottom left")->setOn("click");
90
		$this->wrap("",$popup);
91
		return $popup;
92
	}
93
94
	public function addDropdownAsItem($value,$items=NULL){
95
		$dd=$value;
96
		if(\is_string($value)){
97
			$dd=new HtmlDropdown("dropdown-".$this->identifier."-".$this->count(),$value,$items);
98
		}
99
		return $this->addItem($dd);
100
	}
101
	/**
102
	 * {@inheritDoc}
103
	 * @see \Ajax\common\html\html5\HtmlCollection::createItem()
104
	 */
105
	protected function createItem($value) {
106
		$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 105 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...
107
		return $itemO->setClass("item");
108
	}
109
110
	public function setInverted(){
111
		return $this->addToProperty("class", "inverted");
112
	}
113
114
	public function setSecondary(){
115
		return $this->addToProperty("class", "secondary");
116
	}
117
118
	public function setVertical(){
119
		return $this->addToPropertyCtrl("class", "vertical",array("vertical"));
120
	}
121
122
	public function setPosition($value="right"){
123
		return $this->addToPropertyCtrl("class", $value,array("right","left"));
124
	}
125
126
	public function setPointing($value=Direction::NONE){
127
		return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing"));
128
	}
129
130
	public function asTab($vertical=false){
131
		$this->apply(function(HtmlDoubleElement &$item){$item->setTagName("a");});
132
		if($vertical===true)
133
			$this->setVertical();
134
		return $this->addToProperty("class", "tabular");
135
	}
136
137
	public function asPagination(){
138
		$this->apply(function(HtmlDoubleElement &$item){$item->setTagName("a");});
139
			return $this->addToProperty("class", "pagination");
140
	}
141
142
	public function setFixed(){
143
		return $this->addToProperty("class", "fixed");
144
	}
145
146
	public function setFluid(){
147
		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...
148
	}
149
150
	public function setCompact(){
151
		return $this->addToProperty("class", "compact");
152
	}
153
154
	/* (non-PHPdoc)
155
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
156
	 */
157
	public function fromDatabaseObject($object, $function) {
158
		$return=$function($object);
159
		if(\is_array($return))
160
			$this->addItems($return);
161
		else
162
		$this->addItem($return);
163
	}
164
165
	/**
166
	 * Defines the menu width
167
	 * @param int $width
168
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
169
	 */
170 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...
171
		if(\is_int($width)){
172
			$width=Wide::getConstants()["W".$width];
173
		}
174
		$this->addToPropertyCtrl("class", $width, Wide::getConstants());
175
		return $this->addToPropertyCtrl("class", "item",array("item"));
176
	}
177
178
	public function addImage($identifier, $src="", $alt=""){
179
		return $this->addItem(new HtmlImg($identifier,$src,$alt));
180
	}
181
182
	public static function vertical($identifier,$items=array()){
183
		return (new HtmlMenu($identifier,$items))->setVertical();
184
	}
185
}