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