natec1991

TransformStatus

Jun 23rd, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.15 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Responses\Timeline\Segments;
  4.  
  5. use App\Entities\Timeline\Timeline;
  6. use Illuminate\Http\Resources\Json\JsonResource;
  7.  
  8. class TransformStatus extends JsonResource
  9. {
  10.     /**
  11.      * @var \App\Entities\Timeline\Timeline
  12.      */
  13.     protected $timeline;
  14.  
  15.     /**
  16.      * @var array
  17.      */
  18.     protected $segments = [];
  19.  
  20.     /**
  21.      * TimelineInformationResponse constructor.
  22.      * @param \App\Entities\Timeline\Timeline $timeline
  23.      */
  24.     public function __construct(Timeline $timeline)
  25.     {
  26.         $this->timeline = $timeline;
  27.         $this->segments = [
  28.             'owner' => new TransformOwner(
  29.                 $this
  30.                     ->timeline
  31.                     ->find($this->timeline->id)
  32.             ),
  33.         ];
  34.     }
  35.  
  36.     /**
  37.      * @return array
  38.      */
  39.     public function __invoke()
  40.     {
  41.         return array_merge(
  42.             $this->status($this->timeline)['status'],
  43.             $this->retweet()
  44.         );
  45.     }
  46.  
  47.     /**
  48.      * Create an array of the status information
  49.      * @return array
  50.      */
  51.     public function status($status)
  52.     {
  53.         return [
  54.             'status' => [
  55.                 'id'         => $status->id,
  56.                 'post'       => $status->post,
  57.                 'likes'      => $status->likers()->count(),
  58.                 'retweets'   => $this->timeline->where('timeline_id', $status->id)->count(),
  59.                 'created_at' => $status->created_at->diffForHumans(),
  60.                 'has'        => [
  61.                     'liked'     => Auth()->user()->hasLiked($this->timeline),
  62.                     'retweeted' => Auth()->user()->statuses()->where('timeline_id', $status->id)->count() > 0,
  63.                 ],
  64.             ],
  65.         ];
  66.     }
  67.  
  68.     /**
  69.      * @return array
  70.      */
  71.     public function retweet()
  72.     {
  73.         return [
  74.             'retweet' => $this->when(isset($this->timeline->timeline_id), function () {
  75.                 return array_merge(
  76.                     $this->status($this->timeline->find($this->timeline->timeline_id)),
  77.                     $this->segments['owner']()
  78.                 );
  79.             }),
  80.         ];
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment