Completed
Push — master ( 424d4e...c1a616 )
by Jean-Christophe
03:24
created

HtmlBreadcrumb::asTexts()   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 0
1
<?php
2
3
namespace Ajax\semantic\html\collections;
4
5
use Ajax\semantic\html\base\HtmlSemNavElement;
6
use Ajax\semantic\html\base\HtmlSemDoubleElement;
7
use Ajax\JsUtils;
8
use Phalcon\Mvc\View;
9
use Ajax\semantic\html\elements\HtmlIcon;
10
11
/**
12
 * Semantic UI Breadcrumb component
13
 * @see http://semantic-ui.com/collections/breadcrumb.html
14
 * @author jc
15
 * @version 1.001
16
 */
17
class HtmlBreadcrumb extends HtmlSemNavElement{
18
	/**
19
	 * @var integer the start index for href generation
20
	 */
21
	protected $startIndex=0;
22
	/**
23
	 * @var boolean $autoActive sets the last element's class to <b>active</b> if true
24
	 */
25
	protected $autoActive;
26
27
	/**
28
	 * @var boolean if set to true, the path of the elements is absolute
29
	 */
30
	protected $absolutePaths;
31
32
	/**
33
	 * @var function the function who generates the href elements. default : function($e){return $e->getContent()}
34
	 */
35
	protected $_hrefFunction;
36
37
	/**
38
	 * @param string $identifier
39
	 * @param array $items
40
	 * @param boolean $autoActive sets the last element's class to <b>active</b> if true
41
	 * @param function $hrefFunction the function who generates the href elements. default : function($e){return $e->getContent()}
42
	 */
43 View Code Duplication
	public function __construct( $identifier,$items=array(),$autoActive=true,$startIndex=0,$hrefFunction=NULL){
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...
44
		parent::__construct( $identifier, "div", "ui breadcrumb");
45
		$this->startIndex=$startIndex;
46
		$this->autoActive=$autoActive;
47
		$this->_contentSeparator="<div class='divider'> / </div>";
48
		$this->_hrefFunction=function ($e){return $e->getContent();};
0 ignored issues
show
Documentation Bug introduced by
It seems like function ($e) { return $e->getContent(); } of type object<Closure> is incompatible with the declared type object<Ajax\semantic\html\collections\function> of property $_hrefFunction.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
49
		if(isset($hrefFunction)){
50
			$this->_hrefFunction=$hrefFunction;
51
		}
52
		$this->addItems($items);
53
	}
54
55
	/**
56
	 * Associate an ajax get to the breadcrumb elements, displayed in $targetSelector
57
	 * $attr member is used to build each element url
58
	 * @param string $targetSelector the target of the get
59
	 * @param string $attr the html attribute used to build the elements url
0 ignored issues
show
Bug introduced by
There is no parameter named $attr. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
60
	 * @return HtmlBreadcrumbs
61
	 */
62
	public function autoGetOnClick($targetSelector){
63
		return $this->getOnClick($this->root, $targetSelector,array("attr"=>$this->attr));
64
	}
65
66
	public function contentAsString(){
67
		if($this->autoActive){
68
			$this->setActive();
69
		}
70
		return parent::contentAsString();
71
	}
72
73
	public function setActive($index=null){
74
		if(!isset($index)){
75
			$index=sizeof($this->content)-1;
76
		}
77
		$activeItem=$this->content[$index];
78
		$activeItem->addToProperty("class","active");
79
		$activeItem->setTagName("div");
80
	}
81
82
	/**
83
	 * Add new elements in breadcrumbs corresponding to request dispatcher : controllerName, actionName, parameters
84
	 * @param Dispatcher $dispatcher the request dispatcher
85
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
86
	 */
87 View Code Duplication
	public function fromDispatcher($dispatcher,$startIndex=0){
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...
88
		$this->startIndex=$startIndex;
89
		$params=$dispatcher->getParams();
90
		$action=$dispatcher->getActionName();
91
		$items=array($dispatcher->getControllerName());
92
		if(\sizeof($params)>0 || \strtolower($action)!="index" ){
93
			$items[]=$action;
94
			foreach ($params as $p){
95
				if(\is_object($p)===false)
96
					$items[]=$p;
97
			}
98
		}
99
		return $this->addItems($items);
100
	}
101
102
	/**
103
	 * Return the url of the element at $index or the breadcrumbs url if $index is ommited
104
	 * @param int $index
105
	 * @param string $separator
106
	 * @return string
107
	 */
108 View Code Duplication
	public function getHref($index=null,$separator="/"){
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...
109
		if(!isset($index)){
110
			$index=sizeof($this->content);
111
		}
112
		if($this->absolutePaths===true){
113
			return $this->_hrefFunction($this->content[$index]);
0 ignored issues
show
Documentation Bug introduced by
The method _hrefFunction does not exist on object<Ajax\semantic\htm...ections\HtmlBreadcrumb>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
114
		}else{
115
			return $this->root.implode($separator, array_slice(array_map(function($e){return $this->_hrefFunction($e);}, $this->content),$this->startIndex,$index+1));
0 ignored issues
show
Documentation Bug introduced by
The method _hrefFunction does not exist on object<Ajax\semantic\htm...ections\HtmlBreadcrumb>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
116
		}
117
	}
118
119
	/**
120
	 * sets the function who generates the href elements. default : function($element){return $element->getContent()}
121
	 * @param function $_hrefFunction
122
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
123
	 */
124
	public function setHrefFunction($_hrefFunction) {
125
		$this->_hrefFunction = $_hrefFunction;
126
		return $this;
127
	}
128
129
	public function setStartIndex($startIndex) {
130
		$this->startIndex=$startIndex;
131
		return $this;
132
	}
133
134
	public function setAutoActive($autoActive) {
135
		$this->autoActive = $autoActive;
136
		return $this;
137
	}
138
139
	/*
140
	 * (non-PHPdoc)
141
	 * @see \Ajax\bootstrap\html\BaseHtml::compile()
142
	 */
143
	public function compile(JsUtils $js=NULL, View $view=NULL) {
144
		if($this->autoActive){
145
			$this->setActive();
146
		}
147
		$count=$this->count();
148
		for($i=1;$i<$count;$i++){
149
			$this->content[$i]->wrap($this->getContentDivider($i-1));
150
		}
151
		return parent::compile($js, $view);
152
	}
153
154
	/*
155
	 * (non-PHPdoc)
156
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
157
	 */
158
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
159
		foreach ($this->content as $element){
160
				$element->on($event,$jsCode,$stopPropagation,$preventDefault);
161
		}
162
		return $this;
163
	}
