Completed
Push — master ( d36330...3c9b9d )
by Lars
01:55
created

AbstractSimpleXmlDom::innerXml()

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 AbstractSimpleXmlDom
8
{
9
    /**
10
     * @var array
11
     */
12
    protected static $functionAliases = [
13
        'children'     => 'childNodes',
14
        'first_child'  => 'firstChild',
15
        'last_child'   => 'lastChild',
16
        'next_sibling' => 'nextSibling',
17
        'prev_sibling' => 'previousSibling',
18
        'parent'       => 'parentNode',
19
    ];
20
21
    /**
22
     * @var \DOMElement|\DOMNode|null
23
     */
24
    protected $node;
25
26
    /**
27
     * @param string $name
28
     * @param array  $arguments
29
     *
30
     * @throws \BadMethodCallException
31
     *
32
     * @return SimpleXmlDomInterface|string|null
33
     */
34
    public function __call($name, $arguments)
35
    {
36
        $name = \strtolower($name);
37
38
        if (isset(self::$functionAliases[$name])) {
39
            return \call_user_func_array([$this, self::$functionAliases[$name]], $arguments);
40
        }
41
42
        throw new \BadMethodCallException('Method does not exist');
43
    }
44
45
    /**
46
     * @param string $name
47
     *
48
     * @return array|string|null
49
     */
50
    public function __get($name)
51
    {
52
        $nameOrig = $name;
53
        $name = \strtolower($name);
54
55
        switch ($name) {
56
            case 'xml':
57
                return $this->xml();
58
            case 'plaintext':
59
                return $this->text();
60
            case 'tag':
61
                return $this->node ? $this->node->nodeName : '';
62
            case 'attr':
63
                return $this->getAllAttributes();
64
            default:
65
                if ($this->node && \property_exists($this->node, $nameOrig)) {
66
                    return $this->node->{$nameOrig};
67
                }
68
69
                return $this->getAttribute($name);
70
        }
71
    }
72
73
    /**
74
     * @param string $selector
75
     * @param int    $idx
76
     *
77
     * @return SimpleXmlDomInterface|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface
78
     */
79
    public function __invoke($selector, $idx = null)
80
    {
81
        return $this->find($selector, $idx);
82
    }
83
84
    /**
85
     * @param string $name
86
     *
87
     * @return bool
88
     */
89 View Code Duplication
    public function __isset($name)
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...
90
    {
91
        $nameOrig = $name;
92
        $name = \strtolower($name);
93
94
        switch ($name) {
95
            case 'outertext':
96
            case 'outerhtml':
97
            case 'innertext':
98
            case 'innerhtml':
99
            case 'plaintext':
100
            case 'text':
101
            case 'tag':
102
                return true;
103
            default:
104
                if ($this->node && \property_exists($this->node, $nameOrig)) {
105
                    return isset($this->node->{$nameOrig});
106
                }
107
108
                return $this->hasAttribute($name);
109
        }
110
    }
111
112
    /**
113
     * @param string $name
114
     * @param mixed  $value
115
     *
116
     * @return SimpleXmlDomInterface|null
117
     */
118 View Code Duplication
    public function __set($name, $value)
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...
119
    {
120
        $nameOrig = $name;
121
        $name = \strtolower($name);
122
123
        switch ($name) {
124
            case 'outerhtml':
125
            case 'outertext':
126
                return $this->replaceNodeWithString($value);
127
            case 'innertext':
128
            case 'innerhtml':
129
                return $this->replaceChildWithString($value);
130
            case 'plaintext':
131
                return $this->replaceTextWithString($value);
132
            default:
133
                if ($this->node && \property_exists($this->node, $nameOrig)) {
134
                    return $this->node->{$nameOrig} = $value;
135
                }
136
137
                return $this->setAttribute($name, $value);
138
        }
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function __toString()
145
    {
146
        return $this->xml();
147
    }
148
149
    /**
150
     * @param string $name
151
     *
152
     * @return void
153
     */
154
    public function __unset($name)
155
    {
156
        /** @noinspection UnusedFunctionResultInspection */
157
        $this->removeAttribute($name);
158
    }
159
160
    abstract public function find(string $selector, $idx = null);
161
162
    abstract public function getAllAttributes();
163
164
    abstract public function getAttribute(string $name): string;
165
166
    abstract public function hasAttribute(string $name): bool;
167
168
    abstract public function innerXml(bool $multiDecodeNewHtmlEntity = false): string;
169
170
    abstract public function removeAttribute(string $name): SimpleXmlDomInterface;
171
172
    abstract protected function replaceChildWithString(string $string): SimpleXmlDomInterface;
173
174
    abstract protected function replaceNodeWithString(string $string): SimpleXmlDomInterface;
175
176
    abstract protected function replaceTextWithString($string): SimpleXmlDomInterface;
177
178
    abstract public function setAttribute(string $name, $value = null, bool $strict = false): SimpleXmlDomInterface;
179
180
    abstract public function text(): string;
181
182
    abstract public function xml(bool $multiDecodeNewHtmlEntity = false): string;
183
}
184