1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Osnova\Services\Timeline; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
6
|
|
|
use Osnova\Enums\MediaType; |
7
|
|
|
use Osnova\Services\Media\Image; |
8
|
|
|
use Osnova\Services\Media\Video; |
9
|
|
|
use Osnova\Services\ServiceEntity; |
10
|
|
|
use Osnova\Services\Timeline\Traits\HasLikes; |
11
|
|
|
|
12
|
|
|
class Comment extends ServiceEntity |
13
|
|
|
{ |
14
|
|
|
use HasLikes; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get comment ID. |
18
|
|
|
* |
19
|
|
|
* @return int|null |
20
|
|
|
*/ |
21
|
|
|
public function getId() |
22
|
|
|
{ |
23
|
|
|
return $this->getData('id'); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get comment author. |
28
|
|
|
* |
29
|
|
|
* @return Author|null |
30
|
|
|
*/ |
31
|
|
|
public function getAuthor() |
32
|
|
|
{ |
33
|
|
|
return $this->makeEntity(Author::class, 'author'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get comment's publication date. |
38
|
|
|
* |
39
|
|
|
* @return \DateTimeImmutable |
40
|
|
|
*/ |
41
|
|
|
public function getDate() |
42
|
|
|
{ |
43
|
|
|
return new \DateTimeImmutable($this->getData('date'), 'Europe/Moscow'); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get comment text. |
48
|
|
|
* |
49
|
|
|
* @return string|null |
50
|
|
|
*/ |
51
|
|
|
public function getText() |
52
|
|
|
{ |
53
|
|
|
return $this->getData('text') ?? ''; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get comment media list. |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
public function getMedia() |
62
|
|
|
{ |
63
|
|
|
$media = $this->getData('media', []); |
|
|
|
|
64
|
|
|
|
65
|
|
|
if (!is_array($media) || empty($media)) { |
66
|
|
|
return []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$entities = array_map(function (array $item) { |
70
|
|
|
switch ($item['type'] ?? 0) { |
71
|
|
|
case MediaType::IMAGE: |
72
|
|
|
return $this->makeEntityFrom(Image::class, $item); |
73
|
|
|
case MediaType::VIDEO: |
74
|
|
|
return $this->makeEntityFrom(Video::class, $item); |
75
|
|
|
default: |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
}, $media); |
79
|
|
|
|
80
|
|
|
return array_filter($entities); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get comment source ID. |
85
|
|
|
* |
86
|
|
|
* @return int|null |
87
|
|
|
*/ |
88
|
|
|
public function getSourceId() |
89
|
|
|
{ |
90
|
|
|
return $this->getData('source_id'); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get comment's depth level. |
95
|
|
|
* |
96
|
|
|
* @return int|null |
97
|
|
|
*/ |
98
|
|
|
public function getLevel() |
99
|
|
|
{ |
100
|
|
|
return $this->getData('level'); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get comment's parent comment ID. |
105
|
|
|
* |
106
|
|
|
* @return int|null |
107
|
|
|
*/ |
108
|
|
|
public function getParentCommentId() |
109
|
|
|
{ |
110
|
|
|
return $this->getData('replyTo'); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Determines whether the comment is pinned. |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function isPinned() |
119
|
|
|
{ |
120
|
|
|
return $this->getData('isPinned') === true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Determines whether the comment was favorited by the current user. |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function isFavorited() |
129
|
|
|
{ |
130
|
|
|
return $this->getData('isFavorited') === true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Determnes whether the comment was edited. |
135
|
|
|
* |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
public function isEdited() |
139
|
|
|
{ |
140
|
|
|
return $this->getData('isEdited') === true; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get comment likers list. |
145
|
|
|
* |
146
|
|
|
* @return array|Liker[] |
147
|
|
|
*/ |
148
|
|
|
public function getLikers() |
149
|
|
|
{ |
150
|
|
|
try { |
151
|
|
|
$response = $this->getApiProvider()->getClient()->request('GET', 'comment/likers/'.$this->getId()); |
152
|
|
|
|
153
|
|
|
return $this->getEntitiesBuilder(Liker::class) |
154
|
|
|
->fromResponse($response) |
155
|
|
|
->collection(); |
156
|
|
|
} catch (RequestException $e) { |
157
|
|
|
// |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return []; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|