ReplyTransformer.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/15
  6. * Time: 17:08
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\PostComment;
  10. use App\Traits\PostTrait;
  11. use App\Traits\UserTrait;
  12. use Carbon\Carbon;
  13. use League\Fractal\TransformerAbstract;
  14. class ReplyTransformer extends TransformerAbstract
  15. {
  16. use UserTrait;
  17. use PostTrait;
  18. public function __construct($postId, $uid)
  19. {
  20. $this->postId = $postId;
  21. $this->uid = $uid;
  22. }
  23. public function transform(PostComment $postComment)
  24. {
  25. $user = $this->userInfo($postComment['uid']);
  26. $replyUsername = '';
  27. if($postComment['reply_uid']){
  28. $userReply = $this->userInfo($postComment['reply_uid']);
  29. $replyUsername = $userReply['username'];
  30. }
  31. $commentLike = $this->getCommentLike($this->postId, $postComment['id'], $this->uid);
  32. return [
  33. 'id' => $postComment['id'],
  34. 'uid' => $postComment['uid'],
  35. 'username' => $user['username'],
  36. 'reply_username' => $replyUsername,
  37. 'avatar' => $user['avatar'],
  38. 'content' => $postComment['is_delete']?'该回复已被删除':$postComment['content'],
  39. 'created_at' => Carbon::parse($postComment['created_at'])->diffForHumans(),
  40. 'is_delete' => $postComment['is_delete'],
  41. 'is_like' => $commentLike['is_like'],
  42. 'like_count' => $commentLike['like_count'],
  43. ];
  44. }
  45. }