Completed
Push — master ( 3d4df1...a30943 )
by Taosikai
25:28
created

Content::parseFaceContents()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 8
nop 0
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the slince/smartqq package.
4
 *
5
 * (c) Slince <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Slince\SmartQQ\Message;
12
13
class Content
14
{
15
    /**
16
     * 消息内容.
17
     *
18
     * @var string
19
     */
20
    protected $content;
21
22
    /**
23
     * 消息内容字体.
24
     *
25
     * @var Font
26
     */
27
    protected $font;
28
29
    /**
30
     * 表情
31
     * @var array
32
     */
33
    protected static $faceTexts = [
34
        "微笑" ,"撇嘴" ,"色" ,"发呆" ,"得意" ,"流泪" ,"害羞" ,"闭嘴" ,"睡" ,"大哭" ,"尴尬" ,"发怒" ,"调皮" ,"呲牙" ,"惊讶" ,"难过" ,"酷" ,"冷汗" ,"抓狂" ,"吐",
35
        "偷笑" ,"可爱" ,"白眼" ,"傲慢" ,"饥饿" ,"困" ,"惊恐" ,"流汗" ,"憨笑" ,"大兵" ,"奋斗" ,"咒骂" ,"疑问" ,"嘘" ,"晕" ,"折磨" ,"衰" ,"骷髅" ,"敲打" ,"再见",
36
        "擦汗" ,"抠鼻" ,"鼓掌" ,"糗大了" ,"坏笑" ,"左哼哼" ,"右哼哼" ,"哈欠" ,"鄙视" ,"委屈" ,"快哭了" ,"阴险" ,"亲亲" ,"吓" ,"可怜" ,"菜刀" ,"西瓜" ,"啤酒" ,"篮球" ,"乒乓",
37
        "咖啡" ,"饭" ,"猪头" ,"玫瑰" ,"凋谢" ,"示爱" ,"爱心" ,"心碎" ,"蛋糕" ,"闪电" ,"炸弹" ,"刀" ,"足球" ,"瓢虫" ,"便便" ,"月亮" ,"太阳" ,"礼物" ,"拥抱" ,"强",
38
        "弱" ,"握手" ,"胜利" ,"抱拳" ,"勾引" ,"拳头" ,"差劲" ,"爱你" ,"NO" ,"OK" ,"爱情" ,"飞吻" ,"跳跳" ,"发抖" ,"怄火" ,"转圈" ,"磕头" ,"回头" ,"跳绳" ,"挥手",
39
        "激动", "街舞", "献吻", "左太极", "右太极", "双喜", "鞭炮", "灯笼", "发财", "K歌", "购物", "邮件", "帅", "喝彩","祈祷","爆筋","棒棒糖","喝奶","下面","香蕉",
40
        "飞机","开车","左车头","车厢","右车头","多云","下雨","钞票","熊猫","灯泡","风车","闹钟","打伞","彩球","钻戒","沙发","纸巾","药","手枪","青蛙"
41
    ];
42
43
    /**
44
     * 表情索引
45
     * @var array
46
     */
47
    protected static $faceIndexs = [
48
        14,1,2,3,4,5,6,7,8,9,10,11,12,13,
49
        0,50,51,96,53,54,73,74,75,76,77,78,55,56,
50
        57,58,79,80,81,82,83,84,85,86,87,88,97,98,
51
        99,100,101,102,103,104,105,106,107,108,109,110,111,112,
52
        32,113,114,115,63,64,59,33,34,116,36,37,38,91,
53
        92,93,29,117,72,45,42,39,62,46,47,71,95,118,
54
        119,120,121,122,123,124,27,21,23,25,26,125,126,127,
55
        128,129,130,131,132,133,134,136,137,138,139,140,141,142,
56
        143,144,145,146,147,148,149,150,151,152,153,154,155,156,
57
        157,158,159,160,161,162,163,164,165,166,167,168,169,170
58
    ];
59
60
    /**
61
     * Content constructor.
62
     *
63
     * @param string $content 消息内容正文
64
     * @param Font   $font    消息内容字体
65
     */
66
    public function __construct($content, Font $font = null)
67
    {
68
        $this->content = $content;
69
        $this->font = $font;
70
    }
71
72
    /**
73
     * 魔术方法.
74
     *
75
     * @return string
76
     */
77
    public function __toString()
78
    {
79
        return \GuzzleHttp\json_encode($this->parseFaceContents());
80
    }
81
82
    protected function parseFaceContents()
83
    {
84
        $content = preg_replace('#\[([A-Z\x{4e00}-\x{9fa5}]{1,20}?)\]#ui', '@#[$1]@#', $this->getContent());
85
        $chunks = explode('@#', $content);
86
        $contents = [];
87
        foreach ($chunks as $chunk) {
88
            if (!$chunk) {
89
                continue;
90
            }
91
            $faceText = trim($chunk, '[]');
92
            if ($faceId = static::searchFaceId($faceText)) {
93
                $contents[] = ['face', $faceId];
94
            } else {
95
                $contents[] = $chunk;
96
            }
97
        }
98
        $font = $this->getFont() ?: Font::createDefault();
99
        $contents[] = [
100
            'font',
101
            $font->toArray(),
102
        ];
103
        return $contents;
104
    }
105
106
    /**
107
     * 查找表情对应的ID
108
     * @param string $faceText
109
     * @return int|null
110
     */
111
    protected static function searchFaceId($faceText)
112
    {
113
        if (($key = array_search($faceText, static::$faceTexts)) !== false) {
114
            return isset(static::$faceIndexs[$key]) ? static::$faceIndexs[$key] : null;
115
        }
116
        return null;
117
    }
118
119
    /**
120
     * @param string $content
121
     */
122
    public function setContent($content)
123
    {
124
        $this->content = $content;
125
    }
126
127
    /**
128
     * @param Font $font
129
     */
130
    public function setFont($font)
131
    {
132
        $this->font = $font;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getContent()
139
    {
140
        return $this->content;
141
    }
142
143
    /**
144
     * @return Font
145
     */
146
    public function getFont()
147
    {
148
        return $this->font;
149
    }
150
}
151