Completed
Push — master ( 228e6f...0dc52a )
by Marcel
01:44
created

HtmlElement   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 0
loc 103
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 4 1
A hasAttribute() 0 4 1
A getAttribute() 0 6 2
A getAttributeForBlade() 0 9 3
A setAttribute() 0 4 1
A removeAttribute() 0 5 1
A getInnerText() 0 4 1
A setInnerText() 0 4 1
A getPlainText() 0 4 1
A prependInnerText() 0 4 1
A appendInnerText() 0 4 1
A getOuterText() 0 4 1
A setOuterText() 0 4 1
A setTag() 0 4 1
A __call() 0 4 1
A __get() 0 4 1
A __set() 0 4 1
1
<?php
2
3
namespace BeyondCode\TagHelper\Html;
4
5
use simplehtmldom_1_5\simple_html_dom_node;
6
use BeyondCode\TagHelper\Exceptions\InvalidTagAttribute;
7
8
class HtmlElement
9
{
10
    /** @var simple_html_dom_node */
11
    protected $domNode;
12
13
    public static function create(simple_html_dom_node $node)
14
    {
15
        return new static($node);
16
    }
17
18
    public function __construct(simple_html_dom_node $domNode)
19
    {
20
        $this->domNode = $domNode;
21
    }
22
23
    public function hasAttribute(string $attribute): bool
24
    {
25
        return $this->domNode->hasAttribute($attribute);
26
    }
27
28
    public function getAttribute(string $attribute, $default = null)
29
    {
30
        $attribute = $this->domNode->getAttribute($attribute);
31
32
        return $attribute === false ? $default : $attribute;
33
    }
34
35
    public function getAttributeForBlade(string $attribute, $default = null)
36
    {
37
        if ($this->domNode->hasAttribute(':' . $attribute)) {
38
            return $this->getAttribute(':' . $attribute, $default);
39
        }
40
        $attribute = $this->domNode->getAttribute($attribute);
41
42
        return $attribute === false ? $default : "'" . $attribute . "'";
43
    }
44
45
    public function setAttribute(string $attribute, string $value)
46
    {
47
        $this->domNode->setAttribute($attribute, $value);
48
    }
49
50
    public function removeAttribute(string $attribute)
51
    {
52
        $this->domNode->setAttribute($attribute, null);
53
        $this->domNode->setAttribute(':'.$attribute, null);
54
    }
55
56
    public function getInnerText(): string
57
    {
58
        return $this->domNode->innertext();
59
    }
60
61
    public function setInnerText(string $text)
62
    {
63
        $this->domNode->innertext = $text;
64
    }
65
66
    public function getPlainText(): string
67
    {
68
        return $this->domNode->text();
69
    }
70
71
    public function prependInnerText(string $prepend)
72
    {
73
        $this->setInnerText($prepend . $this->getInnerText());
74
    }
75
76
    public function appendInnerText(string $append)
77
    {
78
        $this->setInnerText($this->getInnerText() . $append);
79
    }
80
81
    public function getOuterText(): string
82
    {
83
        return $this->domNode->outertext();
84
    }
85
86
    public function setOuterText(string $text)
87
    {
88
        $this->domNode->outertext = $text;
89
    }
90
91
    public function setTag(string $tag)
92
    {
93
        $this->domNode->tag = $tag;
94
    }
95
96
    public function __call($method, $args)
97
    {
98
        return call_user_func_array([$this->domNode, $method], $args);
99
    }
100
101
    public function __get($key)
102
    {
103
        return $this->domNode->$key;
104
    }
105
106
    public function __set($key, $val)
107
    {
108
        return $this->domNode->$key = $val;
109
    }
110
}
111