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