Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Responses\Timeline\Segments;
- use App\Entities\Timeline\Timeline;
- use Illuminate\Http\Resources\Json\JsonResource;
- class TransformStatus extends JsonResource
- {
- /**
- * @var \App\Entities\Timeline\Timeline
- */
- protected $timeline;
- /**
- * @var array
- */
- protected $segments = [];
- /**
- * TimelineInformationResponse constructor.
- * @param \App\Entities\Timeline\Timeline $timeline
- */
- public function __construct(Timeline $timeline)
- {
- $this->timeline = $timeline;
- $this->segments = [
- 'owner' => new TransformOwner(
- $this
- ->timeline
- ->find($this->timeline->id)
- ),
- ];
- }
- /**
- * @return array
- */
- public function __invoke()
- {
- return array_merge(
- $this->status($this->timeline)['status'],
- $this->retweet()
- );
- }
- /**
- * Create an array of the status information
- * @return array
- */
- public function status($status)
- {
- return [
- 'status' => [
- 'id' => $status->id,
- 'post' => $status->post,
- 'likes' => $status->likers()->count(),
- 'retweets' => $this->timeline->where('timeline_id', $status->id)->count(),
- 'created_at' => $status->created_at->diffForHumans(),
- 'has' => [
- 'liked' => Auth()->user()->hasLiked($this->timeline),
- 'retweeted' => Auth()->user()->statuses()->where('timeline_id', $status->id)->count() > 0,
- ],
- ],
- ];
- }
- /**
- * @return array
- */
- public function retweet()
- {
- return [
- 'retweet' => $this->when(isset($this->timeline->timeline_id), function () {
- return array_merge(
- $this->status($this->timeline->find($this->timeline->timeline_id)),
- $this->segments['owner']()
- );
- }),
- ];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment