Test Failed
Push — develop ( 2ad78c...b57f29 )
by Nuno
04:26
created

HtmlSplitter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
eloc 20
dl 0
loc 66
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A by() 0 3 1
A __invoke() 0 17 5
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended\Splitters;
15
16
use DOMDocument;
17
18
class HtmlSplitter
19
{
20
    /**
21
     * The list of html tags.
22
     *
23
     * @var string[]
24
     */
25
    protected $tags = [
26
        'h1',
27
        'h2',
28
        'h3',
29
        'h4',
30
        'h5',
31
        'p',
32
    ];
33
34
    /**
35
     * Creates a new instance of the class.
36
     *
37
     * @param array $tags
38
     *
39
     * @return void
40
     */
41
    public function __construct(array $tags = null)
42
    {
43
        if ($tags !== null) {
44
            $this->tags = $tags;
45
        }
46
    }
47
48
    /**
49
     * Acts a static factory.
50
     *
51
     * @param  string|array $tags
52
     *
53
     * @return static
54
     */
55
    public static function by($tags)
56
    {
57
        return new static((array) $tags);
58
    }
59
60
    /**
61
     * Splits the given value.
62
     *
63
     * @param  string $value
64
     *
65
     * @return array
66
     */
67
    public function __invoke($value): array
68
    {
69
        $dom = new DOMDocument();
70
        $dom->loadHTML($value);
71
        $values = [];
72
73
        foreach ($this->tags as $tag) {
74
            foreach ($dom->getElementsByTagName($tag) as $node) {
75
                $values[] = $node->textContent;
76
77
                while (($node = $node->nextSibling) && $node->nodeName !== $tag) {
78
                    $values[] = $node->textContent;
79
                }
80
            }
81
        }
82
83
        return $values;
84
    }
85
}
86