1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chemaclass\StockTicker\Domain\Crawler\Site\Shared; |
6
|
|
|
|
7
|
|
|
use DateTimeImmutable; |
8
|
|
|
use DateTimeZone; |
9
|
|
|
|
10
|
|
|
use function array_slice; |
11
|
|
|
|
12
|
|
|
final class NewsNormalizer implements NewsNormalizerInterface |
13
|
|
|
{ |
14
|
|
|
private const NORMALIZED_DATETIME_FORMAT = 'Y-m-d H:i:s'; |
15
|
|
|
|
16
|
|
|
private const DEFAULT_MAX_TEXT_LENGTH_CHARS = 1500; |
17
|
|
|
|
18
|
|
|
private DateTimeZone $timeZone; |
19
|
|
|
|
20
|
|
|
private ?int $maxNewsToFetch; |
21
|
|
|
|
22
|
8 |
|
private int $maxTextLengthChars; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
DateTimeZone $timeZone, |
26
|
|
|
?int $maxNewsToFetch = null, |
27
|
8 |
|
int $maxTextLengthChars = self::DEFAULT_MAX_TEXT_LENGTH_CHARS, |
28
|
8 |
|
) { |
29
|
8 |
|
$this->timeZone = $timeZone; |
30
|
8 |
|
$this->maxNewsToFetch = $maxNewsToFetch; |
31
|
|
|
$this->maxTextLengthChars = $maxTextLengthChars; |
32
|
6 |
|
} |
33
|
|
|
|
34
|
6 |
|
public function normalizeDateTime(DateTimeImmutable $dt): string |
35
|
|
|
{ |
36
|
6 |
|
$dt->setTimeZone($this->timeZone); |
37
|
|
|
|
38
|
|
|
return $dt->format(self::NORMALIZED_DATETIME_FORMAT); |
39
|
6 |
|
} |
40
|
|
|
|
41
|
6 |
|
public function getTimeZoneName(): string |
42
|
|
|
{ |
43
|
|
|
return $this->timeZone->getName(); |
44
|
6 |
|
} |
45
|
|
|
|
46
|
6 |
|
public function normalizeText(string $text): string |
47
|
6 |
|
{ |
48
|
|
|
if (mb_strlen($text) < $this->maxTextLengthChars) { |
49
|
|
|
return $text; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return mb_strimwidth($text, 0, $this->maxTextLengthChars, '...'); |
53
|
8 |
|
} |
54
|
|
|
|
55
|
8 |
|
public function limitByMaxToFetch(array $info): array |
56
|
4 |
|
{ |
57
|
|
|
if ($this->maxNewsToFetch === null) { |
58
|
|
|
return $info; |
59
|
4 |
|
} |
60
|
|
|
|
61
|
|
|
return array_slice($info, 0, $this->maxNewsToFetch); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|