Completed
Push — master ( e4b3cd...9fa93a )
by Alexander
03:38
created
src/Http/SuccessResponseBuilder.php 1 patch
Spacing   +59 added lines, -59 removed lines patch added patch discarded remove patch
@@ -38,7 +38,7 @@  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 Fractal resource instance containing the data and transformer.
@@ -68,13 +68,13 @@  discard block
 block discarded – undo
68 68
      * @param \Flugg\Responder\ResourceFactory              $resourceFactory
69 69
      * @param \League\Fractal\Manager                       $manager
70 70
      */
71
-    public function __construct(ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager)
71
+    public function __construct( ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager )
72 72
     {
73 73
         $this->resourceFactory = $resourceFactory;
74 74
         $this->manager = $manager;
75 75
         $this->resource = $this->resourceFactory->make();
76 76
 
77
-        parent::__construct($responseFactory);
77
+        parent::__construct( $responseFactory );
78 78
     }
79 79
 
80 80
     /**
@@ -83,9 +83,9 @@  discard block
 block discarded – undo
83 83
      * @param  array $data
84 84
      * @return self
85 85
      */
86
-    public function addMeta(array $data):SuccessResponseBuilder
86
+    public function addMeta( array $data ):SuccessResponseBuilder
87 87
     {
88
-        $this->meta = array_merge($this->meta, $data);
88
+        $this->meta = array_merge( $this->meta, $data );
89 89
 
90 90
         return $this;
91 91
     }
@@ -96,9 +96,9 @@  discard block
 block discarded – undo
96 96
      * @param  \League\Fractal\Serializer\SerializerAbstract|string $serializer
97 97
      * @return self
98 98
      */
99
-    public function serializer($serializer):SuccessResponseBuilder
99
+    public function serializer( $serializer ):SuccessResponseBuilder
100 100
     {
101
-        $this->manager->setSerializer($this->resolveSerializer($serializer));
101
+        $this->manager->setSerializer( $this->resolveSerializer( $serializer ) );
102 102
 
103 103
         return $this;
104 104
     }
@@ -110,13 +110,13 @@  discard block
 block discarded – undo
110 110
      * @return self
111 111
      * @throws \InvalidArgumentException
112 112
      */
