Completed
Push — master ( 6e3264...0dd83f )
by Jean-Christophe
03:39
created

HtmlBreadcrumbs   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 29
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 195
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A addElement() 0 18 4
A setActive() 0 9 2
A addElements() 0 6 2
A fromArray() 0 5 1
A getHref() 0 10 3
A compile() 0 6 2
A fromDatabaseObject() 0 3 1
A on() 0 6 2
A setAutoActive() 0 4 1
A _ajaxOn() 0 6 2
A autoGetOnClick() 0 3 1
A contentAsString() 0 6 2
A getElement() 0 3 1
A addGlyph() 0 4 1
A fromDispatcher() 0 5 1
A setHrefFunction() 0 4 1
1
<?php
2
namespace Ajax\bootstrap\html;
3
4
use Ajax\bootstrap\html\base\HtmlDoubleElement;
5
use Ajax\bootstrap\html\HtmlLink;
6
use Ajax\JsUtils;
7
use Phalcon\Mvc\View;
8
use Phalcon\Mvc\Dispatcher;
9
use Ajax\bootstrap\html\base\HtmlNavElement;
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be exactly one blank line after the last USE statement, 0 were found though.
Loading history...
10
/**
11
 * Twitter Bootstrap Breadcrumbs component
12
 * @see http://getbootstrap.com/components/#breadcrumbs
13
 * @author jc
14
 * @version 1.001
15
 */
16
class HtmlBreadcrumbs extends HtmlNavElement {
1 ignored issue
show
Coding Style introduced by
As per PSR2, the opening brace for this class should be on a new line.
Loading history...
17
	/**
18
	 * @var boolean $autoActive sets the last element's class to <b>active</b> if true
19
	 */
20
	protected $autoActive;
21
	
22
	/**
23
	 * @var boolean if set to true, the path of the elements is absolute
24
	 */
25
	protected $absolutePaths;
26
	
27
	/**
28
	 * @var function the function who generates the href elements. default : function($e){return $e->getContent()}
29
	 */
30
	protected $_hrefFunction;
1 ignored issue
show
Coding Style introduced by
Please declare explicit visibility instead of using a prefixed underscore.
Loading history...
31
		
32
	/**
33
	 * @param string $identifier
34
	 * @param array $elements
35
	 * @param boolean $autoActive sets the last element's class to <b>active</b> if true
36
	 * @param function $hrefFunction the function who generates the href elements. default : function($e){return $e->getContent()}
37
	 */
38
	public function __construct($identifier,$elements=array(),$autoActive=true,$hrefFunction=NULL){
39
		parent::__construct($identifier,"ol");
40
		$this->setProperty("class", "breadcrumb");
41
		$this->content=array();
42
		$this->autoActive=$autoActive;
43
		$this->absolutePaths;
44
		$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\bootstrap\html\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...
45
		if(isset($hrefFunction)){
46
			$this->_hrefFunction=$hrefFunction;
47
		}
48
		$this->addElements($elements);
49
	}
50
	
51
	/**
52
	 * @param mixed $element
53
	 * @param string $href
54
	 * @return \Ajax\bootstrap\html\HtmlLink
55
	 */
56
	public function addElement($element,$href="",$glyph=NULL){
57
		$size=sizeof($this->content);
1 ignored issue
show
Coding Style introduced by
As per coding-style, the use of the aliased function sizeof() should be avoided; please use count() instead.
Loading history...
58
		if(is_array($element)){
59
			$elm=new HtmlLink("lnk-".$this->identifier."-".$size);
60
			$elm->fromArray($element);
61
		}else if($element instanceof HtmlLink){
62
			$elm=$element;
63
		}else{
64
			$elm=new HtmlLink("lnk-".$this->identifier."-".$size,$href,$element);
65
			if(isset($glyph)){
66
				$elm->wrapContentWithGlyph($glyph);
67
			}
68
		}
69
		$elm->wrap("<li>","</li>");
70
		$this->content[]=$elm;
71
		$elm->setProperty($this->attr, $this->getHref($size));
72
		return $elm;
73
	}
74
	
75
	public function setActive($index=null){
76
		if(!isset($index)){
77
			$index=sizeof($this->content)-1;
1 ignored issue
show
Coding Style introduced by
As per coding-style, the use of the aliased function sizeof() should be avoided; please use count() instead.
Loading history...
78
		}
79
		$li=new HtmlDoubleElement("","li");
80
		$li->setClass("active");
81
		$li->setContent($this->content[$index]->getContent());
82
		$this->content[$index]=$li;
83
	}
84
	
85
	public function addElements($elements){
86
		foreach ( $elements as $element ) {
87
			$this->addElement($element);
88
		}
89
		return $this;
90
	}
91
	
92
	public function fromArray($array){
93
		$array=parent::fromArray($array);
94
		$this->addElements($array);
95
		return $array;
96
	}
97
	
98
	/**
99
	 * Return the url of the element at $index or the breadcrumbs url if $index is ommited
100
	 * @param int $index
101
	 * @param string $separator
102
	 * @return string
103
	 */
104
	public function getHref($index=null,$separator="/"){
105
		if(!isset($index)){
106
			$index=sizeof($this->content);
1 ignored issue
show
Coding Style introduced by
As per coding-style, the use of the aliased function sizeof() should be avoided; please use count() instead.
Loading history...
107
		}
108
		if($this->absolutePaths===true){
109
			return $this->_hrefFunction($this->content[$index]);
0 ignored issues
show
Documentation Bug introduced by
The method _hrefFunction does not exist on object<Ajax\bootstrap\html\HtmlBreadcrumbs>? 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...
110
		}else{
111
			return $this->root.implode($separator, array_slice(array_map(function($e){return $this->_hrefFunction($e);}, $this->content),0,$index+1));
0 ignored issues
show
Documentation Bug introduced by
The method _hrefFunction does not exist on object<Ajax\bootstrap\html\HtmlBreadcrumbs>? 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...
112
		}
113
	}
114
	
115
	/*
116
	 * (non-PHPdoc)
117
	 * @see \Ajax\bootstrap\html\BaseHtml::compile()
118
	 */
119
	public function compile(JsUtils $js=NULL, View $view=NULL) {
120
		if($this->autoActive){
121
			$this->setActive();
122
		}
123
		return parent::compile($js, $view);
124
	}
125
	
126
	/* (non-PHPdoc)
127
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
128
	 */
129
	public function fromDatabaseObject($object, $function) {
130
		return $this->addElement($function($object));
131
	}
132
	
133
		/*
134
	 * (non-PHPdoc)
135
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
136
	 */
137
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
138
		foreach ($this->content as $element){
139
			$element->on($event,$jsCode,$stopPropagation,$preventDefault);
140
		}
141
		return $this;
142
	}
