ArticleMapResource.php 1.7 KB

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