Completed
Push — master ( d789b0...e99734 )
by Jean-Christophe
03:05
created

BaseTrait::asHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ajax\semantic\html\base\traits;
4
5
use Ajax\semantic\html\base\constants\Size;
6
use Ajax\semantic\html\base\constants\Color;
7
use Ajax\semantic\html\base\constants\Direction;
8
use Ajax\semantic\html\elements\HtmlIcon;
9
10
trait BaseTrait {
11
	protected $_variations=[ ];
12
	protected $_states=[ ];
13
	protected $_baseClass;
14
15
	protected abstract function setPropertyCtrl($name, $value, $typeCtrl);
16
17
	protected abstract function addToPropertyCtrl($name, $value, $typeCtrl);
18
19
	protected abstract function addToPropertyCtrlCheck($name, $value, $typeCtrl);
20
21
	public abstract function addToProperty($name, $value, $separator=" ");
22
23
	public function addVariation($variation) {
24
		return $this->addToPropertyCtrlCheck("class", $variation, $this->_variations);
25
	}
26
27
	public function addState($state) {
28
		return $this->addToPropertyCtrlCheck("class", $state, $this->_states);
29
	}
30
31
	public function setVariation($variation) {
32
		$this->setPropertyCtrl("class", $variation, $this->_variations);
33
		return $this->addToProperty("class", $this->_baseClass);
34
	}
35
36
	public function setVariations($variations) {
37
		$this->setProperty("class", $this->_baseClass);
0 ignored issues
show
Bug introduced by
The method setProperty() does not exist on Ajax\semantic\html\base\traits\BaseTrait. Did you maybe mean setPropertyCtrl()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
38
		if (\is_string($variations))
39
			$variations=\explode(" ", $variations);
40
		foreach ( $variations as $variation ) {
41
			$this->addVariation($variation);
42
		}
43
		return $this;
44
	}
45
46
	public function setState($state) {
47
		$this->setPropertyCtrl("class", $state, $this->_states);
48
		return $this->addToProperty("class", $this->_baseClass);
49
	}
50
51
	public function addVariations($variations=array()) {
52
		if (\is_string($variations))
53
			$variations=\explode(" ", $variations);
54
		foreach ( $variations as $variation ) {
55
			$this->addVariation($variation);
56
		}
57
		return $this;
58
	}
59
60
	public function addStates($states=array()) {
61
		if (\is_string($states))
62
			$states=\explode(" ", $states);
63
		foreach ( $states as $state ) {
64
			$this->addState($state);
65
		}
66
		return $this;
67
	}
68
69
	public function setStates($states) {
70
		$this->setProperty("class", $this->_baseClass);
0 ignored issues
show
Bug introduced by
The method setProperty() does not exist on Ajax\semantic\html\base\traits\BaseTrait. Did you maybe mean setPropertyCtrl()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
71
		if (\is_string($states))
72
			$states=\explode(" ", $states);
73
		foreach ( $states as $state ) {
74
			$this->addState($state);
75
		}
76
		return $this;
77
	}
78
79
	public function addIcon($icon, $before=true) {
80
		return $this->addContent(new HtmlIcon("icon-" . $this->identifier, $icon), $before);
0 ignored issues
show
Bug introduced by
The property identifier does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like addContent() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
81
	}
82
83
	/**
84
	 *
85
	 * {@inheritDoc}
86
	 *
87
	 * @see \Ajax\common\html\HtmlSingleElement::setSize()
88
	 */
89
	public function setSize($size) {
90
		return $this->addToPropertyCtrl("class", $size, Size::getConstants());
91
	}
92
93
	/**
94
	 * show it is currently unable to be interacted with
95
	 * @return \Ajax\semantic\html\elements\HtmlSemDoubleElement
96
	 */
97
	public function setDisabled() {
98
		return $this->addToProperty("class", "disabled");
99
	}
100
101
	/**
102
	 *
103
	 * @param string $color
104
	 * @return \Ajax\semantic\html\base\HtmlSemDoubleElement
105
	 */
106
	public function setColor($color) {
107
		return $this->addToPropertyCtrl("class", $color, Color::getConstants());
108
	}
109
110
	/**
111
	 *
112
	 * @return \Ajax\semantic\html\base\HtmlSemDoubleElement
113
	 */
114
	public function setFluid() {
115
		return $this->addToProperty("class", "fluid");
116
	}
117
118
	/**
119
	 *
120
	 * @return \Ajax\semantic\html\base\HtmlSemDoubleElement
121
	 */
122
	public function asHeader(){
123
		return $this->addToProperty("class", "header");
124
	}
125
126
	/**
127
	 * show it is currently the active user selection
128
	 * @return \Ajax\semantic\html\base\HtmlSemDoubleElement
129
	 */
130
	public function setActive($value=true){
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
		return $this->addToProperty("class", "active");
132
	}
133
134
	/**
135
	 * can be formatted to appear on dark backgrounds
136
	 */
137
	public function setInverted() {
138
		return $this->addToProperty("class", "inverted");
139
	}
140
141
	public function setCircular() {
142
		return $this->addToProperty("class", "circular");
143
	}
144
145
	public function setFloated($direction="right") {
146
		return $this->addToPropertyCtrl("class", $direction . " floated", Direction::getConstantValues("floated"));
147
	}
148
149
	public function floatRight() {
150
		return $this->setFloated();
151
	}
152
153
	public function floatLeft() {
154
		return $this->setFloated("left");
155
	}
156
157
	public function getBaseClass() {
158
		return $this->_baseClass;
159
	}
160
}