Completed
Push — master ( ea2a7d...e9bdcb )
by Jean-Christophe
03:40
created

HtmlMenu::setFloated()   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 1
1
<?php
2
3
namespace Ajax\semantic\html\collections\menus;
4
5
use Ajax\common\html\HtmlDoubleElement;
6
use Ajax\semantic\html\base\constants\Direction;
7
use Ajax\semantic\html\base\HtmlSemCollection;
8
use Ajax\semantic\html\base\HtmlSemDoubleElement;
9
use Ajax\semantic\html\base\constants\Wide;
10
use Ajax\common\html\html5\HtmlImg;
11
use Ajax\bootstrap\html\HtmlInput;
12
use Ajax\semantic\html\modules\HtmlDropdown;
13
use Ajax\common\html\BaseHtml;
14
use Ajax\semantic\html\modules\HtmlPopup;
15
use Ajax\semantic\html\elements\HtmlIcon;
16
use Ajax\semantic\html\elements\html5\HtmlLink;
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
48
	private function getItemToInsert($item) {
49
		if ($item instanceof HtmlInput || $item instanceof HtmlImg || $item instanceof HtmlIcon) {
50
			$itemO=new HtmlSemDoubleElement("item-" . $this->identifier, "div", "");
51
			$itemO->setContent($item);
52
			$item=$itemO;
53
		}
54
		return $item;
55
	}
56
57
	private function afterInsert($item) {
58
		if (!$item instanceof HtmlMenu)
59
			$item->addToPropertyCtrl("class", "item", array ("item" ));
60
		else {
61
			$this->setSecondary();
62
		}
63
		return $item;
64
	}
65
66
	/**
67
	 *
68
	 * {@inheritDoc}
69
	 *
70
	 * @see \Ajax\common\html\html5\HtmlCollection::addItem()
71
	 */
72
	public function addItem($item) {
73
		$item=parent::addItem($this->getItemToInsert($item));
74
		return $this->afterInsert($item);
75
	}
76
77
	/**
78
	 *
79
	 * {@inheritDoc}
80
	 *
81
	 * @see \Ajax\common\html\HtmlCollection::insertItem()
82
	 */
83
	public function insertItem($item, $position=0) {
84
		$item=parent::insertItem($this->getItemToInsert($item), $position);
85
		return $this->afterInsert($item);
86
	}
87
88
	public function generateMenuAsItem($menu, $header=null) {
89
		$count=$this->count();
90
		$item=new HtmlSemDoubleElement("item-" . $this->identifier . "-" . $count, "div");
91 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...
92
			$headerItem=new HtmlSemDoubleElement("item-header-" . $this->identifier . "-" . $count, "div", "header");
93
			$headerItem->setContent($header);
94
			$item->addContent($headerItem);
95
		}
96
		$menu->setClass("menu");
97
		$item->addContent($menu);
98
		return $item;
99
	}
100
101
	public function addMenuAsItem($menu, $header=null) {
102
		return $this->addItem($this->generateMenuAsItem($menu, $header));
103
	}
104
105
	public function addPopupAsItem($value, $identifier, $content="") {
106
		$value=new HtmlSemDoubleElement($identifier, "a", "browse item", $value);
107
		$value->addContent(new HtmlIcon("", "dropdown"));
108
		$value=$this->addItem($value);
109
		$popup=new HtmlPopup($value, "popup-" . $this->identifier . "-" . $this->count(), $content);
110
		$popup->setFlowing()->setPosition("bottom left")->setOn("click");
111
		$this->wrap("", $popup);
112
		return $popup;
113
	}
114
115
	public function addDropdownAsItem($value, $items=NULL) {
116
		$dd=$value;
117
		if (\is_string($value)) {
118
			$dd=new HtmlDropdown("dropdown-" . $this->identifier . "-" . $this->count(), $value, $items);
119
		}
120
		return $this->addItem($dd);
121
	}
122
123
	/**
124
	 *
125
	 * {@inheritDoc}
126
	 *
127
	 * @see \Ajax\common\html\html5\HtmlCollection::createItem()
128
	 */
129
	protected function createItem($value) {
130
		$itemO=new HtmlLink("item-" . \sizeof($this->content), "", $value);
131
		return $itemO->setClass("item");
132
	}
133
134
	public function setInverted() {
135
		return $this->addToProperty("class", "inverted");
136
	}
137
138
	public function setSecondary() {
139
		return $this->addToProperty("class", "secondary");
140
	}
141
142
	public function setVertical() {
143
		return $this->addToPropertyCtrl("class", "vertical", array ("vertical" ));
144
	}
145
146
	public function setPosition($value="right") {
147
		return $this->addToPropertyCtrl("class", $value, array ("right","left" ));
148
	}
149
150
	public function setPointing($value=Direction::NONE) {
151
		return $this->addToPropertyCtrl("class", $value . " pointing", Direction::getConstantValues("pointing"));
152
	}
153
154
	public function asTab($vertical=false) {
155
		$this->apply(function (HtmlDoubleElement &$item) {
156
			$item->setTagName("a");
157
		});
158
		if ($vertical === true)
159
			$this->setVertical();
160
		return $this->addToProperty("class", "tabular");
161
	}
162
163
	public function asPagination() {
164
		$this->apply(function (HtmlDoubleElement &$item) {
165
			$item->setTagName("a");
166
		});
167
		return $this->addToProperty("class", "pagination");
168
	}
169
170
	public function setFixed() {
171
		return $this->addToProperty("class", "fixed");
172
	}
173
174
	public function setFluid() {
175
		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...
176
	}
177
178
	public function setCompact() {
179
		return $this->addToProperty("class", "compact");
180
	}
181
182
	/*
183
	 * (non-PHPdoc)
184
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
185
	 */
186
	public function fromDatabaseObject($object, $function) {
187
		$return=$function($object);
188
		if (\is_array($return))
189
			$this->addItems($return);
190
		else
191
			$this->addItem($return);
192
	}
193
194
	/**
195
	 * Defines the menu width
196
	 * @param int $width
197
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
198
	 */
199 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...
200
		if (\is_int($width)) {
201
			$width=Wide::getConstants()["W" . $width];
202
		}
203
		$this->addToPropertyCtrl("class", $width, Wide::getConstants());
204
		return $this->addToPropertyCtrl("class", "item", array ("item" ));
205
	}
206
207
	public function addImage($identifier, $src="", $alt="") {
208
		return $this->addItem(new HtmlImg($identifier, $src, $alt));
209
	}
210
211
	public static function vertical($identifier, $items=array()) {
212
		return (new HtmlMenu($identifier, $items))->setVertical();
213
	}
214
}