Completed
Pull Request — master (#12)
by Andrew
08:25
created
src/Http/SuccessResponseBuilder.php 1 patch
Spacing   +47 added lines, -47 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,17 +128,17 @@  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
-        $this->resource = $resource->setTransformer($transformer)->setResourceKey($resourceKey);
141
+        $this->resource = $resource->setTransformer( $transformer )->setResourceKey( $resourceKey );
142 142
 
143 143
         return $this;
144 144
     }
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
      */
151 151
     public function toArray():array
152 152
     {
153
-        return $this->serialize($this->getResource());
153
+        return $this->serialize( $this->getResource() );
154 154
     }
155 155
 
156 156
     /**
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
      */
161 161
     public function getResource():ResourceInterface
162 162
     {
163
-        return $this->resource->setMeta($this->meta);
163
+        return $this->resource->setMeta( $this->meta );
164 164
     }
165 165
 
166 166
     /**
@@ -180,13 +180,13 @@  discard block
 block discarded – undo
180 180
      * @return \League\Fractal\Serializer\SerializerAbstract
181 181
      * @throws \Flugg\Responder\Exceptions\InvalidSerializerException
182 182
      */
183
-    protected function resolveSerializer($serializer):SerializerAbstract
183
+    protected function resolveSerializer( $serializer ):SerializerAbstract
184 184
     {
185
-        if (is_string($serializer)) {
185
+        if ( is_string( $serializer ) ) {
186 186
             $serializer = new $serializer;
187 187
         }
188 188
 
189
-        if (! $serializer instanceof SerializerAbstract) {
189
+        if ( ! $serializer instanceof SerializerAbstract ) {
190 190
             throw new InvalidSerializerException();
191 191
         }
192 192
 
@@ -200,15 +200,15 @@  discard block
 block discarded – undo
200 200
      * @return \Illuminate\Database\Eloquent\Model
201 201
      * @throws \InvalidArgumentException
202 202
      */
203
-    protected function resolveModel($data):Model
203
+    protected function resolveModel( $data ):Model
204 204
     {
205
-        if ($data instanceof Model) {
205
+        if ( $data instanceof Model ) {
206 206
             return $data;
207 207
         }
208 208
 
209
-        $model = array_values($data)[0];
210
-        if (! $model instanceof Model) {
211
-            throw new InvalidArgumentException('You can only transform data containing Eloquent models.');
209
+        $model = array_values( $data )[ 0 ];
210
+        if ( ! $model instanceof Model ) {
211
+            throw new InvalidArgumentException( 'You can only transform data containing Eloquent models.' );
212 212
         }
213 213
 
214 214
         return $model;
@@ -221,15 +221,15 @@  discard block
 block discarded – undo
221 221
      * @param  \Flugg\Responder\Transformer|callable|null $transformer
222 222
      * @return \Flugg\Responder\Transformer|callable
223 223
      */
224
-    protected function resolveTransformer(Model $model, $transformer = null)
224
+    protected function resolveTransformer( Model $model, $transformer = null )
225 225
     {
226
-        $transformer = $transformer ?: $this->resolveTransformerFromModel($model);
226
+        $transformer = $transformer ?: $this->resolveTransformerFromModel( $model );
227 227
 
228
-        if (is_string($transformer)) {
228
+        if ( is_string( $transformer ) ) {
229 229
             $transformer = new $transformer;
230 230
         }
231 231
 
232
-        return $this->parseTransformer($transformer, $model);
232
+        return $this->parseTransformer( $transformer, $model );
233 233
     }
234 234
 
235 235
     /**
@@ -239,9 +239,9 @@  discard block
 block discarded – undo
239 239
      * @param  \Illuminate\Database\ELoquent\Model $model
240 240
      * @return \Flugg\Responder\Transformer|callable
241 241
      */
242
-    protected function resolveTransformerFromModel(Model $model)
242
+    protected function resolveTransformerFromModel( Model $model )
243 243
     {
244
-        if (! $model instanceof Transformable) {
244
+        if ( ! $model instanceof Transformable ) {
245 245
             return function () use ($model) {
246 246
                 return $model->toArray();
247 247
             };
@@ -258,14 +258,14 @@  discard block
 block discarded – undo
258 258
      * @return \Flugg\Responder\Transformer|callable
259 259
      * @throws \InvalidTransformerException
260 260
      */
261
-    protected function parseTransformer($transformer, Model $model)
261
+    protected function parseTransformer( $transformer, Model $model )
262 262
     {
263
-        if ($transformer instanceof Transformer) {
264
-            $transformer = $transformer->setRelations($this->resolveRelations($model));
265
-            $this->manager->parseIncludes($transformer->getRelations());
263
+        if ( $transformer instanceof Transformer ) {
264
+            $transformer = $transformer->setRelations( $this->resolveRelations( $model ) );
265
+            $this->manager->parseIncludes( $transformer->getRelations() );
266 266
 
267
-        } elseif (! is_callable($transformer)) {
268
-            throw new InvalidTransformerException($model);
267
+        } elseif ( ! is_callable( $transformer ) ) {
268
+            throw new InvalidTransformerException( $model );
269 269
         }
270 270
 
271 271
         return $transformer;
@@ -277,9 +277,9 @@  discard block
 block discarded – undo
277 277
      * @param  \Illuminate\Database\Eloquent\Model $model
278 278
      * @return array
279 279
      */
280
-    protected function resolveRelations(Model $model):array
280
+    protected function resolveRelations( Model $model ):array
281 281
     {
282
-        return array_keys($model->getRelations());
282
+        return array_keys( $model->getRelations() );
283 283
     }
284 284
 
285 285
     /**
@@ -289,13 +289,13 @@  discard block
 block discarded – undo
289 289
      * @param  string|null                         $resourceKey
290 290
      * @return string
291 291
      */
292
-    protected function resolveResourceKey(Model $model, string $resourceKey = null):string
292
+    protected function resolveResourceKey( Model $model, string $resourceKey = null ):string
293 293
     {
294
-        if (! is_null($resourceKey)) {
294
+        if ( ! is_null( $resourceKey ) ) {
295 295
             return $resourceKey;
296 296
         }
297 297
 
298
-        if (method_exists($model, 'getResourceKey')) {
298
+        if ( method_exists( $model, 'getResourceKey' ) ) {
299 299
             return $model->getResourceKey();
300 300
         }
301 301
 
@@ -308,8 +308,8 @@  discard block
 block discarded – undo
308 308
      * @param  ResourceInterface $resource
309 309
      * @return array
310 310
      */
311
-    protected function serialize(ResourceInterface $resource):array
311
+    protected function serialize( ResourceInterface $resource ):array
312 312
     {
313
-        return $this->manager->createData($resource)->toArray();
313
+        return $this->manager->createData( $resource )->toArray();
314 314
     }
315 315
 }
Please login to merge, or discard this patch.