CourseTransformer.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Repositories\Transformers\Course;
  3. use App\Repositories\Enums\CourseCommentStatusEnum;
  4. use App\Repositories\Enums\CourseRecEnum;
  5. use App\Repositories\Enums\CourseTypeEnum;
  6. use App\Repositories\Enums\ModelStatusEnum;
  7. use Carbon\Carbon;
  8. use League\Fractal\TransformerAbstract;
  9. use App\Repositories\Models\Course\Course;
  10. /**
  11. * Class CourseTransformer.
  12. *
  13. * @package namespace App\Repositories\Transformers;
  14. */
  15. class CourseTransformer extends TransformerAbstract
  16. {
  17. /**
  18. * Transform the Course entity.
  19. *
  20. * @param \App\Repositories\Models\Course\Course $model
  21. *
  22. * @return array
  23. */
  24. public function transform(Course $model)
  25. {
  26. return [
  27. 'id' => (int)$model->id,
  28. 'title' => $model->title,
  29. 'category_id' => $model->category_id,
  30. 'category_name' => optional($model->category)->name,
  31. 'type' => $model->type,
  32. 'type_text' => CourseTypeEnum::getDescription($model->type),
  33. 'labels' => str2arr($model->labels),
  34. 'slug' => $model->slug,
  35. 'thumb' => $model->thumb,
  36. 'thumb_resource' => $model->thumb_resource,
  37. 'short_description' => $model->short_description,
  38. 'description' => $model->description,
  39. 'published_at' => $model->published_at,
  40. 'is_rec' => $model->is_rec,
  41. 'is_rec_text' => CourseRecEnum::getDescription($model->is_rec),
  42. 'user_count' => $model->user_count,
  43. 'comment_status' => $model->comment_status,
  44. 'comment_status_text' => CourseCommentStatusEnum::getDescription($model->comment_status),
  45. 'sort' => $model->sort,
  46. 'status' => $model->status,
  47. 'watch_progress' => $model->subscribe ? $model->subscribe->progress : false,
  48. 'status_test' => ModelStatusEnum::getDescription($model->status),
  49. 'created_at' => $model->created_at->format(Carbon::DEFAULT_TO_STRING_FORMAT),
  50. 'updated_at' => $model->updated_at->format(Carbon::DEFAULT_TO_STRING_FORMAT)
  51. ];
  52. }
  53. }