Completed
Push — master ( bae989...d1e271 )
by Alexander
06:14
created
src/Http/SuccessResponseBuilder.php 1 patch
Spacing   +53 added lines, -53 removed lines patch added patch discarded remove patch
@@ -39,7 +39,7 @@  discard block
 block discarded – undo
39 39
      *
40 40
      * @var array
41 41
      */
42
-    protected $meta = [];
42
+    protected $meta = [ ];
43 43
 
44 44
     /**
45 45
      * The Fractal resource instance containing the data and transformer.
@@ -69,13 +69,13 @@  discard block
 block discarded – undo
69 69
      * @param \Flugg\Responder\ResourceFactory              $resourceFactory
70 70
      * @param \League\Fractal\Manager                       $manager
71 71
      */
72
-    public function __construct(ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager)
72
+    public function __construct( ResponseFactory $responseFactory, ResourceFactory $resourceFactory, Manager $manager )
73 73
     {
74 74
         $this->resourceFactory = $resourceFactory;
75 75
         $this->manager = $manager;
76 76
         $this->resource = $this->resourceFactory->make();
77 77
 
78
-        parent::__construct($responseFactory);
78
+        parent::__construct( $responseFactory );
79 79
     }
80 80
 
81 81
     /**
@@ -84,9 +84,9 @@  discard block
 block discarded – undo
84 84
      * @param  array $data
85 85
      * @return self
86 86
      */
87
-    public function addMeta(array $data):SuccessResponseBuilder
87
+    public function addMeta( array $data ):SuccessResponseBuilder
88 88
     {
89
-        $this->meta = array_merge($this->meta, $data);
89
+        $this->meta = array_merge( $this->meta, $data );
90 90
 
91 91
         return $this;
92 92
     }
@@ -97,9 +97,9 @@  discard block
 block discarded – undo
97 97
      * @param  \League\Fractal\Serializer\SerializerAbstract|string $serializer
98 98
      * @return self
99 99
      */
100
-    public function serializer($serializer):SuccessResponseBuilder
100
+    public function serializer( $serializer ):SuccessResponseBuilder
101 101
     {
102
-        $this->manager->setSerializer($this->resolveSerializer($serializer));
102
+        $this->manager->setSerializer( $this->resolveSerializer( $serializer ) );
103 103
 
104 104
         return $this;
105 105
     }
@@ -111,13 +111,13 @@  discard block
 block discarded – undo
111 111
      * @return self
112 112
      * @throws \InvalidArgumentException
113 113
      */
