LogTransformer.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/10
  6. * Time: 17:21
  7. */
  8. namespace App\Transformers\Post;
  9. use App\Models\PostLog;
  10. use Illuminate\Support\Carbon;
  11. use League\Fractal\TransformerAbstract;
  12. class LogTransformer extends TransformerAbstract
  13. {
  14. public function transform(PostLog $postLog)
  15. {
  16. $type = [
  17. 'add_pv' => '增加浏览量:',
  18. 'add_praise_count' => '增加点赞数:',
  19. 'add_collect_count' => '增加收藏数:',
  20. 'add_share_count' => '增加分享数:',
  21. 'delete' => '删除内容:',
  22. 'update_topic' => '编辑内容话题:',
  23. 'change_topic' => '替换内容话题:',
  24. 'suggest' => '推荐内容:',
  25. 'cancel_suggest' => '取消推荐内容:',
  26. ];
  27. $data = json_decode($postLog['content']);
  28. $content = '';
  29. foreach($data as $key => $val){
  30. if(!$val) continue;
  31. $content .= $type[$key] . $val .'、';
  32. }
  33. $content = rtrim($content, '、');
  34. return [
  35. 'id' => $postLog['id'],
  36. 'username' => $postLog['username'],
  37. 'log_type' => $postLog['log_type'],
  38. 'created_at' => Carbon::parse($postLog['created_at'])->toDateTimeString(),
  39. 'content' => $content,
  40. ];
  41. }
  42. }