1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Platform package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Karma\Platform\Io; |
11
|
|
|
|
12
|
|
|
use Carbon\Carbon; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class AbstractMessage |
16
|
|
|
* @package Karma\Platform\Io |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractMessage implements MessageInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Message edit timeout |
22
|
|
|
*/ |
23
|
|
|
protected const MESSAGE_EDIT_TIMEOUT = -1; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ChannelInterface |
27
|
|
|
*/ |
28
|
|
|
protected $channel; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var UserInterface |
32
|
|
|
*/ |
33
|
|
|
protected $author; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $id; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $body; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array|UserInterface[] |
47
|
|
|
*/ |
48
|
|
|
protected $mentions = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var \DateTimeInterface |
52
|
|
|
*/ |
53
|
|
|
protected $createdAt; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* AbstractMessage constructor. |
57
|
|
|
* @param ChannelInterface $channel |
58
|
|
|
* @param UserInterface $author |
59
|
|
|
* @param string $id |
60
|
|
|
* @param string $body |
61
|
|
|
*/ |
62
|
|
|
public function __construct(ChannelInterface $channel, UserInterface $author, string $id, string $body) |
63
|
|
|
{ |
64
|
|
|
$this->channel = $channel; |
65
|
|
|
$this->author = $author; |
66
|
|
|
$this->id = $id; |
67
|
|
|
$this->body = $body; |
68
|
|
|
$this->createdAt = Carbon::now(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function canBeUpdated(): bool |
75
|
|
|
{ |
76
|
|
|
switch (static::MESSAGE_EDIT_TIMEOUT) { |
77
|
|
|
case 0: |
78
|
|
|
return true; |
79
|
|
|
case -1; |
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return Carbon::createFromTimestamp($this->at()->getTimestamp(), $this->at()->getTimezone()) |
84
|
|
|
->addSeconds(static::MESSAGE_EDIT_TIMEOUT) > Carbon::now($this->at()->getTimezone()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return \DateTimeInterface |
89
|
|
|
*/ |
90
|
|
|
public function at(): \DateTimeInterface |
91
|
|
|
{ |
92
|
|
|
return $this->createdAt; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function __debugInfo(): array |
99
|
|
|
{ |
100
|
|
|
return [ |
101
|
|
|
'id' => $this->getId(), |
102
|
|
|
'message' => $this->getBody(), |
103
|
|
|
'from' => $this->getUser(), |
104
|
|
|
'in' => $this->getChannel(), |
105
|
|
|
'at' => $this->at(), |
106
|
|
|
'mentions' => iterator_to_array($this->getMentions()), |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getId(): string |
114
|
|
|
{ |
115
|
|
|
return $this->id; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getBody(): string |
122
|
|
|
{ |
123
|
|
|
return $this->body; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return UserInterface |
128
|
|
|
*/ |
129
|
|
|
public function getUser(): UserInterface |
130
|
|
|
{ |
131
|
|
|
return $this->author; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return ChannelInterface |
136
|
|
|
*/ |
137
|
|
|
public function getChannel(): ChannelInterface |
138
|
|
|
{ |
139
|
|
|
return $this->channel; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return \Traversable |
144
|
|
|
*/ |
145
|
|
|
public function getMentions(): \Traversable |
146
|
|
|
{ |
147
|
|
|
return new \ArrayIterator($this->mentions); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.