Completed
Push — master ( 9de830...b49918 )
by Jean-Christophe
03:44
created

HtmlMenu::asPagination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
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
11
/**
12
 * Semantic Menu component
13
 * @see http://semantic-ui.com/collections/menu.html
14
 * @author jc
15
 * @version 1.001
16
 */
17
class HtmlMenu extends HtmlSemCollection {
18
19
	public function __construct($identifier, $items=array()) {
20
		parent::__construct($identifier, "div");
21
		$this->setClass("ui menu");
22
		$this->addItems($items);
23
	}
24
25
	/**
26
	 * Sets the menu type
27
	 * @param string $type one of text,item
28
	 * @return \Ajax\semantic\html\collections\HtmlMenu
29
	 */
30
	public function setType($type=""){
31
		return $this->addToPropertyCtrl("class", $type, array("","item","text"));
32
	}
33
34
	public function setActiveItem($index){
35
		$item=$this->getItem($index);
36
		if($item!==null){
37
			$item->addToProperty("class", "active");
38
		}
39
		return $this;
40
	}
41
42
	/**
43
	 * {@inheritDoc}
44
	 * @see \Ajax\common\html\html5\HtmlCollection::addItem()
45
	 */
46
	public function addItem($item){
47
		if($item instanceof \Ajax\semantic\html\elements\HtmlInput){
48
			$itemO=new HtmlSemDoubleElement("item-".$this->identifier,"div");
49
			$itemO->setContent($item);
50
			$item=$itemO;
51
		}
52
		$item=parent::addItem($item);
53
		if(!$item instanceof HtmlMenu)
54
			$item->addToPropertyCtrl("class", "item",array("item"));
55
		else{
56
			$this->setSecondary();
57
		}
58
	}
59
	public function generateMenuAsItem($menu,$header=null){
60
		$count=$this->count();
61
		$item=new HtmlSemDoubleElement("item-".$this->identifier."-".$count,"div");
62 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...
63
			$headerItem=new HtmlSemDoubleElement("item-header-".$this->identifier."-".$count,"div","header");
64
			$headerItem->setContent($header);
65
			$item->addContent($headerItem);
66
		}
67
		$menu->setClass("menu");
68
		$item->addContent($menu);
69
		return $item;
70
	}
71
	public function addMenuAsItem($menu,$header=null){
72
		return $this->addItem($this->generateMenuAsItem($menu,$header));
73
	}
74
	/**
75
	 * {@inheritDoc}
76
	 * @see \Ajax\common\html\html5\HtmlCollection::createItem()
77
	 */
78
	protected function createItem($value) {
79
		$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 78 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...
80
		return $itemO->setClass("item");
81
	}
82
83
	public function setInverted(){
84
		return $this->addToProperty("class", "inverted");
85
	}
86
87
	public function setSecondary(){
88
		return $this->addToProperty("class", "secondary");
89
	}
90
91
	public function setVertical(){
92
		return $this->addToPropertyCtrl("class", "vertical",array("vertical"));
93
	}
94
95
	public function setPosition($value="right"){
96
		return $this->addToPropertyCtrl("class", $value,array("right","left"));
97
	}
98
99
	public function setPointing($value=Direction::NONE){
100
		return $this->addToPropertyCtrl("class", $value." pointing",Direction::getConstantValues("pointing"));
101
	}
102
103
	public function asTab($vertical=false){
104
		$this->apply(function(HtmlDoubleElement &$item){$item->setTagName("a");});
105
		if($vertical===true)
106
			$this->setVertical();
107
		return $this->addToProperty("class", "tabular");
108
	}
109
110
	public function asPagination(){
111
		$this->apply(function(HtmlDoubleElement &$item){$item->setTagName("a");});
112
			return $this->addToProperty("class", "pagination");
113
	}
114
115
	public function setFixed(){
116
		return $this->addToProperty("class", "fixed");
117
	}
118
119
	public function setFluid(){
120
		return $this->addToProperty("class", "fluid");
121
	}
122
123
	public function setCompact(){
124
		return $this->addToProperty("class", "compact");
125
	}
126
127
	/* (non-PHPdoc)
128
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
129
	 */
130
	public function fromDatabaseObject($object, $function) {
131
		$return=$function($object);
132
		if(\is_array($return))
133
			$this->addItems($return);
134
		else
135
		$this->addItem($return);
136
	}
137
}