Completed
Push — master ( a30943...b7600b )
by Taosikai
14:47
created

Content::searchFaceText()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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