164
165
166
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
167
		foreach ($this->content as $element){
168
			if($element->getProperty($this->attr)!=NULL)
169
				$element->_ajaxOn($operation, $event, $url, $responseElement, $parameters);
170
		}
171
		return $this;
172
	}
173
174
	/**
175
	 * {@inheritDoc}
176
	 * @see \Ajax\common\html\HtmlCollection::createItem()
177
	 */
178
	protected function createItem($value) {
179
		$count=$this->count();
180
		$itemO=new HtmlSemDoubleElement("item-".$this->identifier."-".$count,"a","section");
181
		if(\is_array($value))
182
			$itemO->fromArray($value);
183
		else{
184
			$itemO->setContent($value);
185
			$itemO->setProperty($this->attr, $this->getHref($count));
186
		}
187
		return $itemO;
188
	}
189
190
	public function addIcon($icon,$index){
191
		$item=$this->getItem($index);
192
		if(isset($item)){
193
			$icon=new HtmlIcon("icon-".$this->identifier, $icon);
194
			$item->wrapContent($icon);
195
		}
196
	}
197
198
	public function addItem($item){
199
		$itemO=parent::addItem($item);
200
		$this->addToPropertyCtrl("class", "section", array("section"));
201
		return $itemO;
202
	}
203
204
	public function asLinks(){
205
		$this->contentAs("a");
206
	}
207
208
	public function asTexts(){
209
		$this->contentAs("div");
210
	}
211
212
}