Completed
Push — master ( d6d0c3...7ff1b8 )
by Jean-Christophe
03:01
created

HtmlButtonGroups::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
3
namespace Ajax\semantic\html\elements;
4
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
use Ajax\semantic\html\base\HtmlSemCollection;
7
use Ajax\JsUtils;
8
9
/**
10
 * Semantic UI Buttongroups component
11
 * @see http://semantic-ui.com/elements/button.html
12
 * @author jc
13
 * @version 1.001
14
 */
15
class HtmlButtonGroups extends HtmlSemCollection {
16
17
	public function __construct($identifier, $elements=array(), $asIcons=false) {
18
		parent::__construct($identifier, "div", "ui buttons");
19
		if ($asIcons === true)
20
			$this->asIcons();
21
		$this->addElements($elements, $asIcons);
22
	}
23
	protected function createItem($value){
24
		return new HtmlButton("button-" . $this->identifier . "-" . \sizeof($this->content), $value);
25
	}
26
27
28
	public function addElement($element, $asIcon=false) {
29
		$item=$this->addItem($element);
30
		if($asIcon)
31
			$item->asIcon($element);
32
		return $item;
33
	}
34
35
	public function addElements($elements, $asIcons=false) {
36
		foreach ( $elements as $element ) {
37
			$this->addElement($element, $asIcons);
38
		}
39
		return $this;
40
	}
41
42
	public function insertOr($aferIndex=0, $or="or") {
43
		$orElement=new HtmlSemDoubleElement("or-" . $this->identifier, "div", "or");
44
		$orElement->setProperty("data-text", $or);
45
		array_splice($this->content, $aferIndex + 1, 0, array ($orElement ));
46
		return $this;
47
	}
48
49
	/*
50
	 * (non-PHPdoc)
51
	 * @see \Ajax\bootstrap\html\HtmlSingleElement::fromArray()
52
	 */
53
	public function fromArray($array) {
54
		$this->addElements($array);
55
	}
56
57
	public function asIcons() {
58
		foreach ( $this->content as $item ) {
59
			if($item instanceof HtmlButton)
60
			$item->asIcon($item->getContent());
61
		}
62
		return $this->addToProperty("class", "icons");
63
	}
64
65
	public function setVertical() {
66
		return $this->addToProperty("class", "vertical");
67
	}
68
69
	public function setLabeled() {
70
		return $this->addToProperty("class", "labeled icon");
71
	}
72
73
	/**
74
	 * Return the element at index
75
	 * @param int $index
76
	 * @return HtmlButton
77
	 */
78
	public function getElement($index) {
79
		return $this->getItem($index);
80
	}
81
82
	public function setElement($index, $button) {
83
		$this->setItem($index, $button);
84
		return $this;
85
	}
86
87
	/*
88
	 * (non-PHPdoc)
89
	 * @see \Ajax\bootstrap\html\base\BaseHtml::on()
90
	 */
91
	public function on($event, $jsCode, $stopPropagation=false, $preventDefault=false) {
92
		foreach ( $this->content as $element ) {
93
			$element->on($event, $jsCode, $stopPropagation, $preventDefault);
94
		}
95
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Ajax\semantic\html\elements\HtmlButtonGroups) is incompatible with the return type of the parent method Ajax\common\html\BaseHtml::on of type Ajax\common\html\traits\BaseHtmlEventsTrait.

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...
96
	}
97
98
	public function getElements() {
99
		return $this->content;
100
	}
101
102
	public function addClasses($classes=array()) {
103
		$i=0;
104
		foreach ( $this->content as $button ) {
105
			$button->addToProperty("class", $classes[$i++]);
106
		}
107
		return $this;
108
	}
109
110
	/*
111
	 * (non-PHPdoc)
112
	 * @see \Ajax\bootstrap\html\base\BaseHtml::fromDatabaseObject()
113
	 */
114
	public function fromDatabaseObject($object, $function) {
115
		$this->addElement($function($object));
116
	}
117
118
	public function run(JsUtils $js){
119
		$result= parent::run($js);
120
		return $result->setItemSelector(".ui.button");
121
	}
122
}