1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace League\Emoji; |
6
|
|
|
|
7
|
|
|
use League\Emoji\Environment\Environment; |
8
|
|
|
use League\Emoji\Environment\EnvironmentInterface; |
9
|
|
|
use League\Emoji\Parser\EmojiParser; |
10
|
|
|
use League\Emoji\Parser\EmojiParserInterface; |
11
|
|
|
use League\Emoji\Renderer\DocumentRenderer; |
12
|
|
|
use League\Emoji\Renderer\DocumentRendererInterface; |
13
|
|
|
|
14
|
|
|
class EmojiConverter implements EmojiConverterInterface |
15
|
|
|
{ |
16
|
|
|
/** @var EnvironmentInterface */ |
17
|
|
|
private $environment; |
18
|
|
|
|
19
|
|
|
/** @var EmojiParserInterface */ |
20
|
|
|
private $parser; |
21
|
|
|
|
22
|
|
|
/** @var DocumentRendererInterface */ |
23
|
|
|
private $renderer; |
24
|
|
|
|
25
|
630 |
|
public function __construct(EnvironmentInterface $environment, ?EmojiParserInterface $parser = null, ?DocumentRendererInterface $renderer = null) |
26
|
|
|
{ |
27
|
630 |
|
$this->environment = $environment; |
28
|
630 |
|
$this->parser = $parser ?? new EmojiParser($environment); |
29
|
630 |
|
$this->renderer = $renderer ?? new DocumentRenderer($environment); |
30
|
630 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array<string, mixed> $config |
34
|
|
|
*/ |
35
|
81 |
|
public static function create(array $config = []): EmojiConverterInterface |
36
|
|
|
{ |
37
|
81 |
|
return new self(Environment::create($config)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Converts all HTML entities, shortcodes or emoticons to emojis (unicode). |
42
|
|
|
* |
43
|
|
|
* @see EmojiConverterInterface::convert |
44
|
|
|
* |
45
|
|
|
* @throws \RuntimeException |
46
|
|
|
*/ |
47
|
3 |
|
public function __invoke(string $input): string |
48
|
|
|
{ |
49
|
3 |
|
return $this->convert($input); |
50
|
|
|
} |
51
|
|
|
|
52
|
231 |
|
public function convert(string $input): string |
53
|
|
|
{ |
54
|
231 |
|
$document = $this->parser->parse($input); |
55
|
|
|
|
56
|
231 |
|
return $this->renderer->renderDocument($document); |
57
|
|
|
} |
58
|
|
|
|
59
|
531 |
|
public function getEnvironment(): EnvironmentInterface |
60
|
|
|
{ |
61
|
531 |
|
return $this->environment; |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
public function getParser(): EmojiParserInterface |
65
|
|
|
{ |
66
|
3 |
|
return $this->parser; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
public function getRenderer(): DocumentRendererInterface |
70
|
|
|
{ |
71
|
3 |
|
return $this->renderer; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|