1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Teebot\Api\Entity; |
4
|
|
|
|
5
|
|
|
class MessageEntityArray extends AbstractEntity |
6
|
|
|
{ |
7
|
|
|
const ENTITY_TYPE = 'MessageEntityArray'; |
8
|
|
|
|
9
|
|
|
protected $entities; |
10
|
|
|
|
11
|
|
|
protected $supportedTypes = [ |
12
|
|
|
MessageEntity::TYPE_BOLD, |
13
|
|
|
MessageEntity::TYPE_BOT_COMMAND, |
14
|
|
|
MessageEntity::TYPE_CODE, |
15
|
|
|
MessageEntity::TYPE_EMAIL, |
16
|
|
|
MessageEntity::TYPE_HASHTAG, |
17
|
|
|
MessageEntity::TYPE_ITALIC, |
18
|
|
|
MessageEntity::TYPE_MENTION, |
19
|
|
|
MessageEntity::TYPE_PRE, |
20
|
|
|
MessageEntity::TYPE_TEXT_LINK, |
21
|
|
|
MessageEntity::TYPE_URL |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
public function __construct(array $data) |
25
|
|
|
{ |
26
|
|
|
if (empty($data)) { |
27
|
|
|
return; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$this->setEntities($data); |
31
|
|
|
|
32
|
|
|
parent::__construct($data); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function getEntities() |
39
|
|
|
{ |
40
|
|
|
return $this->entities; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $entities |
45
|
|
|
*/ |
46
|
|
|
public function setEntities(array $entities) |
47
|
|
|
{ |
48
|
|
|
$this->entities = $entities; |
49
|
|
|
|
50
|
|
|
if (!empty($entities)) { |
51
|
|
|
$this->entities = []; |
52
|
|
|
|
53
|
|
|
$previous = null; |
54
|
|
|
|
55
|
|
|
foreach ($entities as $entity) { |
56
|
|
|
$messageEntity = new MessageEntity($entity); |
57
|
|
|
|
58
|
|
|
if ($previous instanceof MessageEntity) { |
59
|
|
|
$previous->setNext($messageEntity); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$previous = $messageEntity; |
63
|
|
|
|
64
|
|
|
$this->entities[] = $messageEntity; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getEntitiesByType($type) |
70
|
|
|
{ |
71
|
|
|
$entities = []; |
72
|
|
|
|
73
|
|
|
if (!in_array($type, $this->supportedTypes)) { |
74
|
|
|
return $entities; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
foreach ($this->entities as $entity) { |
78
|
|
|
|
79
|
|
|
/** @var MessageEntity $entity */ |
80
|
|
|
if ($entity->getType() === $type) { |
81
|
|
|
$entities[] = $entity; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $entities; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|