143
	
144
	public function setAutoActive($autoActive) {
145
		$this->autoActive = $autoActive;
146
		return $this;
147
	}
148
	
149
	public function _ajaxOn($operation, $event, $url, $responseElement="", $parameters=array()) {
1 ignored issue
show
Coding Style introduced by
Method name "_ajaxOn" should not be prefixed with an underscore to indicate visibility
Loading history...
150
		foreach ($this->content as $element){
151
			$element->_ajaxOn($operation, $event, $url, $responseElement, $parameters);
152
		}
153
		return $this;
154
	}
155
	
156
	/**
157
	 * Associate an ajax get to the breadcrumbs elements, displayed in $targetSelector
158
	 * $attr member is used to build each element url
159
	 * @param string $targetSelector the target of the get
160
	 * @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...
161
	 * @return HtmlBreadcrumbs
162
	 */
163
	public function autoGetOnClick($targetSelector){
164
		return $this->getOnClick($this->root, $targetSelector,array("attr"=>$this->attr));
165
	}
166
	
167
	public function contentAsString(){
168
		if($this->autoActive){
169
			$this->setActive();
170
		}	
171
		return parent::contentAsString();
172
	}
173
	
174
	public function getElement($index){
175
		return $this->content[$index];
176
	}
177
	
178
	/**
179
	 * Add a glyphicon to the element at index $index
180
	 * @param mixed $glyph
181
	 * @param int $index
182
	 */
183
	public function addGlyph($glyph,$index){
184
		$elm=$this->getElement($index);
185
		return $elm->wrapContentWithGlyph($glyph);
186
	}
187
	
188
	/**
189
	 * Add new elements in breadcrumbs corresponding to request dispatcher : controllerName, actionName, parameters
190
	 * @param Dispatcher $dispatcher the request dispatcher
191
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
192
	 */
193
	public function fromDispatcher($dispatcher){
194
		$items=array($dispatcher->getControllerName(),$dispatcher->getActionName());
195
		$items=array_merge($items,$dispatcher->getParams());
196
		return $this->addElements($items);
197
	}
198
	
199
	
200
	/**
201
	 * sets the function who generates the href elements. default : function($element){return $element->getContent()}
202
	 * @param function $_hrefFunction
203
	 * @return \Ajax\bootstrap\html\HtmlBreadcrumbs
204
	 */
205
	public function setHrefFunction($_hrefFunction) {
206
		$this->_hrefFunction = $_hrefFunction;
207
		return $this;
208
	}	
209
	
210
}
1 ignored issue
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...