114
-    public function setStatus(int $statusCode):ResponseBuilder
114
+    public function setStatus( int $statusCode ):ResponseBuilder
115 115
     {
116
-        if ($statusCode < 100 || $statusCode >= 400) {
117
-            throw new InvalidArgumentException("{$statusCode} is not a valid success HTTP status code.");
116
+        if ( $statusCode < 100 || $statusCode >= 400 ) {
117
+            throw new InvalidArgumentException( "{$statusCode} is not a valid success HTTP status code." );
118 118
         }
119 119
 
120
-        return parent::setStatus($statusCode);
120
+        return parent::setStatus( $statusCode );
121 121
     }
122 122
 
123 123
     /**
@@ -129,17 +129,17 @@  discard block
 block discarded – undo
129 129
      * @param  string|null          $resourceKey
130 130
      * @return self
131 131
      */
132
-    public function transform($data = null, $transformer = null, string $resourceKey = null):SuccessResponseBuilder
132
+    public function transform( $data = null, $transformer = null, string $resourceKey = null ):SuccessResponseBuilder
133 133
     {
134
-        $resource = $this->resourceFactory->make($data);
134
+        $resource = $this->resourceFactory->make( $data );
135 135
 
136
-        if (! is_null($resource->getData())) {
137
-            $model = $this->resolveModel($resource->getData());
138
-            $transformer = $this->resolveTransformer($model, $transformer);
139
-            $resourceKey = $this->resolveResourceKey($model, $resourceKey);
136
+        if ( ! is_null( $resource->getData() ) ) {
137
+            $model = $this->resolveModel( $resource->getData() );
138
+            $transformer = $this->resolveTransformer( $model, $transformer );
139
+            $resourceKey = $this->resolveResourceKey( $model, $resourceKey );
140 140
         }
141 141
 
142
-        $this->resource = $resource->setTransformer($transformer)->setResourceKey($resourceKey);
142
+        $this->resource = $resource->setTransformer( $transformer )->setResourceKey( $resourceKey );
143 143
 
144 144
         return $this;
145 145
     }
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
      */
152 152
     public function toArray():array
153 153
     {
154
-        return $this->serialize($this->getResource());
154
+        return $this->serialize( $this->getResource() );
155 155
     }
156 156
 
157 157
     /**
@@ -161,7 +161,7 @@  discard block
 block discarded – undo
161 161
      */
162 162
     public function getResource():ResourceInterface
163 163
     {
164
-        return $this->resource->setMeta($this->meta);
164
+        return $this->resource->setMeta( $this->meta );
165 165
     }
166 166
 
167 167
     /**
@@ -181,13 +181,13 @@  discard block
 block discarded – undo
181 181
      * @return \League\Fractal\Serializer\SerializerAbstract
182 182
      * @throws \Flugg\Responder\Exceptions\InvalidSerializerException
183 183
      */
184
-    protected function resolveSerializer($serializer):SerializerAbstract
184
+    protected function resolveSerializer( $serializer ):SerializerAbstract
185 185
     {
186
-        if (is_string($serializer)) {
186
+        if ( is_string( $serializer ) ) {
187 187
             $serializer = new $serializer;
188 188
         }
189 189
 
190
-        if (! $serializer instanceof SerializerAbstract) {
190
+        if ( ! $serializer instanceof SerializerAbstract ) {
191 191
             throw new InvalidSerializerException();
192 192
         }
193 193
 
@@ -201,15 +201,15 @@  discard block
 block discarded – undo
201 201
      * @return \Illuminate\Database\Eloquent\Model
202 202
      * @throws \InvalidArgumentException
203 203
      */
204
-    protected function resolveModel($data):Model
204
+    protected function resolveModel( $data ):Model
205 205
     {
206
-        if ($data instanceof Model) {
206
+        if ( $data instanceof Model ) {
207 207
             return $data;
208 208
         }
209 209
 
210
-        $model = array_values($data)[0];
211
-        if (! $model instanceof Model) {
212
-            throw new InvalidArgumentException('You can only transform data containing Eloquent models.');
210
+        $model = array_values( $data )[ 0 ];
211
+        if ( ! $model instanceof Model ) {
212
+            throw new InvalidArgumentException( 'You can only transform data containing Eloquent models.' );
213 213
         }
214 214
 
215 215
         return $model;
@@ -222,15 +222,15 @@  discard block
 block discarded – undo
222 222
      * @param  \Flugg\Responder\Transformer|callable|null $transformer
223 223
      * @return \Flugg\Responder\Transformer|callable
224 224
      */
225
-    protected function resolveTransformer(Model $model, $transformer = null)
225
+    protected function resolveTransformer( Model $model, $transformer = null )
226 226
     {
227
-        $transformer = $transformer ?: $this->resolveTransformerFromModel($model);
227
+        $transformer = $transformer ?: $this->resolveTransformerFromModel( $model );
228 228
 
229
-        if (is_string($transformer)) {
229
+        if ( is_string( $transformer ) ) {
230 230
             $transformer = new $transformer;
231 231
         }
232 232
 
233
-        return $this->parseTransformer($transformer, $model);
233
+        return $this->parseTransformer( $transformer, $model );
234 234
     }
235 235
 
236 236
     /**
@@ -240,9 +240,9 @@  discard block
 block discarded – undo
240 240
      * @param  \Illuminate\Database\ELoquent\Model $model
241 241
      * @return \Flugg\Responder\Transformer|callable
242 242
      */
243
-    protected function resolveTransformerFromModel(Model $model)
243
+    protected function resolveTransformerFromModel( Model $model )
244 244
     {
245
-        if (! $model instanceof Transformable) {
245
+        if ( ! $model instanceof Transformable ) {
246 246
             return function () use ($model) {
247 247
                 return $model->toArray();
248 248
             };
@@ -259,14 +259,14 @@  discard block
 block discarded – undo
259 259
      * @return \Flugg\Responder\Transformer|callable
260 260
      * @throws \InvalidTransformerException
261 261
      */
262
-    protected function parseTransformer($transformer, Model $model)
262
+    protected function parseTransformer( $transformer, Model $model )
263 263
     {
264
-        if ($transformer instanceof Transformer) {
265
-            $transformer = $transformer->setRelations($this->resolveRelations($model));
266
-            $this->manager->parseIncludes(array_merge($transformer->getRelations(), $this->resolveNestedRelations($model)));
264
+        if ( $transformer instanceof Transformer ) {
265
+            $transformer = $transformer->setRelations( $this->resolveRelations( $model ) );
266
+            $this->manager->parseIncludes( array_merge( $transformer->getRelations(), $this->resolveNestedRelations( $model ) ) );
267 267
 
268
-        } elseif (! is_callable($transformer)) {
269
-            throw new InvalidTransformerException($model);
268
+        } elseif ( ! is_callable( $transformer ) ) {
269
+            throw new InvalidTransformerException( $model );
270 270
         }
271 271
 
272 272
         return $transformer;
@@ -278,9 +278,9 @@  discard block
 block discarded – undo
278 278
      * @param  \Illuminate\Database\Eloquent\Model $model
279 279
      * @return array
280 280
      */
281
-    protected function resolveRelations(Model $model):array
281
+    protected function resolveRelations( Model $model ):array
282 282
     {
283
-        return array_keys($model->getRelations());
283
+        return array_keys( $model->getRelations() );
284 284
     }
285 285
 
286 286
     /**
@@ -289,17 +289,17 @@  discard block
 block discarded – undo
289 289
      * @param  \Illuminate\Database\Eloquent\Model $model
290 290
      * @return array
291 291
      */
292
-    protected function resolveNestedRelations(Model $model):array
292
+    protected function resolveNestedRelations( Model $model ):array
293 293
     {
294 294
         $relations = $model->getRelations();
295
-        $keys = array_keys($relations);
295
+        $keys = array_keys( $relations );
296 296
 
297
-        foreach ($relations as $key => $relation) {
297
+        foreach ( $relations as $key => $relation ) {
298 298
             $relation = $relation instanceof Collection ? $relation->first() : $relation;
299
-            if (! is_null($relation)) {
300
-                $keys = array_merge($keys, array_map(function ($nestedRelation) use ($key) {
299
+            if ( ! is_null( $relation ) ) {
300
+                $keys = array_merge( $keys, array_map( function ( $nestedRelation ) use ($key) {
301 301
                     return $key . '.' . $nestedRelation;
302
-                }, $this->resolveNestedRelations($relation)));
302
+                }, $this->resolveNestedRelations( $relation ) ) );
303 303
             }
304 304
         }
305 305
 
@@ -313,13 +313,13 @@  discard block
 block discarded – undo
313 313
      * @param  string|null                         $resourceKey
314 314
      * @return string
315 315
      */
316
-    protected function resolveResourceKey(Model $model, string $resourceKey = null):string
316
+    protected function resolveResourceKey( Model $model, string $resourceKey = null ):string
317 317
     {
318
-        if (! is_null($resourceKey)) {
318
+        if ( ! is_null( $resourceKey ) ) {
319 319
             return $resourceKey;
320 320
         }
321 321
 
322
-        if (method_exists($model, 'getResourceKey')) {
322
+        if ( method_exists( $model, 'getResourceKey' ) ) {
323 323
             return $model->getResourceKey();
324 324
         }
325 325
 
@@ -332,8 +332,8 @@  discard block
 block discarded – undo
332 332
      * @param  ResourceInterface $resource
333 333
      * @return array
334 334
      */
335
-    protected function serialize(ResourceInterface $resource):array
335
+    protected function serialize( ResourceInterface $resource ):array
336 336
     {
337
-        return $this->manager->createData($resource)->toArray();
337
+        return $this->manager->createData( $resource )->toArray();
338 338
     }
339 339
 }
Please login to merge, or discard this patch.