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) { |
|
|
|
|
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
|
|
|
|