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

Element   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 1
dl 0
loc 119
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributes() 0 9 2
A setAttributes() 0 8 2
A removeAttributes() 0 8 2
A addClassNames() 0 15 3
A removeClassNames() 0 20 4
B getStyles() 0 23 5
1
<?php
2
/**
3
 * Class Element
4
 *
5
 * @filesource   Element.php
6
 * @created      05.05.2017
7
 * @package      chillerlan\PrototypeDOM
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\PrototypeDOM\Node;
14
15
use DOMElement;
16
use chillerlan\PrototypeDOM\Traits\HTMLElementTrait;
17
18
class Element extends DOMElement implements PrototypeHTMLElement{
19
	use HTMLElementTrait;
20
21
	/**
22
	 * @return  string[]
23
	 */
24 3
	public function getAttributes():array{
25 3
		$attributes = [];
26
27 3
		foreach($this->attributes as $attribute){
28 3
			$attributes[$attribute->nodeName] = $attribute->nodeValue;
29
		}
30
31 3
		return $attributes;
32
	}
33
34
	/**
35
	 * @param array $attributes
36
	 *
37
	 * @return \chillerlan\PrototypeDOM\Node\Element
38
	 */
39 5
	public function setAttributes(array $attributes):Element{
40
41 5
		foreach($attributes as $name => $value){
42 5
			$this->setAttribute($name, $value);
43
		}
44
45 5
		return $this;
46
	}
47
48
	/**
49
	 * @param array $attributes
50
	 *
51
	 * @return \chillerlan\PrototypeDOM\Node\Element
52
	 */
53 1
	public function removeAttributes(array $attributes):Element{
54
55 1
		foreach($attributes as $name){
56 1
			$this->removeAttribute($name);
57
		}
58
59 1
		return $this;
60
	}
61
62
	/**
63
	 * @param array $classnames
64
	 *
65
	 * @return \chillerlan\PrototypeDOM\Node\Element
66
	 */
67 2
	public function addClassNames(array $classnames):Element{
68 2
		$currentClassnames = $this->classNames();
69
70 2
		foreach($classnames as $classname){
71
72 2
			if(!in_array($classname, $currentClassnames, true)){
73 2
				array_push($currentClassnames, $classname);
74
			}
75
76
		}
77
78 2
		$this->class = implode(' ', array_unique($currentClassnames));
79
80 2
		return $this;
81
	}
82
83
	/**
84
	 * @param array $classnames
85
	 *
86
	 * @return \chillerlan\PrototypeDOM\Node\Element
87
	 */
88 2
	public function removeClassNames(array $classnames):Element{
89 2
		$currentClassnames = $this->classNames();
90
91 2
		foreach($classnames as $classname){
92 2
			$keys = array_keys($currentClassnames, $classname);
93
94 2
			if(!empty($keys)){
95
96 2
				foreach($keys as $key){
97 2
					unset($currentClassnames[$key]);
98
				}
99
100
			}
101
102
		}
103
104 2
		$this->class = implode(' ', array_unique($currentClassnames));
105
106 2
		return $this;
107
	}
108
109
	/**
110
	 * @return string[]
111
	 */
112 3
	public function getStyles():array{
113 3
		$currentStyle = [];
114
115 3
		if($this->hasAttributes()){
116 3
			$styles = explode(';', trim($this->getAttribute('style')));
117
118 3
			if(!empty($styles)){
119
120 3
				foreach($styles as $style){
121 3
					$s = explode(':', $style);
122
123 3
					if(count($s) === 2){
124 3
						$currentStyle[strtolower(trim($s[0]))] = trim($s[1]);
125
					}
126
127
				}
128
129
			}
130
131
		}
132
133 3
		return $currentStyle;
134
	}
135
136
}
137