VideoTransformer.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/18
  6. * Time: 10:08
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\Post;
  10. use App\Models\PostCollect;
  11. use App\Models\PostLike;
  12. use App\Traits\UserTrait;
  13. use League\Fractal\TransformerAbstract;
  14. class VideoTransformer extends TransformerAbstract
  15. {
  16. use UserTrait;
  17. public function __construct($uid)
  18. {
  19. $this->uid = $uid;
  20. }
  21. public function transform(Post $post)
  22. {
  23. $isFollow = 0;
  24. $followStatus = $this->getFollowStatus($this->uid, $post['uid']);
  25. if($followStatus){
  26. $isFollow = $followStatus;
  27. }
  28. $topic = [];
  29. foreach($post->topic() as $key => $val){
  30. $topic[] = [
  31. 'id' => $key,
  32. 'name' => $val
  33. ];
  34. }
  35. return [
  36. 'id' => $post['id'],
  37. 'type' => $post['type'],
  38. 'uid' => $post['uid'],
  39. 'username' => $post['username'],
  40. 'avatar' => $post['avatar'],
  41. 'title' => $post['title'],
  42. 'content' => $post['content'],
  43. 'img' => $post['img'],
  44. 'video' => $post['video'],
  45. 'topic' => $topic,
  46. 'praise_count' => $post->data->praise_count,
  47. 'collect_count' => $post->data->collect_count,
  48. 'comment_count' => $post->data->comment_count,
  49. 'is_like' => PostLike::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  50. 'is_collect' => PostCollect::where('post_id', $post['id'])->where('uid', $this->uid)->exists()?1:0,
  51. 'is_follow' => $isFollow,
  52. ];
  53. }
  54. }