|
1 | 1 | <?php
|
2 |
| -/** |
3 |
| - * Created by PhpStorm. |
4 |
| - * User: TianChen |
5 |
| - * Date: 17/1/7 |
6 |
| - * Time: 15:59 |
7 |
| - */ |
8 | 2 |
|
9 | 3 | namespace Hidehalo\Emoji\Features;
|
10 | 4 |
|
11 |
| - |
12 | 5 | use Hidehalo\Emoji\Unicode\Emoji;
|
| 6 | +use Hidehalo\Emoji\Features\Protocol\Utf8String; |
| 7 | +use Hidehalo\Emoji\Features\Protocol\ProtocolInterface; |
13 | 8 |
|
14 | 9 | class EmojiParser extends UnicodeParser
|
15 | 10 | {
|
16 | 11 | //http://apps.timwhitlock.info/emoji/tables/unicode
|
17 |
| - protected $maps = [ |
| 12 | + private $maps = [ |
18 | 13 | [0x0080,0x02AF],
|
19 | 14 | [0x0300,0x03FF],
|
20 | 15 | [0x0600,0x06FF],
|
@@ -42,13 +37,19 @@ class EmojiParser extends UnicodeParser
|
42 | 37 | [0x1F910,0x1F918],
|
43 | 38 | [0x1F980,0x1F9C0],
|
44 | 39 | ];
|
| 40 | + /** |
| 41 | + * @var string $pattern; |
| 42 | + */ |
45 | 43 | protected $pattern;
|
| 44 | + /** |
| 45 | + * @var ProtocolInterface $protocol |
| 46 | + */ |
| 47 | + protected $protocol; |
46 | 48 |
|
47 |
| - public function __construct($config = []) |
| 49 | + public function __construct(array $config = []) |
48 | 50 | {
|
49 |
| - if (isset($config['maps'])) { |
50 |
| - $this->maps = $config['maps']; |
51 |
| - } |
| 51 | + $protocolName = (isset($config['protocol_name'])) ? $config['protocol_name'] : Utf8String::class; |
| 52 | + $this->protocol = ProtocolFactory::generate($protocolName); |
52 | 53 | $this->pattern = $this->buildRegex($this->maps);
|
53 | 54 | }
|
54 | 55 |
|
@@ -92,18 +93,54 @@ public function clean($string)
|
92 | 93 | return $count>0?$result:$string;
|
93 | 94 | }
|
94 | 95 |
|
95 |
| - public function replace($string,callable $callback) |
| 96 | + public function utf8stringEncode($string) |
96 | 97 | {
|
97 |
| - if (!$this->pattern) { |
98 |
| - return $string; |
| 98 | + $protocol = $this->protocol; |
| 99 | + $encodeString = $this->replace($string, function ($matches) use ($protocol) { |
| 100 | + if (is_array($matches) && !empty($matches)) { |
| 101 | + foreach ($matches as &$matched) { |
| 102 | + $matched = $protocol->encode($matched); |
| 103 | + |
| 104 | + return $matched; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return ''; |
| 109 | + }); |
| 110 | + |
| 111 | + return $encodeString; |
| 112 | + } |
| 113 | + |
| 114 | + public function utf8StringDecode($string) |
| 115 | + { |
| 116 | + $protocol = $this->protocol; |
| 117 | + $decodeString = $this->replace($string, function ($matches) use ($protocol) { |
| 118 | + if (is_array($matches) && !empty($matches)) { |
| 119 | + foreach ($matches as &$matched) { |
| 120 | + $matched = $protocol->decode($matched); |
| 121 | + |
| 122 | + return $matched; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return ''; |
| 127 | + }, $protocol->getPattern()); |
| 128 | + |
| 129 | + return $decodeString; |
| 130 | + } |
| 131 | + |
| 132 | + private function replace($string,callable $callback, $pattern = '') |
| 133 | + { |
| 134 | + if (!$pattern) { |
| 135 | + $pattern = $this->pattern; |
99 | 136 | }
|
100 | 137 | $count = 0;
|
101 |
| - $result = preg_replace_callback($this->pattern,$callback,$string,-1,$count); |
| 138 | + $result = preg_replace_callback($pattern,$callback,$string,-1,$count); |
102 | 139 |
|
103 |
| - return $count>0?$result:$string; |
| 140 | + return $count>0 ? $result : $string; |
104 | 141 | }
|
105 | 142 |
|
106 |
| - protected function buildRegex($maps) |
| 143 | + private function buildRegex($maps) |
107 | 144 | {
|
108 | 145 | $pattern = '';
|
109 | 146 | if ($maps) {
|
|
0 commit comments