-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordExtractor.php
More file actions
156 lines (122 loc) · 4.24 KB
/
Copy pathWordExtractor.php
File metadata and controls
156 lines (122 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* Bit&Black Word Extract.
*
* @author Tobias Köngeter
* @copyright Copyright © Bit&Black
* @link https://www.bitandblack.com
* @license MIT
*/
namespace BitAndBlack\WordExtract;
use BitAndBlack\Helpers\XMLHelper;
use DOMDocument;
use DOMElement;
use DOMNode;
use DOMText;
class WordExtractor implements WordExtractorInterface
{
private string $pattern = '/(\w+\/\b\p{Ll}\p{L}*)|(\w+:\w+)|(\w+\*\w+)|\w+/u';
/**
* @param positive-int $minWordLength
*/
public function __construct(
private readonly int $minWordLength
) {
}
public function getWords(string $content): array
{
$words = [];
$wordExtractCallback = function (string $word) use (&$words): string {
if ('' !== $word) {
$words[] = $word;
}
return $word;
};
$this->getWithWordsHandled($content, $wordExtractCallback);
return $words;
}
public function getWithWordsHandled(string $content, callable $wordHandler, bool $ignoreHtml = false): string
{
$pregReplaceCallback = function (array $match) use ($wordHandler): string {
/** @var array<int, string> $match */
$word = $match[0];
if (mb_strlen($word) < $this->minWordLength) {
return $word;
}
return (string) $wordHandler($word);
};
$contentStriped = strip_tags($content);
$doesContentContainHtml = $content !== $contentStriped;
if (false === $doesContentContainHtml || true === $ignoreHtml) {
return (string) preg_replace_callback(
$this->pattern,
$pregReplaceCallback,
$content
);
}
$tempNodeName = 'temp';
$domDocument = new DOMDocument('1.0', 'UTF-8');
XMLHelper::loadHTML($domDocument, '<' . $tempNodeName . '>' . $content . '</' . $tempNodeName . '>');
$callback = function (string $content) use ($pregReplaceCallback): string {
return (string) preg_replace_callback(
$this->pattern,
$pregReplaceCallback,
$content
);
};
$this->extractDomContent(
$domDocument,
$callback
);
$tempNodeFirst = $domDocument->getElementsByTagName($tempNodeName)->item(0);
$hasChildNodes = null !== $tempNodeFirst && $tempNodeFirst->hasChildNodes();
$childNodes = true === $hasChildNodes ? $tempNodeFirst->childNodes : [];
$childNodesHtml = [];
foreach ($childNodes as $childNode) {
$childNodesHtml[] = $domDocument->saveHTML($childNode);
}
return implode('', $childNodesHtml);
}
/**
* @param callable(non-empty-string):string $callback
*/
private function extractDomContent(DOMDocument $domDocument, callable $callback): void
{
$attributesToHandle = [
'alt',
'title',
];
$traverse = static function (DOMNode $domNode) use ($attributesToHandle, &$traverse, $callback): void {
if ($domNode instanceof DOMText) {
$value = $domNode->nodeValue;
if (null === $value || '' === $value) {
return;
}
$value = $callback($value);
$domNode->nodeValue = $value;
return;
}
if ($domNode instanceof DomElement) {
foreach ($attributesToHandle as $attributeToHandle) {
if (false === $domNode->hasAttribute($attributeToHandle)) {
continue;
}
$value = $domNode->getAttribute($attributeToHandle);
if ('' === $value) {
continue;
}
$value = $callback($value);
$domNode->setAttribute($attributeToHandle, $value);
}
}
foreach ($domNode->childNodes as $childNode) {
$traverse($childNode);
}
};
$node = $domDocument->getElementsByTagName('temp')->item(0);
if (null === $node) {
return;
}
$traverse($node);
}
}