LogTransformer.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. 'suggest' => '推荐内容:',
  24. 'cancel_suggest' => '取消推荐内容:',
  25. ];
  26. $data = json_decode($postLog['content']);
  27. $content = '';
  28. foreach($data as $key => $val){
  29. if(!$val) continue;
  30. $content .= $type[$key] . $val .'、';
  31. }
  32. $content = rtrim($content, '、');
  33. return [
  34. 'id' => $postLog['id'],
  35. 'username' => $postLog['username'],
  36. 'log_type' => $postLog['log_type'],
  37. 'created_at' => Carbon::parse($postLog['created_at'])->toDateTimeString(),
  38. 'content' => $content,
  39. ];
  40. }
  41. }