PostListTransformer.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/6
  6. * Time: 14:08
  7. */
  8. namespace App\Transformers\Circle;
  9. use App\Models\Post;
  10. use App\Traits\PostTrait;
  11. use Illuminate\Support\Carbon;
  12. use Illuminate\Support\Facades\Redis;
  13. use League\Fractal\TransformerAbstract;
  14. class PostListTransformer extends TransformerAbstract
  15. {
  16. use PostTrait;
  17. public function __construct($circleId)
  18. {
  19. $this->circle_id = $circleId;
  20. }
  21. public function transform(Post $post)
  22. {
  23. $key = 'circle_articles_' . $this->circle_id;
  24. $isexists = Redis::zscore($key, $post['id']);
  25. return [
  26. 'id' => $post['id'],
  27. 'created_at' => Carbon::parse($post['created_at'])->toDateTimeString(),
  28. 'uid' => $post['uid'],
  29. 'username' => $post['username'],
  30. 'topic' => $this->getTopic($post['topic_ids'], 1),
  31. 'content' => subtext(strip_tags($post['content']), 20),
  32. 'image' => $post['img'],
  33. 'is_exists_circle' => $isexists ? 1 : 0,
  34. 'circle_id' => $this->circle_id,
  35. ];
  36. }
  37. }