ArticleMapResource.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Http\Api\MdRender;
  4. use App\Http\Api\UserApi;
  5. use App\Models\Collection;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Http\Resources\Json\JsonResource;
  9. class ArticleMapResource extends JsonResource
  10. {
  11. /**
  12. * Transform the resource into an array.
  13. *
  14. * @param Request $request
  15. * @return array|Arrayable|\JsonSerializable
  16. */
  17. public function toArray($request)
  18. {
  19. $data = [
  20. 'id' => $this->id,
  21. 'collect_id' => $this->collect_id,
  22. 'article_id' => $this->article_id,
  23. 'level' => $this->level,
  24. 'title' => $this->title,
  25. 'editor' => UserApi::getById($this->editor_id),
  26. 'children' => $this->children,
  27. 'status' => $this->status,
  28. 'deleted_at' => $this->deleted_at,
  29. 'created_at' => $this->created_at,
  30. 'updated_at' => $this->updated_at,
  31. ];
  32. $channels = [];
  33. if ($request->has('channel')) {
  34. $channels = explode('_', $request->input('channel'));
  35. } else {
  36. $defaultChannel = Collection::where('uid', $this->collect_id)->value('default_channel');
  37. if ($defaultChannel) {
  38. $channels[] = $defaultChannel;
  39. }
  40. }
  41. $mdRender = new MdRender(['format' => 'simple']);
  42. $data['title_text'] = $mdRender->convert($this->title, $channels);
  43. if ($request->input('view') === 'article') {
  44. $collection = Collection::where('uid', $this->collect_id)->first();
  45. if ($collection) {
  46. $data['collection']['id'] = $collection->uid;
  47. $data['collection']['title'] = $collection->title;
  48. }
  49. }
  50. return $data;
  51. }
  52. }