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
|
|
|
$start = $this->offset + $this->length; |
94
|
|
|
$length = mb_strlen($this->source, 'UTF-8'); |
95
|
|
|
$this->args = trim(mb_substr($this->source, $start, $length, 'UTF-8')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->source = mb_substr($this->source, $this->offset, $this->length, 'UTF-8'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
|
|
public function getSource() |
105
|
|
|
{ |
106
|
|
|
return $this->source; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function getType() |
113
|
|
|
{ |
114
|
|
|
return $this->type; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
|
|
public function getOffset() |
121
|
|
|
{ |
122
|
|
|
return $this->offset; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return int |
127
|
|
|
*/ |
128
|
|
|
public function getLength() |
129
|
|
|
{ |
130
|
|
|
return $this->length; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return mixed |
135
|
|
|
*/ |
136
|
|
|
public function getUrl() |
137
|
|
|
{ |
138
|
|
|
return $this->url; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getCommand() |
142
|
|
|
{ |
143
|
|
|
if (!$this->isNativeCommand()) { |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return ltrim($this->source, '/'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getHashtag() |
151
|
|
|
{ |
152
|
|
|
if (!$this->isHashtag()) { |
153
|
|
|
return null; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return ltrim($this->source, '#'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function getArgs() |
160
|
|
|
{ |
161
|
|
|
return $this->args; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return null |
166
|
|
|
*/ |
167
|
|
|
public function getNext() |
168
|
|
|
{ |
169
|
|
|
return $this->next; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param null $next |
|
|
|
|
174
|
|
|
*/ |
175
|
|
|
public function setNext($next) |
176
|
|
|
{ |
177
|
|
|
$this->next = $next; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|