Completed
Push — master ( f4d0ab...6ebcf0 )
by Alexander
04:07
created
src/Http/SuccessResponseBuilder.php 1 patch
Spacing   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -38,14 +38,14 @@  discard block
 block discarded – undo
38 38
      *
39 39
      * @var array
40 40
      */
41
-    protected $meta = [];
41
+    protected $meta = [ ];
42 42
 
43 43
     /**
44 44
      * The included relations.
45 45
      *
46 46
      * @var array
47 47
      */
48
-    protected $relations = [];
48
+    protected $relations = [ ];
49 49
 
50 50
     /**
51 51
      * The Fractal resource instance containing the data and transformer.
@@ -75,13 +75,13 @@  discard block
 block discarded – undo
75 75
      * @param \Flugg\Responder\ResourceFactory              $resourceFactory
76 76
      * @param \League\Fractal\Manager                       $manager
77 77
      */
78
-    public function __construct(ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager)
78
+    public function __construct( ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager )
79 79
     {
80 80
         $this->resourceFactory = $resourceFactory;
81 81
         $this->manager = $manager;
82 82
         $this->resource = $this->resourceFactory->make();
83 83
 
84
-        parent::__construct($responseFactory);
84
+        parent::__construct( $responseFactory );
85 85
     }
86 86
 
87 87
     /**
@@ -90,9 +90,9 @@  discard block
 block discarded – undo
90 90
      * @param  array $data
91 91
      * @return self
92 92
      */
93
-    public function addMeta(array $data):SuccessResponseBuilder
93
+    public function addMeta( array $data ):SuccessResponseBuilder
94 94
     {
95
-        $this->meta = array_merge($this->meta, $data);
95
+        $this->meta = array_merge( $this->meta, $data );
96 96
 
97 97
         return $this;
98 98
     }
@@ -105,7 +105,7 @@  discard block
 block discarded – undo
105 105
      */
106 106
     public function include($relations):SuccessResponseBuilder
107 107
     {
108
-        $this->relations = array_merge($this->relations, $relations);
108
+        $this->relations = array_merge( $this->relations, $relations );
109 109
 
110 110
         return $this;
111 111
     }
@@ -116,9 +116,9 @@  discard block
 block discarded – undo
116 116
      * @param  \League\Fractal\Serializer\SerializerAbstract|string $serializer
117 117
      * @return self
118 118
      */
119
-    public function serializer($serializer):SuccessResponseBuilder
119
+    public function serializer( $serializer ):SuccessResponseBuilder
120 120
     {
121
-        $this->manager->setSerializer($this->resolveSerializer($serializer));
121
+        $this->manager->setSerializer( $this->resolveSerializer( $serializer ) );
122 122
 
123 123
         return $this;
124 124
     }
@@ -130,13 +130,13 @@  discard block
 block discarded – undo
130 130
      * @return self
131 131
      * @throws \InvalidArgumentException
132 132
      */
