1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ReliqArts\Scavenger\Service; |
6
|
|
|
|
7
|
|
|
class Scanner |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* List of words (regex) we don't want in our scraps.'. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected array $badWords = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Scanner constructor. |
18
|
|
|
*/ |
19
|
|
|
public function __construct(array $badWords = []) |
20
|
|
|
{ |
21
|
|
|
$this->badWords = $badWords; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Determine whether a scrap data has bad words and therefore is unwanted. |
26
|
|
|
* |
27
|
|
|
* @param array $badWords List of words (regex) we don't want in our scraps.'. |
28
|
|
|
*/ |
29
|
|
|
public function hasBadWords(array $scrapData, array $badWords = []) |
30
|
|
|
{ |
31
|
|
|
$invalid = false; |
32
|
|
|
$badWords = array_merge($this->badWords, $badWords); |
33
|
|
|
|
34
|
|
|
if (count($badWords)) { |
35
|
|
|
$badWordsRegex = sprintf('/(%s)/i', implode(')|(', $badWords)); |
36
|
|
|
|
37
|
|
|
// check for bad words |
38
|
|
|
foreach ($scrapData as $attr => $value) { |
39
|
|
|
if (!ConfigProvider::isSpecialKey($attr) && preg_match($badWordsRegex, $value)) { |
40
|
|
|
$invalid = true; |
41
|
|
|
|
42
|
|
|
break; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $invalid; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Scour a string and pluck details. |
52
|
|
|
* |
53
|
|
|
* @param string $string the string to be scoured |
54
|
|
|
* @param array $map map to use for detail scouring |
55
|
|
|
* @param bool $retain whether to leave match in source string |
56
|
|
|
*/ |
57
|
|
|
public static function pluckDetails(string &$string, array $map = [], bool $retain = false): array |
58
|
|
|
{ |
59
|
|
|
$details = []; |
60
|
|
|
|
61
|
|
|
// Pluck mapped details from string |
62
|
|
|
foreach ($map as $attr => $regex) { |
63
|
|
|
// match and replace details in string |
64
|
|
|
$string = preg_replace_callback($regex, static function ($m) use ($attr, &$details, $retain) { |
65
|
|
|
// grab match |
66
|
|
|
$match = trim($m[0]); |
67
|
|
|
$details[$attr] = $match; |
68
|
|
|
// return match if it should be left in string |
69
|
|
|
if ($retain) { |
70
|
|
|
return $match; |
71
|
|
|
} |
72
|
|
|
// @noinspection PhpInconsistentReturnPointsInspection |
73
|
|
|
}, $string); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $details; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Searches array for needles. The first one found is returned. |
81
|
|
|
* If needles aren't supplied the first non-empty item in array is returned. |
82
|
|
|
* |
83
|
|
|
* @param array $haystack array to search |
84
|
|
|
* @param array $needles optional list of items to check for |
85
|
|
|
*/ |
86
|
|
|
public static function firstNonEmpty(array &$haystack, array $needles = []) |
87
|
|
|
{ |
88
|
|
|
if (!empty($needles)) { |
89
|
|
|
foreach ($needles as $value) { |
90
|
|
|
if (!empty($haystack[$value])) { |
91
|
|
|
return $haystack[$value]; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
foreach ($haystack as $value) { |
96
|
|
|
if (!empty($value)) { |
97
|
|
|
return $value; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Convert <br/> to newlines. |
107
|
|
|
*/ |
108
|
|
|
public static function br2nl(string $text): string |
109
|
|
|
{ |
110
|
|
|
return preg_replace('/<br[\\/]?>/', "\n", $text); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function cleanText(string $text): string |
114
|
|
|
{ |
115
|
|
|
return self::removeReturnsAndTabs(strip_tags($text)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Remove tabs and newlines from text. |
120
|
|
|
*/ |
121
|
|
|
private static function removeReturnsAndTabs(string $text): string |
122
|
|
|
{ |
123
|
|
|
$text = preg_replace('/\\s{2,}/', ' ', preg_replace("/[\r\n\t]+/", ' ', $text)); |
124
|
|
|
|
125
|
|
|
return str_replace(' / ', null, $text); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|