SettingTransformer.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Repositories\Transformers\CMS;
  3. use App\Repositories\Enums\CMS\SettingTypeEnum;
  4. use App\Repositories\Models\Base\Resource;
  5. use Carbon\Carbon;
  6. use League\Fractal\TransformerAbstract;
  7. use App\Repositories\Models\CMS\Setting;
  8. /**
  9. * Class SettingTransformer.
  10. *
  11. * @package namespace App\Repositories\Transformers\CMS;
  12. */
  13. class SettingTransformer extends TransformerAbstract
  14. {
  15. /**
  16. * Transform the Setting entity.
  17. *
  18. * @param \App\Repositories\Models\CMS\Setting $model
  19. *
  20. * @return array
  21. */
  22. public function transform(Setting $model)
  23. {
  24. switch ((int)$model->type) {
  25. case SettingTypeEnum::STARING:
  26. case SettingTypeEnum::TEXT:
  27. $body = $model->body;
  28. break;
  29. case SettingTypeEnum::IMG:
  30. $body = Resource::query()->where('id', $model->body)->select(['id', 'path', 'url'])->first();
  31. break;
  32. }
  33. return [
  34. 'id' => (int)$model->id,
  35. 'name' => $model->name,
  36. 'key' => $model->key,
  37. 'type' => $model->type,
  38. 'type_name' => SettingTypeEnum::getDescription($model->type),
  39. 'body' => $body,
  40. 'is_init' => $model->is_init,
  41. 'sort' => $model->sort,
  42. 'status' => $model->status,
  43. 'created_at' => $model->created_at->format(Carbon::DEFAULT_TO_STRING_FORMAT),
  44. 'updated_at' => $model->updated_at->format(Carbon::DEFAULT_TO_STRING_FORMAT)
  45. ];
  46. }
  47. }