Просмотр файла app/Models/Comment.php

Размер файла: 2.77Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use App\Services\Str;
  8. use MotorORM\Builder;
  9. use MotorORM\Collection;
  10.  
  11. /**
  12. * Class Guestbook
  13. *
  14. * @property int $id
  15. * @property string $user_id
  16. * @property string $story_id
  17. * @property string $text
  18. * @property int $rating
  19. * @property int $created_at
  20. *
  21. * @property-read User $user
  22. * @property-read Story $story
  23. * @property-read Poll $poll
  24. * @property-read Collection<Poll> $polls
  25. */
  26. class Comment extends Model
  27. {
  28. /**
  29. * Table name
  30. */
  31. protected string $table = 'comments.csv';
  32.  
  33. /**
  34. * The attributes that should be cast.
  35. */
  36. protected array $casts = [
  37. 'rating' => 'int',
  38. ];
  39.  
  40. /**
  41. * Возвращает связь пользователя
  42. *
  43. * return Builder
  44. */
  45. public function user(): Builder
  46. {
  47. return $this->HasOne(User::class, 'id', 'user_id');
  48. }
  49.  
  50. /**
  51. * Возвращает связь статьи
  52. *
  53. * return Builder
  54. */
  55. public function story(): Builder
  56. {
  57. return $this->HasOne(Story::class, 'id', 'story_id');
  58. }
  59.  
  60. /**
  61. * Возвращает связь голосования пользователя
  62. *
  63. * @return Builder
  64. */
  65. public function poll(): Builder
  66. {
  67. return $this->hasOne(Poll::class, 'entity_id')
  68. ->where('user_id', getUser('id'))
  69. ->where('entity_name', 'comment');
  70. }
  71.  
  72. /**
  73. * Возвращает связь голосований
  74. *
  75. * @return Builder
  76. */
  77. public function polls(): Builder
  78. {
  79. return $this->hasMany(Poll::class, 'entity_id')
  80. ->where('entity_name', 'comment');
  81. }
  82.  
  83. /**
  84. * Возвращает сокращенный текст комментария
  85. *
  86. * @param int $words
  87. *
  88. * @return string
  89. */
  90. public function shortText(int $words = 30): string
  91. {
  92. if (Str::wordCount($this->text) > $words) {
  93. return bbCodeTruncate($this->text, $words);
  94. }
  95.  
  96. return bbCode($this->text);
  97. }
  98.  
  99. /**
  100. * Get format rating
  101. *
  102. * @return string Форматированное число
  103. */
  104. public function getRating(): string
  105. {
  106. if ($this->rating > 0) {
  107. $rating = '<span style="color:#00aa00">+' . $this->rating . '</span>';
  108. } elseif ($this->rating < 0) {
  109. $rating = '<span style="color:#ff0000">' . $this->rating . '</span>';
  110. } else {
  111. $rating = '<span>0</span>';
  112. }
  113.  
  114. return $rating;
  115. }
  116.  
  117. /**
  118. * Delete comment
  119. *
  120. * @return int
  121. */
  122. public function delete(): int
  123. {
  124. // delete polls
  125. foreach ($this->polls as $poll) {
  126. $poll->delete();
  127. }
  128.  
  129. return parent::delete();
  130. }
  131. }