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