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