CommentTransformer.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/10
  6. * Time: 10:56
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\PostComment;
  10. use Illuminate\Support\Carbon;
  11. use League\Fractal\TransformerAbstract;
  12. class CommentTransformer extends TransformerAbstract
  13. {
  14. public function transform(PostComment $postComment)
  15. {
  16. if($postComment['is_delete']){
  17. $content = '该评论已被删除';
  18. }elseif($postComment['reply_username']){
  19. $content = '回复 @'.subtext($postComment['reply_username'], 6).': '.$postComment['content'];
  20. }else{
  21. $content = $postComment['content'];
  22. }
  23. return [
  24. 'id' => $postComment['id'],
  25. 'post_id' => $postComment['post_id'],
  26. 'parent_id' => $postComment['parent_id'],
  27. 'uid' => $postComment['uid'],
  28. 'username' => $postComment['username'],
  29. 'avatar' => $postComment['avatar'],
  30. 'content' => $content,
  31. 'created_at' => Carbon::parse($postComment['created_at'])->toDateTimeString(),
  32. 'is_delete' => $postComment['is_delete'],
  33. ];
  34. }
  35. }