|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace UnicornFail\Emoji; |
|
6
|
|
|
|
|
7
|
|
|
use UnicornFail\Emoji\Token\AbstractEmojiToken; |
|
8
|
|
|
|
|
9
|
|
|
final class Converter |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var Parser|ParserInterface */ |
|
12
|
|
|
private $parser; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param mixed[]|\Traversable $configuration |
|
16
|
|
|
*/ |
|
17
|
606 |
|
public function __construct(?iterable $configuration = null, ?Dataset $dataset = null, ?ParserInterface $parser = null) |
|
18
|
|
|
{ |
|
19
|
606 |
|
$this->parser = $parser ?? new Parser($configuration, $dataset); |
|
20
|
192 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param mixed[]|\Traversable $configuration |
|
24
|
|
|
*/ |
|
25
|
63 |
|
public static function create(?iterable $configuration = null): self |
|
26
|
|
|
{ |
|
27
|
63 |
|
return new self($configuration); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
81 |
|
public function convert(string $input, ?int $type = null): string |
|
31
|
|
|
{ |
|
32
|
81 |
|
$stringableType = (int) $this->parser->getConfiguration()->get('stringableType'); |
|
33
|
|
|
|
|
34
|
|
|
// Parse. |
|
35
|
81 |
|
$tokens = $this->getParser()->parse($input); |
|
36
|
|
|
|
|
37
|
|
|
// Ensure tokens are set to the correct stringable type. |
|
38
|
81 |
|
if ($type !== null && $type !== $stringableType) { |
|
39
|
39 |
|
foreach (AbstractEmojiToken::filter($tokens) as $token) { |
|
40
|
39 |
|
$token->setStringableType($type); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
81 |
|
return \implode($tokens); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
3 |
|
public function convertToEmoticon(string $input): string |
|
48
|
|
|
{ |
|
49
|
3 |
|
return $this->convert($input, Lexer::T_EMOTICON); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
public function convertToHtml(string $input): string |
|
53
|
|
|
{ |
|
54
|
6 |
|
return $this->convert($input, Lexer::T_HTML_ENTITY); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
6 |
|
public function convertToShortcode(string $input): string |
|
58
|
|
|
{ |
|
59
|
6 |
|
return $this->convert($input, Lexer::T_SHORTCODE); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
15 |
|
public function convertToUnicode(string $input): string |
|
63
|
|
|
{ |
|
64
|
15 |
|
return $this->convert($input, Lexer::T_UNICODE); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
81 |
|
public function getParser(): ParserInterface |
|
68
|
|
|
{ |
|
69
|
81 |
|
return $this->parser; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|