|
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 string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getBody(): string |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->body; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return ChannelInterface |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getChannel(): ChannelInterface |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->channel; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return UserInterface |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getUser(): UserInterface |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->author; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return \Traversable |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getMentions(): \Traversable |
|
99
|
|
|
{ |
|
100
|
|
|
return new \ArrayIterator($this->mentions); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return \DateTimeInterface |
|
105
|
|
|
*/ |
|
106
|
|
|
public function at(): \DateTimeInterface |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->createdAt; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getId(): string |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->id; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
|
|
public function canBeUpdated(): bool |
|
123
|
|
|
{ |
|
124
|
|
|
switch (static::MESSAGE_EDIT_TIMEOUT) { |
|
125
|
|
|
case 0: return true; |
|
|
|
|
|
|
126
|
|
|
case -1; return false; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return Carbon::createFromTimestamp($this->at()->getTimestamp(), $this->at()->getTimezone()) |
|
130
|
|
|
->addSeconds(static::MESSAGE_EDIT_TIMEOUT) > Carbon::now($this->at()->getTimezone()); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.