tzurbaev /
osnova-api
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Osnova\Services\Timeline; |
||||||
| 4 | |||||||
| 5 | use GuzzleHttp\Exception\RequestException; |
||||||
| 6 | use Osnova\Services\Media\CoverImage; |
||||||
| 7 | use Osnova\Services\ServiceEntity; |
||||||
| 8 | use Osnova\Services\Subsites\Subsite; |
||||||
| 9 | use Osnova\Services\Timeline\Traits\HasLikes; |
||||||
| 10 | |||||||
| 11 | class Entry extends ServiceEntity |
||||||
| 12 | { |
||||||
| 13 | use HasLikes; |
||||||
| 14 | |||||||
| 15 | /** |
||||||
| 16 | * Get entry ID. |
||||||
| 17 | * |
||||||
| 18 | * @return int|null |
||||||
| 19 | */ |
||||||
| 20 | public function getId() |
||||||
| 21 | { |
||||||
| 22 | return $this->getData('id'); |
||||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||||
| 23 | } |
||||||
| 24 | |||||||
| 25 | /** |
||||||
| 26 | * Get entry author. |
||||||
| 27 | * |
||||||
| 28 | * @return Author|null |
||||||
| 29 | */ |
||||||
| 30 | public function getAuthor() |
||||||
| 31 | { |
||||||
| 32 | return $this->makeEntity(Author::class, 'author'); |
||||||
| 33 | } |
||||||
| 34 | |||||||
| 35 | /** |
||||||
| 36 | * Get entry subsite. |
||||||
| 37 | * |
||||||
| 38 | * @return Subsite|null |
||||||
| 39 | */ |
||||||
| 40 | public function getSubsite() |
||||||
| 41 | { |
||||||
| 42 | return $this->makeEntity(Subsite::class, 'subsite'); |
||||||
| 43 | } |
||||||
| 44 | |||||||
| 45 | /** |
||||||
| 46 | * Get entry title. |
||||||
| 47 | * |
||||||
| 48 | * @return string|null |
||||||
| 49 | */ |
||||||
| 50 | public function getTitle() |
||||||
| 51 | { |
||||||
| 52 | return $this->getData('title') ?? ''; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 53 | } |
||||||
| 54 | |||||||
| 55 | /** |
||||||
| 56 | * Get entry intro text. |
||||||
| 57 | * |
||||||
| 58 | * @return string|null |
||||||
| 59 | */ |
||||||
| 60 | public function getIntro() |
||||||
| 61 | { |
||||||
| 62 | return $this->getData('intro') ?? ''; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 63 | } |
||||||
| 64 | |||||||
| 65 | /** |
||||||
| 66 | * Get "introInFeed" attribute value. |
||||||
| 67 | * |
||||||
| 68 | * @return string|null |
||||||
| 69 | */ |
||||||
| 70 | public function getIntroInFeed() |
||||||
| 71 | { |
||||||
| 72 | return $this->getData('introInFeed') ?? ''; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 73 | } |
||||||
| 74 | |||||||
| 75 | /** |
||||||
| 76 | * Get entry's cover image. |
||||||
| 77 | * |
||||||
| 78 | * @return CoverImage|null |
||||||
| 79 | */ |
||||||
| 80 | public function getCover() |
||||||
| 81 | { |
||||||
| 82 | return $this->makeEntity(CoverImage::class, 'cover'); |
||||||
| 83 | } |
||||||
| 84 | |||||||
| 85 | /** |
||||||
| 86 | * Get entry content. |
||||||
| 87 | * |
||||||
| 88 | * @return EntryContent|null |
||||||
| 89 | */ |
||||||
| 90 | public function getContent() |
||||||
| 91 | { |
||||||
| 92 | return $this->makeEntity(EntryContent::class, 'entryContent'); |
||||||
| 93 | } |
||||||
| 94 | |||||||
| 95 | /** |
||||||
| 96 | * Get hits count. |
||||||
| 97 | * |
||||||
| 98 | * @return int |
||||||
| 99 | */ |
||||||
| 100 | public function getHitsCount() |
||||||
| 101 | { |
||||||
| 102 | return intval($this->getData('hitsCount')); |
||||||
| 103 | } |
||||||
| 104 | |||||||
| 105 | /** |
||||||
| 106 | * Get comments count. |
||||||
| 107 | * |
||||||
| 108 | * @return int |
||||||
| 109 | */ |
||||||
| 110 | public function getCommentsCount() |
||||||
| 111 | { |
||||||
| 112 | return intval($this->getData('commentsCount')); |
||||||
| 113 | } |
||||||
| 114 | |||||||
| 115 | /** |
||||||
| 116 | * Get favorites count. |
||||||
| 117 | * |
||||||
| 118 | * @return int |
||||||
| 119 | */ |
||||||
| 120 | public function getFavoritesCount() |
||||||
| 121 | { |
||||||
| 122 | return intval($this->getData('favoritesCount')); |
||||||
| 123 | } |
||||||
| 124 | |||||||
| 125 | /** |
||||||
| 126 | * Get badges list. |
||||||
| 127 | * |
||||||
| 128 | * @return array|mixed|string |
||||||
| 129 | */ |
||||||
| 130 | public function getBadges() |
||||||
| 131 | { |
||||||
| 132 | return $this->getData('badges', []); |
||||||
|
0 ignored issues
–
show
array() of type array is incompatible with the type null|string expected by parameter $default of Osnova\Services\ServiceEntity::getData().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 133 | } |
||||||
| 134 | |||||||
| 135 | /** |
||||||
| 136 | * Get publication date. |
||||||
| 137 | * |
||||||
| 138 | * @return \DateTimeImmutable |
||||||
| 139 | */ |
||||||
| 140 | public function getDate() |
||||||
| 141 | { |
||||||
| 142 | return new \DateTimeImmutable($this->getData('date', 0), 'Europe/Moscow'); |
||||||
|
0 ignored issues
–
show
'Europe/Moscow' of type string is incompatible with the type DateTimeZone expected by parameter $timezone of DateTimeImmutable::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$this->getData('date', 0) can also be of type array; however, parameter $time of DateTimeImmutable::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 143 | } |
||||||
| 144 | |||||||
| 145 | /** |
||||||
| 146 | * Get entry's last modification date. |
||||||
| 147 | * |
||||||
| 148 | * @return \DateTimeImmutable |
||||||
| 149 | */ |
||||||
| 150 | public function getLastModificationDate() |
||||||
| 151 | { |
||||||
| 152 | return new \DateTimeImmutable($this->getData('last_modification_date', 0), 'Europe/Moscow'); |
||||||
|
0 ignored issues
–
show
'Europe/Moscow' of type string is incompatible with the type DateTimeZone expected by parameter $timezone of DateTimeImmutable::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$this->getData('last_modification_date', 0) can also be of type array; however, parameter $time of DateTimeImmutable::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 153 | } |
||||||
| 154 | |||||||
| 155 | /** |
||||||
| 156 | * Determines whether the comments are enabled to current entry. |
||||||
| 157 | * |
||||||
| 158 | * @return bool |
||||||
| 159 | */ |
||||||
| 160 | public function commentsEnabled() |
||||||
| 161 | { |
||||||
| 162 | return $this->getData('isEnabledComments', true) === true; |
||||||
|
0 ignored issues
–
show
true of type true is incompatible with the type null|string expected by parameter $default of Osnova\Services\ServiceEntity::getData().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 163 | } |
||||||
| 164 | |||||||
| 165 | /** |
||||||
| 166 | * Determines whether the likes are enabled to current entry. |
||||||
| 167 | * |
||||||
| 168 | * @return bool |
||||||
| 169 | */ |
||||||
| 170 | public function likesEnabled() |
||||||
| 171 | { |
||||||
| 172 | return $this->getData('isEnabledLikes', true) === true; |
||||||
|
0 ignored issues
–
show
true of type true is incompatible with the type null|string expected by parameter $default of Osnova\Services\ServiceEntity::getData().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 173 | } |
||||||
| 174 | |||||||
| 175 | /** |
||||||
| 176 | * Determines whether the entry was favorited by the current user. |
||||||
| 177 | * |
||||||
| 178 | * @return bool |
||||||
| 179 | */ |
||||||
| 180 | public function isFavorited() |
||||||
| 181 | { |
||||||
| 182 | return $this->getData('isFavorited', false) === true; |
||||||
|
0 ignored issues
–
show
false of type false is incompatible with the type null|string expected by parameter $default of Osnova\Services\ServiceEntity::getData().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 183 | } |
||||||
| 184 | |||||||
| 185 | /** |
||||||
| 186 | * Determines whether the entry is pinned. |
||||||
| 187 | * |
||||||
| 188 | * @return bool |
||||||
| 189 | */ |
||||||
| 190 | public function isPinned() |
||||||
| 191 | { |
||||||
| 192 | return $this->getData('isPinned', false) === true; |
||||||
|
0 ignored issues
–
show
false of type false is incompatible with the type null|string expected by parameter $default of Osnova\Services\ServiceEntity::getData().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 193 | } |
||||||
| 194 | |||||||
| 195 | /** |
||||||
| 196 | * Determines whether the entry was published by resource staff. |
||||||
| 197 | * |
||||||
| 198 | * @return bool |
||||||
| 199 | */ |
||||||
| 200 | public function isEditorial() |
||||||
| 201 | { |
||||||
| 202 | return $this->getData('isEditorial', false) === true; |
||||||
|
0 ignored issues
–
show
false of type false is incompatible with the type null|string expected by parameter $default of Osnova\Services\ServiceEntity::getData().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 203 | } |
||||||
| 204 | |||||||
| 205 | /** |
||||||
| 206 | * Get commentators avatars list. |
||||||
| 207 | * |
||||||
| 208 | * @return array|string[] |
||||||
| 209 | */ |
||||||
| 210 | public function getCommentatorsAvatars() |
||||||
| 211 | { |
||||||
| 212 | return $this->getData('commentatorsAvatars', []); |
||||||
|
0 ignored issues
–
show
array() of type array is incompatible with the type null|string expected by parameter $default of Osnova\Services\ServiceEntity::getData().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 213 | } |
||||||
| 214 | |||||||
| 215 | /** |
||||||
| 216 | * Get entry URL. |
||||||
| 217 | * |
||||||
| 218 | * @return string |
||||||
| 219 | */ |
||||||
| 220 | public function getUrl() |
||||||
| 221 | { |
||||||
| 222 | if (!is_null($subsite = $this->getSubsite())) { |
||||||
| 223 | return $subsite->getUrl().'/'.$this->getId(); |
||||||
| 224 | } |
||||||
| 225 | |||||||
| 226 | return ''; |
||||||
| 227 | } |
||||||
| 228 | |||||||
| 229 | /** |
||||||
| 230 | * Get WebView URL. |
||||||
| 231 | * |
||||||
| 232 | * @return string |
||||||
| 233 | */ |
||||||
| 234 | public function getWebviewUrl() |
||||||
| 235 | { |
||||||
| 236 | return $this->getData('webviewUrl') ?? ''; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 237 | } |
||||||
| 238 | |||||||
| 239 | /** |
||||||
| 240 | * Get entry's audio version URL. |
||||||
| 241 | * |
||||||
| 242 | * @return string |
||||||
| 243 | */ |
||||||
| 244 | public function getAudioUrl() |
||||||
| 245 | { |
||||||
| 246 | return $this->getData('audioUrl') ?? ''; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 247 | } |
||||||
| 248 | |||||||
| 249 | /** |
||||||
| 250 | * Get the internal API URL. |
||||||
| 251 | * |
||||||
| 252 | * @param string $path = '' |
||||||
| 253 | * |
||||||
| 254 | * @return string |
||||||
| 255 | */ |
||||||
| 256 | protected function apiUrl(string $path = '') |
||||||
| 257 | { |
||||||
| 258 | return 'entries/'.$this->getId().($path ? '/'.ltrim($path) : ''); |
||||||
| 259 | } |
||||||
| 260 | |||||||
| 261 | /** |
||||||
| 262 | * Get list of popular entries for the current entry. |
||||||
| 263 | * |
||||||
| 264 | * @return array|Entry[] |
||||||
| 265 | */ |
||||||
| 266 | public function getPopularEntries() |
||||||
| 267 | { |
||||||
| 268 | try { |
||||||
| 269 | $response = $this->getApiProvider()->getClient()->request('GET', $this->apiUrl('popular')); |
||||||
| 270 | |||||||
| 271 | return $this->getEntitiesBuilder(static::class) |
||||||
| 272 | ->fromResponse($response) |
||||||
| 273 | ->with($this->apiProvider, $this->resource) |
||||||
| 274 | ->collection(); |
||||||
| 275 | } catch (RequestException $e) { |
||||||
| 276 | // |
||||||
| 277 | } |
||||||
| 278 | |||||||
| 279 | return []; |
||||||
| 280 | } |
||||||
| 281 | |||||||
| 282 | /** |
||||||
| 283 | * Get comments list for the current entry. |
||||||
| 284 | * |
||||||
| 285 | * @param string $sorting |
||||||
| 286 | * |
||||||
| 287 | * @return array|Comment[] |
||||||
| 288 | */ |
||||||
| 289 | public function getComments(string $sorting) |
||||||
| 290 | { |
||||||
| 291 | try { |
||||||
| 292 | $response = $this->getApiProvider()->getClient()->request('GET', $this->apiUrl('comments/'.$sorting)); |
||||||
| 293 | |||||||
| 294 | return $this->getEntitiesBuilder(Comment::class) |
||||||
| 295 | ->fromResponse($response) |
||||||
| 296 | ->with($this->apiProvider, $this->resource) |
||||||
| 297 | ->collection(); |
||||||
| 298 | } catch (RequestException $e) { |
||||||
| 299 | // |
||||||
| 300 | } |
||||||
| 301 | |||||||
| 302 | return []; |
||||||
| 303 | } |
||||||
| 304 | } |
||||||
| 305 |