Code Duplication    Length = 154-155 lines in 2 locations

src/voku/helper/SimpleHtmlDomNode.php 1 location

@@ 10-163 (lines=154) @@
7
/**
8
 * {@inheritdoc}
9
 */
10
class SimpleHtmlDomNode extends AbstractSimpleHtmlDomNode implements SimpleHtmlDomNodeInterface
11
{
12
    /**
13
     * Find list of nodes with a CSS selector.
14
     *
15
     * @param string   $selector
16
     * @param int|null $idx
17
     *
18
     * @return SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>|SimpleHtmlDomNodeInterface[]|null
19
     */
20
    public function find(string $selector, $idx = null)
21
    {
22
        // init
23
        $elements = new static();
24
25
        foreach ($this as $node) {
26
            assert($node instanceof SimpleHtmlDomInterface);
27
            foreach ($node->find($selector) as $res) {
28
                $elements[] = $res;
29
            }
30
        }
31
32
        // return all elements
33
        if ($idx === null) {
34
            if (\count($elements) === 0) {
35
                return new SimpleHtmlDomNodeBlank();
36
            }
37
38
            return $elements;
39
        }
40
41
        // handle negative values
42
        if ($idx < 0) {
43
            $idx = \count($elements) + $idx;
44
        }
45
46
        // return one element
47
        return $elements[$idx] ?? null;
48
    }
49
50
    /**
51
     * Find nodes with a CSS selector.
52
     *
53
     * @param string $selector
54
     *
55
     * @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
56
     */
57
    public function findMulti(string $selector): SimpleHtmlDomNodeInterface
58
    {
59
        return $this->find($selector, null);
60
    }
61
62
    /**
63
     * Find nodes with a CSS selector.
64
     *
65
     * @param string $selector
66
     *
67
     * @return false|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
68
     */
69
    public function findMultiOrFalse(string $selector)
70
    {
71
        $return = $this->find($selector, null);
72
73
        if ($return instanceof SimpleHtmlDomNodeBlank) {
74
            return false;
75
        }
76
77
        return $return;
78
    }
79
80
    /**
81
     * Find one node with a CSS selector.
82
     *
83
     * @param string $selector
84
     *
85
     * @return SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>|null
86
     */
87
    public function findOne(string $selector)
88
    {
89
        return $this->find($selector, 0);
90
    }
91
92
    /**
93
     * Find one node with a CSS selector.
94
     *
95
     * @param string $selector
96
     *
97
     * @return false|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface>
98
     */
99
    public function findOneOrFalse(string $selector)
100
    {
101
        $return = $this->find($selector, 0);
102
103
        if ($return === null) {
104
            return false;
105
        }
106
107
        return $return;
108
    }
109
110
    /**
111
     * Get html of elements.
112
     *
113
     * @return string[]
114
     */
115
    public function innerHtml(): array
116
    {
117
        // init
118
        $html = [];
119
120
        foreach ($this as $node) {
121
            $html[] = $node->outertext;
122
        }
123
124
        return $html;
125
    }
126
127
    /**
128
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
129
     *
130
     * @return string[]
131
     */
132
    public function innertext()
133
    {
134
        return $this->innerHtml();
135
    }
136
137
    /**
138
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
139
     *
140
     * @return string[]
141
     */
142
    public function outertext()
143
    {
144
        return $this->innerHtml();
145
    }
146
147
    /**
148
     * Get plain text.
149
     *
150
     * @return string[]
151
     */
152
    public function text(): array
153
    {
154
        // init
155
        $text = [];
156
157
        foreach ($this as $node) {
158
            $text[] = $node->plaintext;
159
        }
160
161
        return $text;
162
    }
163
}
164

src/voku/helper/SimpleXmlDomNode.php 1 location

@@ 10-164 (lines=155) @@
7
/**
8
 * {@inheritdoc}
9
 */
10
class SimpleXmlDomNode extends AbstractSimpleXmlDomNode implements SimpleXmlDomNodeInterface
11
{
12
    /**
13
     * Find list of nodes with a CSS selector.
14
     *
15
     * @param string   $selector
16
     * @param int|null $idx
17
     *
18
     * @return SimpleXmlDomNodeInterface<SimpleXmlDomInterface>|SimpleXmlDomNodeInterface[]|null
19
     */
20
    public function find(string $selector, $idx = null)
21
    {
22
        // init
23
        $elements = new static();
24
25
        foreach ($this as $node) {
26
            assert($node instanceof SimpleXmlDomInterface);
27
            foreach ($node->find($selector) as $res) {
28
                $elements->append($res);
29
            }
30
        }
31
32
        // return all elements
33
        if ($idx === null) {
34
            if (\count($elements) === 0) {
35
                return new SimpleXmlDomNodeBlank();
36
            }
37
38
            return $elements;
39
        }
40
41
        // handle negative values
42
        if ($idx < 0) {
43
            $idx = \count($elements) + $idx;
44
        }
45
46
        // return one element
47
        return $elements[$idx] ?? null;
48
    }
49
50
    /**
51
     * Find nodes with a CSS selector.
52
     *
53
     * @param string $selector
54
     *
55
     * @return SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
56
     */
57
    public function findMulti(string $selector): SimpleXmlDomNodeInterface
58
    {
59
        return $this->find($selector, null);
60
    }
61
62
    /**
63
     * Find nodes with a CSS selector.
64
     *
65
     * @param string $selector
66
     *
67
     * @return false|SimpleXmlDomInterface[]|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
68
     */
69
    public function findMultiOrFalse(string $selector)
70
    {
71
        $return = $this->find($selector, null);
72
73
        if ($return instanceof SimpleXmlDomNodeBlank) {
74
            return false;
75
        }
76
77
        return $return;
78
    }
79
80
    /**
81
     * Find one node with a CSS selector.
82
     *
83
     * @param string $selector
84
     *
85
     * @return SimpleXmlDomNodeInterface<SimpleXmlDomInterface>|null
86
     */
87
    public function findOne(string $selector)
88
    {
89
        return $this->find($selector, 0);
90
    }
91
92
    /**
93
     * Find one node with a CSS selector or false, if no element is found.
94
     *
95
     * @param string $selector
96
     *
97
     * @return false|SimpleXmlDomNodeInterface<SimpleXmlDomInterface>
98
     */
99
    public function findOneOrFalse(string $selector)
100
    {
101
        $return = $this->find($selector, 0);
102
103
        /** @noinspection NullCoalescingOperatorCanBeUsedInspection */
104
        if ($return === null) {
105
            return false;
106
        }
107
108
        return $return;
109
    }
110
111
    /**
112
     * Get html of elements.
113
     *
114
     * @return string[]
115
     */
116
    public function innerHtml(): array
117
    {
118
        // init
119
        $html = [];
120
121
        foreach ($this as $node) {
122
            $html[] = $node->outertext;
123
        }
124
125
        return $html;
126
    }
127
128
    /**
129
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
130
     *
131
     * @return string[]
132
     */
133
    public function innertext()
134
    {
135
        return $this->innerHtml();
136
    }
137
138
    /**
139
     * alias for "$this->innerHtml()" (added for compatibly-reasons with v1.x)
140
     *
141
     * @return string[]
142
     */
143
    public function outertext()
144
    {
145
        return $this->innerHtml();
146
    }
147
148
    /**
149
     * Get plain text.
150
     *
151
     * @return string[]
152
     */
153
    public function text(): array
154
    {
155
        // init
156
        $text = [];
157
158
        foreach ($this as $node) {
159
            $text[] = $node->plaintext;
160
        }
161
162
        return $text;
163
    }
164
}
165