1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Teebot\Api\Entity; |
4
|
|
|
|
5
|
|
|
class MessageEntity extends AbstractEntity |
6
|
|
|
{ |
7
|
|
|
const ENTITY_TYPE = 'MessageEntity'; |
8
|
|
|
|
9
|
|
|
const ENTITY_TYPE_COMMAND = 'Command'; |
10
|
|
|
|
11
|
|
|
const ENTITY_TYPE_HASHTAG = 'Hashtag'; |
12
|
|
|
|
13
|
|
|
const ENTITY_TYPE_MENTION = 'Mention'; |
14
|
|
|
|
15
|
|
|
const ENTITY_TYPE_EMAIL = 'Email'; |
16
|
|
|
|
17
|
|
|
const TYPE_MENTION = 'mention'; |
18
|
|
|
|
19
|
|
|
const TYPE_HASHTAG = 'hashtag'; |
20
|
|
|
|
21
|
|
|
const TYPE_BOT_COMMAND = 'bot_command'; |
22
|
|
|
|
23
|
|
|
const TYPE_URL = 'url'; |
24
|
|
|
|
25
|
|
|
const TYPE_EMAIL = 'email'; |
26
|
|
|
|
27
|
|
|
const TYPE_BOLD = 'bold'; |
28
|
|
|
|
29
|
|
|
const TYPE_ITALIC = 'italic'; |
30
|
|
|
|
31
|
|
|
const TYPE_CODE = 'code'; |
32
|
|
|
|
33
|
|
|
const TYPE_PRE = 'pre'; |
34
|
|
|
|
35
|
|
|
const TYPE_TEXT_LINK = 'text_link'; |
36
|
|
|
|
37
|
|
|
protected $source; |
38
|
|
|
|
39
|
|
|
protected $args; |
40
|
|
|
|
41
|
|
|
protected $type; |
42
|
|
|
|
43
|
|
|
protected $offset = 0; |
44
|
|
|
|
45
|
|
|
protected $length = 0; |
46
|
|
|
|
47
|
|
|
protected $url; |
48
|
|
|
|
49
|
|
|
protected $next = null; |
50
|
|
|
|
51
|
|
|
public function __construct(array $data = []) |
52
|
|
|
{ |
53
|
|
|
parent::__construct($data); |
54
|
|
|
|
55
|
|
|
$this->parseSource(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getEntityType() |
59
|
|
|
{ |
60
|
|
|
$type = static::ENTITY_TYPE; |
61
|
|
|
|
62
|
|
|
switch ($this->type) { |
63
|
|
|
case self::TYPE_BOT_COMMAND: |
64
|
|
|
$type = self::ENTITY_TYPE_COMMAND; |
65
|
|
|
break; |
66
|
|
|
case self::TYPE_HASHTAG: |
67
|
|
|
$type = self::ENTITY_TYPE_HASHTAG; |
68
|
|
|
break; |
69
|
|
|
case self::TYPE_MENTION: |
70
|
|
|
$type = self::ENTITY_TYPE_MENTION; |
71
|
|
|
break; |
72
|
|
|
case self::TYPE_EMAIL: |
73
|
|
|
$type = self::ENTITY_TYPE_EMAIL; |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $type; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function isNativeCommand() |
81
|
|
|
{ |
82
|
|
|
return $this->type === self::TYPE_BOT_COMMAND; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function isHashtag() |
86
|
|
|
{ |
87
|
|
|
return $this->type === self::TYPE_HASHTAG; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function parseSource() |
91
|
|
|
{ |
92
|
|
|
if ($this->isNativeCommand()) { |
93
|
|
|
$args = mb_substr($this->source, $this->offset, $this->offset + $this->length, 'UTF-8'); |
94
|
|
|
|
95
|
|
|
if (preg_match('/([^\/]+)/', $args, $matches)) { |
96
|
|
|
$this->args = trim($matches[1]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->source = mb_substr($this->source, $this->offset, $this->length, 'UTF-8'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
public function getSource() |
107
|
|
|
{ |
108
|
|
|
return $this->source; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
public function getType() |
115
|
|
|
{ |
116
|
|
|
return $this->type; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
|
|
public function getOffset() |
123
|
|
|
{ |
124
|
|
|
return $this->offset; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return int |
129
|
|
|
*/ |
130
|
|
|
public function getLength() |
131
|
|
|
{ |
132
|
|
|
return $this->length; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return mixed |
137
|
|
|
*/ |
138
|
|
|
public function getUrl() |
139
|
|
|
{ |
140
|
|
|
return $this->url; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getCommand() |
144
|
|
|
{ |
145
|
|
|
if (!$this->isNativeCommand()) { |
146
|
|
|
return null; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return ltrim($this->source, '/'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getHashtag() |
153
|
|
|
{ |
154
|
|
|
if (!$this->isHashtag()) { |
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return ltrim($this->source, '#'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getArgs() |
162
|
|
|
{ |
163
|
|
|
return $this->args; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return null |
168
|
|
|
*/ |
169
|
|
|
public function getNext() |
170
|
|
|
{ |
171
|
|
|
return $this->next; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param null $next |
176
|
|
|
*/ |
177
|
|
|
public function setNext($next) |
178
|
|
|
{ |
179
|
|
|
$this->next = $next; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|