Code Duplication    Length = 149-150 lines in 2 locations

src/voku/helper/SimpleHtmlDomNode.php 1 location

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

src/voku/helper/SimpleXmlDomNode.php 1 location

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