113
-    public function setStatus(int $statusCode):ResponseBuilder
113
+    public function setStatus( int $statusCode ):ResponseBuilder
114 114
     {
115
-        if ($statusCode < 100 || $statusCode >= 400) {
116
-            throw new InvalidArgumentException("{$statusCode} is not a valid success HTTP status code.");
115
+        if ( $statusCode < 100 || $statusCode >= 400 ) {
116
+            throw new InvalidArgumentException( "{$statusCode} is not a valid success HTTP status code." );
117 117
         }
118 118
 
119
-        return parent::setStatus($statusCode);
119
+        return parent::setStatus( $statusCode );
120 120
     }
121 121
 
122 122
     /**
@@ -128,21 +128,21 @@  discard block
 block discarded – undo
128 128
      * @param  string|null          $resourceKey
129 129
      * @return self
130 130
      */
131
-    public function transform($data = null, $transformer = null, string $resourceKey = null):SuccessResponseBuilder
131
+    public function transform( $data = null, $transformer = null, string $resourceKey = null ):SuccessResponseBuilder
132 132
     {
133
-        $resource = $this->resourceFactory->make($data);
133
+        $resource = $this->resourceFactory->make( $data );
134 134
 
135
-        if (! is_null($resource->getData())) {
136
-            $model = $this->resolveModel($resource->getData());
137
-            $transformer = $this->resolveTransformer($model, $transformer);
138
-            $resourceKey = $this->resolveResourceKey($model, $resourceKey);
135
+        if ( ! is_null( $resource->getData() ) ) {
136
+            $model = $this->resolveModel( $resource->getData() );
137
+            $transformer = $this->resolveTransformer( $model, $transformer );
138
+            $resourceKey = $this->resolveResourceKey( $model, $resourceKey );
139 139
         }
140 140
 
141
-        if ($transformer instanceof Transformer) {
142
-            $this->manager->parseIncludes(array_merge($transformer->getRelations(), $this->resolveNestedRelations($resource->getData())));
141
+        if ( $transformer instanceof Transformer ) {
142
+            $this->manager->parseIncludes( array_merge( $transformer->getRelations(), $this->resolveNestedRelations( $resource->getData() ) ) );
143 143
         }
144 144
 
145
-        $this->resource = $resource->setTransformer($transformer)->setResourceKey($resourceKey);
145
+        $this->resource = $resource->setTransformer( $transformer )->setResourceKey( $resourceKey );
146 146
 
147 147
         return $this;
148 148
     }
@@ -154,7 +154,7 @@  discard block
 block discarded – undo
154 154
      */
155 155
     public function toArray():array
156 156
     {
157
-        return $this->serialize($this->getResource());
157
+        return $this->serialize( $this->getResource() );
158 158
     }
159 159
 
160 160
     /**
@@ -164,7 +164,7 @@  discard block
 block discarded – undo
164 164
      */
165 165
     public function getResource():ResourceInterface
166 166
     {
167
-        return $this->resource->setMeta($this->meta);
167
+        return $this->resource->setMeta( $this->meta );
168 168
     }
169 169
 
170 170
     /**
@@ -184,13 +184,13 @@  discard block
 block discarded – undo
184 184
      * @return \League\Fractal\Serializer\SerializerAbstract
185 185
      * @throws \Flugg\Responder\Exceptions\InvalidSerializerException
186 186
      */
187
-    protected function resolveSerializer($serializer):SerializerAbstract
187
+    protected function resolveSerializer( $serializer ):SerializerAbstract
188 188
     {
189
-        if (is_string($serializer)) {
189
+        if ( is_string( $serializer ) ) {
190 190
             $serializer = new $serializer;
191 191
         }
192 192
 
193
-        if (! $serializer instanceof SerializerAbstract) {
193
+        if ( ! $serializer instanceof SerializerAbstract ) {
194 194
             throw new InvalidSerializerException();
195 195
         }
196 196
 
@@ -204,15 +204,15 @@  discard block
 block discarded – undo
204 204
      * @return \Illuminate\Database\Eloquent\Model
205 205
      * @throws \InvalidArgumentException
206 206
      */
207
-    protected function resolveModel($data):Model
207
+    protected function resolveModel( $data ):Model
208 208
     {
209
-        if ($data instanceof Model) {
209
+        if ( $data instanceof Model ) {
210 210
             return $data;
211 211
         }
212 212
 
213
-        $model = array_values($data)[0];
214
-        if (! $model instanceof Model) {
215
-            throw new InvalidArgumentException('You can only transform data containing Eloquent models.');
213
+        $model = array_values( $data )[ 0 ];
214
+        if ( ! $model instanceof Model ) {
215
+            throw new InvalidArgumentException( 'You can only transform data containing Eloquent models.' );
216 216
         }
217 217
 
218 218
         return $model;
@@ -225,15 +225,15 @@  discard block
 block discarded – undo
225 225
      * @param  \Flugg\Responder\Transformer|callable|null $transformer
226 226
      * @return \Flugg\Responder\Transformer|callable
227 227
      */
228
-    protected function resolveTransformer(Model $model, $transformer = null)
228
+    protected function resolveTransformer( Model $model, $transformer = null )
229 229
     {
230
-        $transformer = $transformer ?: $this->resolveTransformerFromModel($model);
230
+        $transformer = $transformer ?: $this->resolveTransformerFromModel( $model );
231 231
 
232
-        if (is_string($transformer)) {
232
+        if ( is_string( $transformer ) ) {
233 233
             $transformer = new $transformer;
234 234
         }
235 235
 
236
-        return $this->parseTransformer($transformer, $model);
236
+        return $this->parseTransformer( $transformer, $model );
237 237
     }
238 238
 
239 239
     /**
@@ -243,10 +243,10 @@  discard block
 block discarded – undo
243 243
      * @param  \Illuminate\Database\ELoquent\Model $model
244 244
      * @return \Flugg\Responder\Transformer|callable
245 245
      */
246
-    protected function resolveTransformerFromModel(Model $model)
246
+    protected function resolveTransformerFromModel( Model $model )
247 247
     {
248
-        if (! $model instanceof Transformable) {
249
-            return function ($model) {
248
+        if ( ! $model instanceof Transformable ) {
249
+            return function ( $model ) {
250 250
                 return $model->toArray();
251 251
             };
252 252
         }
@@ -262,13 +262,13 @@  discard block
 block discarded – undo
262 262
      * @return \Flugg\Responder\Transformer|callable
263 263
      * @throws \InvalidTransformerException
264 264
      */
265
-    protected function parseTransformer($transformer, Model $model)
265
+    protected function parseTransformer( $transformer, Model $model )
266 266
     {
267
-        if ($transformer instanceof Transformer) {
268
-            $transformer = $transformer->setRelations($this->resolveRelations($model));
267
+        if ( $transformer instanceof Transformer ) {
268
+            $transformer = $transformer->setRelations( $this->resolveRelations( $model ) );
269 269
 
270
-        } elseif (! is_callable($transformer)) {
271
-            throw new InvalidTransformerException($model);
270
+        } elseif ( ! is_callable( $transformer ) ) {
271
+            throw new InvalidTransformerException( $model );
272 272
         }
273 273
 
274 274
         return $transformer;
@@ -280,9 +280,9 @@  discard block
 block discarded – undo
280 280
      * @param  \Illuminate\Database\Eloquent\Model $model
281 281
      * @return array
282 282
      */
283
-    protected function resolveRelations(Model $model):array
283
+    protected function resolveRelations( Model $model ):array
284 284
     {
285
-        return array_keys($model->getRelations());
285
+        return array_keys( $model->getRelations() );
286 286
     }
287 287
 
288 288
     /**
@@ -291,24 +291,24 @@  discard block
 block discarded – undo
291 291
      * @param  \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $data
292 292
      * @return array
293 293
      */
294
-    protected function resolveNestedRelations($data):array
294
+    protected function resolveNestedRelations( $data ):array
295 295
     {
296
-        $keys = [];
296
+        $keys = [ ];
297 297
 
298
-        if ($data instanceof Model) {
299
-            $data = [$data];
300
-        } elseif (is_null($data)) {
298
+        if ( $data instanceof Model ) {
299
+            $data = [ $data ];
300
+        } elseif ( is_null( $data ) ) {
301 301
             return $keys;
302 302
         }
303 303
 
304
-        foreach ($data as $model) {
304
+        foreach ( $data as $model ) {
305 305
             $relations = $model->getRelations();
306
-            $keys = array_merge($keys, array_keys($relations));
306
+            $keys = array_merge( $keys, array_keys( $relations ) );
307 307
 
308
-            foreach ($relations as $key => $relation) {
309
-                $keys = array_merge($keys, array_map(function ($nestedRelation) use ($key) {
308
+            foreach ( $relations as $key => $relation ) {
309
+                $keys = array_merge( $keys, array_map( function ( $nestedRelation ) use ($key) {
310 310
                     return $key . '.' . $nestedRelation;
311
-                }, $this->resolveNestedRelations($relation)));
311
+                }, $this->resolveNestedRelations( $relation ) ) );
312 312
             }
313 313
         }
314 314
 
@@ -322,13 +322,13 @@  discard block
 block discarded – undo
322 322
      * @param  string|null                         $resourceKey
323 323
      * @return string
324 324
      */
325
-    protected function resolveResourceKey(Model $model, string $resourceKey = null):string
325
+    protected function resolveResourceKey( Model $model, string $resourceKey = null ):string
326 326
     {
327
-        if (! is_null($resourceKey)) {
327
+        if ( ! is_null( $resourceKey ) ) {
328 328
             return $resourceKey;
329 329
         }
330 330
 
331
-        if (method_exists($model, 'getResourceKey')) {
331
+        if ( method_exists( $model, 'getResourceKey' ) ) {
332 332
             return $model->getResourceKey();
333 333
         }
334 334
 
@@ -341,8 +341,8 @@  discard block
 block discarded – undo
341 341
      * @param  ResourceInterface $resource
342 342
      * @return array
343 343
      */
344
-    protected function serialize(ResourceInterface $resource):array
344
+    protected function serialize( ResourceInterface $resource ):array
345 345
     {
346
-        return $this->manager->createData($resource)->toArray();
346
+        return $this->manager->createData( $resource )->toArray();
347 347
     }
348 348
 }
Please login to merge, or discard this patch.