Completed
Pull Request — master (#170)
by
unknown
05:36 queued 11s
created

HtmlSplitter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 85
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A by() 0 3 1
A updateSettings() 0 5 1
A split() 0 19 3
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 DOMXPath;
17
use DOMDocument;
18
use Algolia\ScoutExtended\Contracts\SplitterContract;
19
use Algolia\ScoutExtended\Splitters\HtmlSplitter\Node;
20
use Algolia\ScoutExtended\Contracts\SettingsUpdaterContract;
21
use Algolia\ScoutExtended\Splitters\HtmlSplitter\NodeCollection;
22
23
final class HtmlSplitter implements SplitterContract, SettingsUpdaterContract
24
{
25
    /**
26
     * The list of html tags.
27
     *
28
     * @var string[]
29
     */
30
    private $tags = [
31
        'h1',
32
        'h2',
33
        'h3',
34
        'h4',
35
        'h5',
36
        'h6',
37
        'p',
38
    ];
39
40
    /**
41
     * Creates a new instance of the class.
42
     *
43
     * @param array $tags
44
     *
45
     * @return void
46
     */
47 6
    public function __construct(array $tags = null)
48
    {
49 6
        if ($tags !== null) {
50 2
            $this->tags = $tags;
51
        }
52 6
    }
53
54
    /**
55
     * Acts a static factory.
56
     *
57
     * @param string|array<string> $tags
58
     *
59
     * @return static
60
     */
61 2
    public static function by($tags)
62
    {
63 2
        return new static((array) $tags);
64
    }
65
66
    /**
67
     * Splits the given value.
68
     *
69
     * @param object $searchable
70
     * @param string $value
71
     *
72
     * @return array
73
     */
74 5
    public function split($searchable, $value): array
75
    {
76 5
        $dom = new DOMDocument();
77
        //DOMDocument is only for HTML4, this exception is too avoid errors from HTML5
78
        try {
79 5
            $dom->loadHTML($value);
80 2
        } catch (\ErrorException $exception) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
81
        }
82
83 5
        $xpath = new DOMXpath($dom);
84 5
        $xpathQuery = '//'.implode(' | //', $this->tags);
85 5
        $nodes = $xpath->query($xpathQuery);
86 5
        $nodeCollection = new NodeCollection($this->tags);
87
88 5
        foreach ($nodes as $node) {
89 5
            $nodeCollection->push(new Node($node->nodeName, $node->textContent));
90
        }
91
92 5
        return $nodeCollection->toArray();
93
    }
94
95
    /**
96
     * Returns the updated version of the given settings.
97
     *
98
     * @param array $settings
99
     * @param string $key
100
     *
101
     * @return array
102
     */
103 1
    public function updateSettings(array $settings, string $key): array
104
    {
105 1
        $settings['customRanking'][] = 'asc('.$key.'.importance)';
106
107 1
        return $settings;
108
    }
109
}
110