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