Completed
Push — master ( 14b45d...d8ec9e )
by smiley
04:40
created

HTMLElementTrait::setStyle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 2
crap 3
1
<?php
2
/**
3
 * Trait HTMLElementTrait
4
 *
5
 * @filesource   HTMLElementTrait.php
6
 * @created      11.05.2017
7
 * @package      chillerlan\PrototypeDOM\Traits
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\PrototypeDOM\Traits;
14
15
use chillerlan\PrototypeDOM\Node\PrototypeHTMLElement;
16
17
/**
18
 * @property string $id
19
 * @property string $class
20
 * @property string $innerHTML
21
 */
22
trait HTMLElementTrait{
23
	use Magic, ElementTrait;
24
25 12
	protected function magic_get_id():string {
26 12
		return trim($this->getAttribute('id'));
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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...
27
	}
28
29 1
	protected function magic_set_id($id) {
30 1
		return $this->setAttribute('id', $id);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() 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...
31
	}
32
33 6
	protected function magic_get_class():string {
34 6
		return trim($this->getAttribute('class'));
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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...
35
	}
36
37 3
	protected function magic_set_class($class) {
38 3
		return $this->setAttribute('class', $class);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() 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...
39
	}
40
41 3
	protected function magic_get_innerHTML():string {
42 3
		return $this->inspect();
43
	}
44
45
	/**
46
	 * http://api.prototypejs.org/dom/Element/identify/
47
	 *
48
	 * @param string|null $newID
49
	 *
50
	 * @return string
51
	 */
52 1
	public function identify(string $newID = null):string {
53 1
		$oldID = $this->id;
54
55 1
		if(!is_null($newID)){
56 1
			$this->id = $newID;
57
		}
58
59 1
		return $oldID;
60
	}
61
62
	/**
63
	 * @link http://api.prototypejs.org/dom/Element/classNames/
64
	 *
65
	 * @return array
66
	 */
67 6
	public function classNames():array{
68 6
		$currentClassnames = [];
69
70 6
		if($this->hasAttributes()){
0 ignored issues
show
Bug introduced by
It seems like hasAttributes() 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...
71 6
			$classnames = explode(' ', $this->class);
72
73 6
			if(!empty($classnames)){
74
75 6
				foreach($classnames as $classname){
76
77 6
					if(empty($classname)){
78 5
						continue;
79
					}
80
81 6
					$currentClassnames[] = $classname;
82
				}
83
84
			}
85
86
		}
87
88 6
		return $currentClassnames;
89
	}
90
91
	/**
92
	 * @link http://api.prototypejs.org/dom/Element/hasClassName/
93
	 *
94
	 * @param string $classname
95
	 *
96
	 * @return bool
97
	 */
98 5
	public function hasClassName(string $classname):bool{
99 5
		return in_array($classname, $this->classNames(), true);
100
	}
101
102
	/**
103
	 * @link http://api.prototypejs.org/dom/Element/addClassName/
104
	 *
105
	 * @param string $classname
106
	 *
107
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
108
	 */
109 2
	public function addClassName(string $classname):PrototypeHTMLElement{
110 2
		return $this->addClassNames([$classname]);
0 ignored issues
show
Bug introduced by
The method addClassNames() does not exist on chillerlan\PrototypeDOM\Traits\HTMLElementTrait. Did you maybe mean classNames()?

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...
111
	}
112
113
	/**
114
	 * @link http://api.prototypejs.org/dom/Element/removeClassName/
115
	 *
116
	 * @param string $classname
117
	 *
118
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
119
	 */
120 2
	public function removeClassName(string $classname):PrototypeHTMLElement{
121 2
		return $this->removeClassNames([$classname]);
0 ignored issues
show
Bug introduced by
The method removeClassNames() does not exist on chillerlan\PrototypeDOM\Traits\HTMLElementTrait. Did you maybe mean classNames()?

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...
122
	}
123
124
	/**
125
	 * @link http://api.prototypejs.org/dom/Element/toggleClassName/
126
	 *
127
	 * @param string $classname
128
	 *
129
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
130
	 */
131 1
	public function toggleClassName(string $classname):PrototypeHTMLElement{
132
133 1
		return $this->hasClassName($classname)
134 1
			? $this->removeClassName($classname)
135 1
			: $this->addClassName($classname);
136
	}
137
138
	/**
139
	 * @link http://api.prototypejs.org/dom/Element/getStyle/
140
	 *
141
	 * @param string $property
142
	 *
143
	 * @return null|string
144
	 */
145 2
	public function getStyle(string $property){
146 2
		$currentStyle = $this->getStyles();
0 ignored issues
show
Bug introduced by
The method getStyles() does not exist on chillerlan\PrototypeDOM\Traits\HTMLElementTrait. Did you maybe mean getStyle()?

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...
147
148 2
		if(array_key_exists(strtolower($property), $currentStyle)){
149 2
			return $currentStyle[$property];
150
		}
151
152 2
		return null;
153
	}
154
155
	/**
156
	 * @link http://api.prototypejs.org/dom/Element/setStyle/
157
	 *
158
	 * @param array $style
159
	 * @param bool  $replace
160
	 *
161
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
162
	 */
163 2
	public function setStyle(array $style, bool $replace = false):PrototypeHTMLElement{
164 2
		$currentStyle = $this->getStyles();
0 ignored issues
show
Bug introduced by
The method getStyles() does not exist on chillerlan\PrototypeDOM\Traits\HTMLElementTrait. Did you maybe mean getStyle()?

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...
165
166 2
		if(!$replace){
167 2
			$style = array_merge($currentStyle, $style);
168
		}
169
170 2
		foreach($style as $property => $value){
171 2
			$style[$property] = $property.': '.$value.';';
172
		}
173
174 2
		$this->setAttribute('style', implode(' ', $style));
0 ignored issues
show
Bug introduced by
It seems like setAttribute() 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...
175
176 2
		return $this;
177
	}
178
179
180
}
181