1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Goose; |
4
|
|
|
|
5
|
|
|
use Goose\Text\StopWords; |
6
|
|
|
use Goose\Modules\ModuleInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Configuration |
10
|
|
|
* |
11
|
|
|
* @package Goose |
12
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
13
|
|
|
*/ |
14
|
|
|
class Configuration { |
15
|
|
|
/** @var mixed[] */ |
16
|
|
|
protected $options = [ |
17
|
|
|
'language' => 'en', |
18
|
|
|
'image_min_bytes' => 4500, |
19
|
|
|
'image_max_bytes' => 5242880, |
20
|
|
|
'image_min_width' => 120, |
21
|
|
|
'image_min_height' => 120, |
22
|
|
|
'image_fetch_best' => true, |
23
|
|
|
'image_fetch_all' => false, |
24
|
|
|
/** @see http://guzzle.readthedocs.io/en/stable/request-options.html */ |
25
|
|
|
'browser' => [ |
26
|
|
|
'timeout' => 60, |
27
|
|
|
'connect_timeout' => 30, |
28
|
|
|
'headers' => [ |
29
|
|
|
'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36', |
30
|
|
|
'Referer' => 'https://www.google.com/', |
31
|
|
|
], |
32
|
|
|
] |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** @var mixed[] */ |
36
|
|
|
protected $modules = [ |
37
|
|
|
'cleaners' => [ |
38
|
|
|
'\Goose\Modules\Cleaners\DocumentCleaner', |
39
|
|
|
], |
40
|
|
|
'extractors' => [ |
41
|
|
|
'\Goose\Modules\Extractors\MetaExtractor', |
42
|
|
|
'\Goose\Modules\Extractors\ContentExtractor', |
43
|
|
|
'\Goose\Modules\Extractors\ImageExtractor', |
44
|
|
|
'\Goose\Modules\Extractors\PublishDateExtractor', |
45
|
|
|
'\Goose\Modules\Extractors\AdditionalDataExtractor', |
46
|
|
|
], |
47
|
|
|
'formatters' => [ |
48
|
|
|
'\Goose\Modules\Formatters\OutputFormatter', |
49
|
|
|
], |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param mixed[] $options |
54
|
|
|
*/ |
55
|
|
|
public function __construct(array $options = []) { |
56
|
|
|
$this->options = array_replace_recursive($this->options, $options); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $option |
61
|
|
|
* |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function get(string $option) { |
65
|
|
|
if (isset($this->options[$option])) { |
66
|
|
|
return $this->options[$option]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $option |
74
|
|
|
* @param mixed $value |
75
|
|
|
* |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
|
|
public function set(string $option, $value): self { |
79
|
|
|
$this->options[$option] = $value; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $category |
86
|
|
|
* |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
|
|
public function getModules(string $category) { |
90
|
|
|
if (isset($this->modules[$category])) { |
91
|
|
|
return $this->modules[$category]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $category |
99
|
|
|
* @param string[] $classes |
100
|
|
|
* |
101
|
|
|
* @return self |
102
|
|
|
*/ |
103
|
|
|
public function setModules(string $category, array $classes): self { |
104
|
|
|
if ($this->areValidModules($category, $classes)) { |
105
|
|
|
$this->modules[$category] = $classes; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $category |
113
|
|
|
* @param string $class |
114
|
|
|
* |
115
|
|
|
* @return self |
116
|
|
|
*/ |
117
|
|
|
public function addModule(string $category, string $class): self { |
118
|
|
|
if ($this->isValidModule($category, $class)) { |
119
|
|
|
$this->modules[$category][] = $class; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $category |
127
|
|
|
* @param string $class |
128
|
|
|
* |
129
|
|
|
* @return self |
130
|
|
|
*/ |
131
|
|
|
public function removeModule(string $category, string $class): self { |
132
|
|
|
if (isset($this->modules[$category])) { |
133
|
|
|
$key = array_search($class, $this->modules[$category]); |
134
|
|
|
|
135
|
|
|
if ($key !== false) { |
136
|
|
|
unset($this->modules[$category][$key]); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $category |
145
|
|
|
* @param string $class |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
public function isValidModule(string $category, string $class): bool { |
150
|
|
|
if (isset($this->modules[$category]) |
151
|
|
|
&& is_subclass_of($class, ModuleInterface::class)) { |
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $category |
160
|
|
|
* @param string[] $classes |
161
|
|
|
* |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function areValidModules(string $category, array $classes): bool { |
165
|
|
|
foreach ($classes as $class) { |
166
|
|
|
if (!$this->isValidModule($category, $class)) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** @var StopWords|null */ |
175
|
|
|
protected $stopWords; |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param StopWords|null $stopWords |
179
|
|
|
* |
180
|
|
|
* @return self |
181
|
|
|
*/ |
182
|
|
|
public function setStopWords(StopWords $stopWords = null): ?self { |
183
|
|
|
$this->stopWords = $stopWords; |
184
|
|
|
|
185
|
|
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/* |
189
|
|
|
* @return StopWords |
190
|
|
|
*/ |
191
|
|
|
public function getStopWords(): StopWords { |
192
|
|
|
if (is_null($this->stopWords)) { |
193
|
|
|
$this->stopWords = new StopWords($this); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $this->stopWords; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|