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