Completed
Push — master ( 68d119...ebc63c )
by Lars
02:09
created

AbstractSimpleHtmlDom::html()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\helper;
6
7
abstract class AbstractSimpleHtmlDom
8
{
9
    /**
10
     * @var \DOMElement|\DOMNode|null
11
     */
12
    protected $node;
13
14
    /**
15
     * @var array
16
     */
17
    protected static $functionAliases = [
18
        'children'     => 'childNodes',
19
        'first_child'  => 'firstChild',
20
        'last_child'   => 'lastChild',
21
        'next_sibling' => 'nextSibling',
22
        'prev_sibling' => 'previousSibling',
23
        'parent'       => 'parentNode',
24
        'outertext'    => 'html',
25
        'outerhtml'    => 'html',
26
        'innertext'    => 'innerHtml',
27
        'innerhtml'    => 'innerHtml',
28
    ];
29
30
    /**
31
     * @param string $name
32
     * @param array  $arguments
33
     *
34
     * @throws \BadMethodCallException
35
     *
36
     * @return SimpleHtmlDomInterface|string|null
37
     */
38 View Code Duplication
    public function __call($name, $arguments)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        $name = \strtolower($name);
41
42
        if (isset(self::$functionAliases[$name])) {
43
            return \call_user_func_array([$this, self::$functionAliases[$name]], $arguments);
44
        }
45
46
        throw new \BadMethodCallException('Method does not exist');
47
    }
48
49
    /**
50
     * @param string $name
51
     *
52
     * @return array|string|null
53
     */
54 51
    public function __get($name)
55
    {
56 51
        $nameOrig = $name;
57 51
        $name = \strtolower($name);
58
59 51
        switch ($name) {
60 51
            case 'outerhtml':
61 46
            case 'outertext':
62 39
            case 'html':
63 21
                return $this->html();
64 39
            case 'innerhtml':
65 33
            case 'innertext':
66 11
                return $this->innerHtml();
67 30
            case 'text':
68 25
            case 'plaintext':
69 20
                return $this->text();
70 14
            case 'tag':
71 5
                return $this->node ? $this->node->nodeName : '';
72 12
            case 'attr':
73
                return $this->getAllAttributes();
74 View Code Duplication
            default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75 12
                if ($this->node && \property_exists($this->node, $nameOrig)) {
76 1
                    return $this->node->{$nameOrig};
77
                }
78
79 12
                return $this->getAttribute($name);
80
        }
81
    }
82
83
    /**
84
     * @param string $selector
85
     * @param int    $idx
86
     *
87
     * @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface
88
     */
89 12
    public function __invoke($selector, $idx = null)
90
    {
91 12
        return $this->find($selector, $idx);
92
    }
93
94
    /**
95
     * @param string $name
96
     *
97
     * @return bool
98
     */
99 1
    public function __isset($name)
100
    {
101 1
        $nameOrig = $name;
102 1
        $name = \strtolower($name);
103
104 1
        switch ($name) {
105 1
            case 'outertext':
106 1
            case 'outerhtml':
107 1
            case 'innertext':
108 1
            case 'innerhtml':
109 1
            case 'plaintext':
110 1
            case 'text':
111 1
            case 'tag':
112
                return true;
113 View Code Duplication
            default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114 1
                if ($this->node && \property_exists($this->node, $nameOrig)) {
115
                    return isset($this->node->{$nameOrig});
116
                }
117
118 1
                return $this->hasAttribute($name);
119
        }
120
    }
121
122
    /**
123
     * @param string $name
124
     * @param mixed  $value
125
     *
126
     * @return SimpleHtmlDomInterface|null
127
     */
128 17
    public function __set($name, $value)
129
    {
130 17
        $nameOrig = $name;
131 17
        $name = \strtolower($name);
132
133 17
        switch ($name) {
134 17
            case 'outerhtml':
135 15
            case 'outertext':
136 4
                return $this->replaceNodeWithString($value);
137 13
            case 'innertext':
138 11
            case 'innerhtml':
139 7
                return $this->replaceChildWithString($value);
140 10
            case 'plaintext':
141 1
                return $this->replaceTextWithString($value);
142 View Code Duplication
            default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143 9
                if ($this->node && \property_exists($this->node, $nameOrig)) {
144
                    return $this->node->{$nameOrig} = $value;
145
                }
146
147 9
                return $this->setAttribute($name, $value);
148
        }
149
    }
150
151
    /**
152
     * @return string
153
     */
154 2
    public function __toString()
155
    {
156 2
        return $this->html();
157
    }
158
159
    /**
160
     * @param string $name
161
     *
162
     * @return void
163
     */
164
    public function __unset($name)
165
    {
166
        /** @noinspection UnusedFunctionResultInspection */
167
        $this->removeAttribute($name);
168
    }
169
170
    abstract public function setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface;
171
172
    abstract protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface;
173
174
    abstract protected function replaceChildWithString(string $string): SimpleHtmlDomInterface;
175
176
    abstract protected function replaceTextWithString($string): SimpleHtmlDomInterface;
177
178
    abstract public function hasAttribute(string $name): bool;
179
180
    abstract public function find(string $selector, $idx = null);
181
182
    abstract public function getAttribute(string $name): string;
183
184
    abstract public function getAllAttributes();
185
186
    abstract public function text(): string;
187
188
    abstract public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string;
189
190
    abstract public function html(bool $multiDecodeNewHtmlEntity = false): string;
191
192
    abstract public function removeAttribute(string $name): SimpleHtmlDomInterface;
193
}
194