133
-    public function setStatus(int $statusCode):ResponseBuilder
133
+    public function setStatus( int $statusCode ):ResponseBuilder
134 134
     {
135
-        if ($statusCode < 100 || $statusCode >= 400) {
136
-            throw new InvalidArgumentException("{$statusCode} is not a valid success HTTP status code.");
135
+        if ( $statusCode < 100 || $statusCode >= 400 ) {
136
+            throw new InvalidArgumentException( "{$statusCode} is not a valid success HTTP status code." );
137 137
         }
138 138
 
139
-        return parent::setStatus($statusCode);
139
+        return parent::setStatus( $statusCode );
140 140
     }
141 141
 
142 142
     /**
@@ -148,25 +148,25 @@  discard block
 block discarded – undo
148 148
      * @param  string|null          $resourceKey
149 149
      * @return self
150 150
      */
151
-    public function transform($data = null, $transformer = null, string $resourceKey = null):SuccessResponseBuilder
151
+    public function transform( $data = null, $transformer = null, string $resourceKey = null ):SuccessResponseBuilder
152 152
     {
153
-        $resource = $this->resourceFactory->make($data);
153
+        $resource = $this->resourceFactory->make( $data );
154 154
 
155
-        if (! is_null($resource->getData())) {
156
-            $model = $this->resolveModel($resource->getData());
157
-            $transformer = $this->resolveTransformer($model, $transformer);
158
-            $resourceKey = $this->resolveResourceKey($model, $resourceKey);
155
+        if ( ! is_null( $resource->getData() ) ) {
156
+            $model = $this->resolveModel( $resource->getData() );
157
+            $transformer = $this->resolveTransformer( $model, $transformer );
158
+            $resourceKey = $this->resolveResourceKey( $model, $resourceKey );
159 159
         }
160 160
 
161
-        if ($transformer instanceof Transformer) {
162
-            $this->include($relations = $this->resolveNestedRelations($resource->getData()));
161
+        if ( $transformer instanceof Transformer ) {
162
+            $this->include( $relations = $this->resolveNestedRelations( $resource->getData() ) );
163 163
 
164
-            if ($transformer->allRelationsAllowed()) {
165
-                $transformer->setRelations($relations);
164
+            if ( $transformer->allRelationsAllowed() ) {
165
+                $transformer->setRelations( $relations );
166 166
             }
167 167
         }
168 168
 
169
-        $this->resource = $resource->setTransformer($transformer)->setResourceKey($resourceKey);
169
+        $this->resource = $resource->setTransformer( $transformer )->setResourceKey( $resourceKey );
170 170
 
171 171
         return $this;
172 172
     }
@@ -178,7 +178,7 @@  discard block
 block discarded – undo
178 178
      */
179 179
     public function toArray():array
180 180
     {
181
-        return $this->serialize($this->getResource());
181
+        return $this->serialize( $this->getResource() );
182 182
     }
183 183
 
184 184
     /**
@@ -188,14 +188,14 @@  discard block
 block discarded – undo
188 188
      */
189 189
     public function getResource():ResourceInterface
190 190
     {
191
-        $this->manager->parseIncludes($this->relations);
191
+        $this->manager->parseIncludes( $this->relations );
192 192
         $transformer = $this->resource->getTransformer();
193 193
 
194
-        if ($transformer instanceof Transformer && $transformer->allRelationsAllowed()) {
195
-            $this->resource->setTransformer($transformer->setRelations($this->manager->getRequestedIncludes()));
194
+        if ( $transformer instanceof Transformer && $transformer->allRelationsAllowed() ) {
195
+            $this->resource->setTransformer( $transformer->setRelations( $this->manager->getRequestedIncludes() ) );
196 196
         }
197 197
 
198
-        return $this->resource->setMeta($this->meta);
198
+        return $this->resource->setMeta( $this->meta );
199 199
     }
200 200
 
201 201
     /**
@@ -215,13 +215,13 @@  discard block
 block discarded – undo
215 215
      * @return \League\Fractal\Serializer\SerializerAbstract
216 216
      * @throws \Flugg\Responder\Exceptions\InvalidSerializerException
217 217
      */
218
-    protected function resolveSerializer($serializer):SerializerAbstract
218
+    protected function resolveSerializer( $serializer ):SerializerAbstract
219 219
     {
220
-        if (is_string($serializer)) {
220
+        if ( is_string( $serializer ) ) {
221 221
             $serializer = new $serializer;
222 222
         }
223 223
 
224
-        if (! $serializer instanceof SerializerAbstract) {
224
+        if ( ! $serializer instanceof SerializerAbstract ) {
225 225
             throw new InvalidSerializerException();
226 226
         }
227 227
 
@@ -235,15 +235,15 @@  discard block
 block discarded – undo
235 235
      * @return \Illuminate\Database\Eloquent\Model
236 236
      * @throws \InvalidArgumentException
237 237
      */
238
-    protected function resolveModel($data):Model
238
+    protected function resolveModel( $data ):Model
239 239
     {
240
-        if ($data instanceof Model) {
240
+        if ( $data instanceof Model ) {
241 241
             return $data;
242 242
         }
243 243
 
244
-        $model = array_values($data)[0];
245
-        if (! $model instanceof Model) {
246
-            throw new InvalidArgumentException('You can only transform data containing Eloquent models.');
244
+        $model = array_values( $data )[ 0 ];
245
+        if ( ! $model instanceof Model ) {
246
+            throw new InvalidArgumentException( 'You can only transform data containing Eloquent models.' );
247 247
         }
248 248
 
249 249
         return $model;
@@ -256,15 +256,15 @@  discard block
 block discarded – undo
256 256
      * @param  \Flugg\Responder\Transformer|callable|null $transformer
257 257
      * @return \Flugg\Responder\Transformer|callable
258 258
      */
259
-    protected function resolveTransformer(Model $model, $transformer = null)
259
+    protected function resolveTransformer( Model $model, $transformer = null )
260 260
     {
261
-        $transformer = $transformer ?: $this->resolveTransformerFromModel($model);
261
+        $transformer = $transformer ?: $this->resolveTransformerFromModel( $model );
262 262
 
263
-        if (is_string($transformer)) {
263
+        if ( is_string( $transformer ) ) {
264 264
             $transformer = new $transformer;
265 265
         }
266 266
 
267
-        return $this->parseTransformer($transformer, $model);
267
+        return $this->parseTransformer( $transformer, $model );
268 268
     }
269 269
 
270 270
     /**
@@ -274,10 +274,10 @@  discard block
 block discarded – undo
274 274
      * @param  \Illuminate\Database\ELoquent\Model $model
275 275
      * @return \Flugg\Responder\Transformer|callable
276 276
      */
277
-    protected function resolveTransformerFromModel(Model $model)
277
+    protected function resolveTransformerFromModel( Model $model )
278 278
     {
279
-        if (! $model instanceof Transformable) {
280
-            return function ($model) {
279
+        if ( ! $model instanceof Transformable ) {
280
+            return function ( $model ) {
281 281
                 return $model->toArray();
282 282
             };
283 283
         }
@@ -293,14 +293,14 @@  discard block
 block discarded – undo
293 293
      * @return \Flugg\Responder\Transformer|callable
294 294
      * @throws \InvalidTransformerException
295 295
      */
296
-    protected function parseTransformer($transformer, Model $model)
296
+    protected function parseTransformer( $transformer, Model $model )
297 297
     {
298
-        if ($transformer instanceof Transformer) {
299
-            $relations = $transformer->allRelationsAllowed() ? $this->resolveRelations($model) : $transformer->getRelations();
300
-            $transformer = $transformer->setRelations($relations);
298
+        if ( $transformer instanceof Transformer ) {
299
+            $relations = $transformer->allRelationsAllowed() ? $this->resolveRelations( $model ) : $transformer->getRelations();
300
+            $transformer = $transformer->setRelations( $relations );
301 301
 
302
-        } elseif (! is_callable($transformer)) {
303
-            throw new InvalidTransformerException($model);
302
+        } elseif ( ! is_callable( $transformer ) ) {
303
+            throw new InvalidTransformerException( $model );
304 304
         }
305 305
 
306 306
         return $transformer;
@@ -312,9 +312,9 @@  discard block
 block discarded – undo
312 312
      * @param  \Illuminate\Database\Eloquent\Model $model
313 313
      * @return array
314 314
      */
315
-    protected function resolveRelations(Model $model):array
315
+    protected function resolveRelations( Model $model ):array
316 316
     {
317
-        return array_keys($model->getRelations());
317
+        return array_keys( $model->getRelations() );
318 318
     }
319 319
 
320 320
     /**
@@ -323,22 +323,22 @@  discard block
 block discarded – undo
323 323
      * @param  \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $data
324 324
      * @return array
325 325
      */
326
-    protected function resolveNestedRelations($data):array
326
+    protected function resolveNestedRelations( $data ):array
327 327
     {
328
-        if (is_null($data)) {
329
-            return [];
328
+        if ( is_null( $data ) ) {
329
+            return [ ];
330 330
         }
331 331
 
332
-        $data = $data instanceof Model ? [$data] : $data;
332
+        $data = $data instanceof Model ? [ $data ] : $data;
333 333
 
334
-        return collect($data)->flatMap(function ($model) {
335
-            $relations = collect($model->getRelations());
334
+        return collect( $data )->flatMap( function ( $model ) {
335
+            $relations = collect( $model->getRelations() );
336 336
 
337
-            return $relations->keys()->merge($relations->flatMap(function ($relation, $key) {
338
-                return collect($this->resolveNestedRelations($relation))->map(function ($nestedRelation) use ($key) {
337
+            return $relations->keys()->merge( $relations->flatMap( function ( $relation, $key ) {
338
+                return collect( $this->resolveNestedRelations( $relation ) )->map( function ( $nestedRelation ) use ($key) {
339 339
                     return $key . '.' . $nestedRelation;
340 340
                 });
341
-            }));
341
+            }) );
342 342
         })->toArray();
343 343
     }
344 344
 
@@ -349,13 +349,13 @@  discard block
 block discarded – undo
349 349
      * @param  string|null                         $resourceKey
350 350
      * @return string
351 351
      */
352
-    protected function resolveResourceKey(Model $model, string $resourceKey = null):string
352
+    protected function resolveResourceKey( Model $model, string $resourceKey = null ):string
353 353
     {
354
-        if (! is_null($resourceKey)) {
354
+        if ( ! is_null( $resourceKey ) ) {
355 355
             return $resourceKey;
356 356
         }
357 357
 
358
-        if (method_exists($model, 'getResourceKey')) {
358
+        if ( method_exists( $model, 'getResourceKey' ) ) {
359 359
             return $model->getResourceKey();
360 360
         }
361 361
 
@@ -368,8 +368,8 @@  discard block
 block discarded – undo
368 368
      * @param  ResourceInterface $resource
369 369
      * @return array
370 370
      */
371
-    protected function serialize(ResourceInterface $resource):array
371
+    protected function serialize( ResourceInterface $resource ):array
372 372
     {
373
-        return $this->manager->createData($resource)->toArray();
373
+        return $this->manager->createData( $resource )->toArray();
374 374
     }
375 375
 }
376 376
\ No newline at end of file
Please login to merge, or discard this patch.