1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Twitter\Object; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Twitter\TwitterMessage; |
7
|
|
|
use Twitter\TwitterMessageId; |
8
|
|
|
|
9
|
|
|
abstract class AbstractMessage implements TwitterMessage |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var TwitterMessageId |
13
|
|
|
*/ |
14
|
|
|
protected $id; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var TwitterUser |
18
|
|
|
*/ |
19
|
|
|
protected $sender; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $text; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var TwitterEntities |
28
|
|
|
*/ |
29
|
|
|
protected $entities; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \DateTimeInterface |
33
|
|
|
*/ |
34
|
|
|
protected $date; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Init. |
38
|
|
|
* |
39
|
|
|
* @param TwitterMessageId $id |
40
|
|
|
* @param TwitterUser $sender |
41
|
|
|
* @param string $text |
42
|
|
|
* @param TwitterEntities $entities |
43
|
|
|
* @param \DateTimeInterface $date |
44
|
|
|
*/ |
45
|
36 |
|
public function init( |
46
|
|
|
TwitterMessageId $id, |
47
|
|
|
TwitterUser $sender, |
48
|
|
|
$text, |
49
|
|
|
TwitterEntities $entities, |
50
|
|
|
\DateTimeInterface $date |
51
|
|
|
) { |
52
|
36 |
|
Assertion::eq(new \DateTimeZone('UTC'), $date->getTimezone()); |
53
|
|
|
|
54
|
36 |
|
$this->entities = $entities; |
55
|
36 |
|
$this->id = $id; |
56
|
36 |
|
$this->sender = $sender; |
57
|
36 |
|
$this->text = $text; |
58
|
36 |
|
$this->date = $date; |
59
|
36 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return TwitterMessageId |
63
|
|
|
*/ |
64
|
21 |
|
public function getId() |
65
|
|
|
{ |
66
|
21 |
|
return $this->id; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
21 |
|
public function getText() |
73
|
|
|
{ |
74
|
21 |
|
return $this->text; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return TwitterEntities |
79
|
|
|
*/ |
80
|
21 |
|
public function getEntities() |
81
|
|
|
{ |
82
|
21 |
|
return $this->entities; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return TwitterUser |
87
|
|
|
*/ |
88
|
21 |
|
public function getSender() |
89
|
|
|
{ |
90
|
21 |
|
return $this->sender; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return \DateTimeInterface |
96
|
|
|
*/ |
97
|
21 |
|
public function getDate() |
98
|
|
|
{ |
99
|
21 |
|
return $this->date; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string[] |
104
|
|
|
*/ |
105
|
6 |
|
public function getFormattedHashtags() |
106
|
|
|
{ |
107
|
6 |
|
$formattedHashtags = []; |
108
|
6 |
|
if ($this->entities && $this->entities->getHashtags()) { |
109
|
6 |
|
foreach ($this->entities->getHashtags() as $hashtag) { |
110
|
6 |
|
$formattedHashtags[] = $hashtag->__toString(); |
111
|
4 |
|
} |
112
|
4 |
|
} |
113
|
6 |
|
return $formattedHashtags; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string[] |
118
|
|
|
*/ |
119
|
6 |
|
public function getFormattedUserMentions() |
120
|
|
|
{ |
121
|
6 |
|
$formattedUserMentions = []; |
122
|
6 |
|
if ($this->entities && $this->entities->getUserMentions()) { |
123
|
6 |
|
foreach ($this->entities->getUserMentions() as $userMention) { |
124
|
6 |
|
$formattedUserMentions[] = $userMention->__toString(); |
125
|
4 |
|
} |
126
|
4 |
|
} |
127
|
6 |
|
return $formattedUserMentions; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
6 |
|
public function getStrippedText() |
134
|
|
|
{ |
135
|
6 |
|
$formattedHashtags = $this->getFormattedHashtags(); |
136
|
6 |
|
$formattedUserMentions = $this->getFormattedUserMentions(); |
137
|
6 |
|
return trim(str_ireplace(array_merge($formattedHashtags, $formattedUserMentions), '', $this->text)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $text |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
6 |
|
public function containsHashtag($text) |
145
|
|
|
{ |
146
|
6 |
|
if ($this->entities && $this->entities->getHashtags()) { |
147
|
6 |
|
foreach ($this->entities->getHashtags() as $hashtag) { |
148
|
6 |
|
if ($hashtag->getText() == $text) { |
149
|
6 |
|
return true; |
150
|
|
|
} |
151
|
3 |
|
} |
152
|
3 |
|
} |
153
|
5 |
|
return false; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|