HtmlSplitter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 67
ccs 15
cts 16
cp 0.9375
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A by() 0 3 1
A split() 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 Algolia\ScoutExtended\Contracts\SplitterContract;
17
use DOMDocument;
18
19
class HtmlSplitter implements SplitterContract
20
{
21
    /**
22
     * The list of html tags.
23
     *
24
     * @var string[]
25
     */
26
    protected $tags = [
27
        'h1',
28
        'h2',
29
        'h3',
30
        'h4',
31
        'h5',
32
        'p',
33
    ];
34
35
    /**
36
     * Creates a new instance of the class.
37
     *
38
     * @param array $tags
39
     *
40
     * @return void
41
     */
42 3
    public function __construct(array $tags = null)
43
    {
44 3
        if ($tags !== null) {
45 2
            $this->tags = $tags;
46
        }
47 3
    }
48
49
    /**
50
     * Acts a static factory.
51
     *
52
     * @param  string|array $tags
53
     *
54
     * @return static
55
     */
56 2
    public static function by($tags)
57
    {
58 2
        return new static((array) $tags);
59
    }
60
61
    /**
62
     * Splits the given value.
63
     *
64
     * @param  object $searchable
65
     * @param  string $value
66
     *
67
     * @return array
68
     */
69 3
    public function split($searchable, $value): array
70
    {
71 3
        $dom = new DOMDocument();
72 3
        $dom->loadHTML($value);
73 3
        $values = [];
74
75 3
        foreach ($this->tags as $tag) {
76 3
            foreach ($dom->getElementsByTagName($tag) as $node) {
77 3
                $values[] = $node->textContent;
78
79 3
                while (($node = $node->nextSibling) && $node->nodeName !== $tag) {
80
                    $values[] = $node->textContent;
81
                }
82
            }
83
        }
84
85 3
        return $values;
86
    }
87
}
88