LogTransformer.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. ];
  22. $data = json_decode($postLog['content']);
  23. $content = '';
  24. foreach($data as $key => $val){
  25. if(!$val) continue;
  26. $content .= $type[$key] . $val .'、';
  27. }
  28. $content = rtrim($content, '、');
  29. return [
  30. 'id' => $postLog['id'],
  31. 'username' => $postLog['username'],
  32. 'log_type' => $postLog['log_type'],
  33. 'created_at' => Carbon::parse($postLog['created_at'])->toDateTimeString(),
  34. 'content' => $content,
  35. ];
  36. }
  37. }