Advertisement
natec1991

StatusInfo

Jun 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.75 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Responses\Timeline;
  4.  
  5. use App\Entities\Timeline\Timeline;
  6. use App\Http\Responses\Timeline\Segments\TransformOwner;
  7. use App\Http\Responses\Timeline\Segments\TransformRoutes;
  8. use App\Http\Responses\Timeline\Segments\TransformStatus;
  9. use Illuminate\Contracts\Support\Responsable;
  10.  
  11. class TimelineInformationResponse implements Responsable
  12. {
  13.     /**
  14.      * @var \App\Entities\Timeline\Timeline
  15.      */
  16.     protected $timeline;
  17.  
  18.     /**
  19.      * @var array
  20.      */
  21.     protected $segments = [];
  22.  
  23.     /**
  24.      * TimelineInformationResponse constructor.
  25.      * @param \App\Entities\Timeline\Timeline $timeline
  26.      */
  27.     public function __construct(Timeline $timeline)
  28.     {
  29.         $this->timeline = $timeline;
  30.         $this->segments = [
  31.             'status' => new TransformStatus($this->timeline),
  32.             'routes' => new TransformRoutes($this->timeline),
  33.             'owner'  => new TransformOwner($this->timeline),
  34.         ];
  35.     }
  36.  
  37.     /**
  38.      * Return the created response
  39.      * @param \Illuminate\Http\Request $request
  40.      * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
  41.      */
  42.     public function toResponse($request)
  43.     {
  44.         return response()->json(json_decode(json_encode($this->mergeInfo())));
  45.     }
  46.  
  47.     /**
  48.      * Return the status information in an array
  49.      * @return array
  50.      */
  51.     public function toArray()
  52.     {
  53.         return $this->mergeInfo();
  54.     }
  55.  
  56.     /**
  57.      * Merge all the information together in to an object
  58.      * @return array
  59.      */
  60.     public function mergeInfo()
  61.     {
  62.         return array_merge(
  63.             $this->segments['status'](),
  64.             $this->segments['routes'](),
  65.             $this->segments['owner']()
  66.         );
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement