@@ -37,14 +37,14 @@ discard block |
||
| 37 | 37 | * |
| 38 | 38 | * @var array |
| 39 | 39 | */ |
| 40 | - protected $meta = []; |
|
| 40 | + protected $meta = [ ]; |
|
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | 43 | * The included relations. |
| 44 | 44 | * |
| 45 | 45 | * @var array |
| 46 | 46 | */ |
| 47 | - protected $relations = []; |
|
| 47 | + protected $relations = [ ]; |
|
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | 50 | * The Fractal resource instance containing the data and transformer. |
@@ -74,13 +74,13 @@ discard block |
||
| 74 | 74 | * @param \Flugg\Responder\ResourceFactory $resourceFactory |
| 75 | 75 | * @param \League\Fractal\Manager $manager |
| 76 | 76 | */ |
| 77 | - public function __construct($responseFactory, ResourceFactory $resourceFactory, Manager $manager) |
|
| 77 | + public function __construct( $responseFactory, ResourceFactory $resourceFactory, Manager $manager ) |
|
| 78 | 78 | { |
| 79 | 79 | $this->resourceFactory = $resourceFactory; |
| 80 | 80 | $this->manager = $manager; |
| 81 | 81 | $this->resource = $this->resourceFactory->make(); |
| 82 | 82 | |
| 83 | - parent::__construct($responseFactory); |
|
| 83 | + parent::__construct( $responseFactory ); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
@@ -89,9 +89,9 @@ discard block |
||
| 89 | 89 | * @param array $data |
| 90 | 90 | * @return self |
| 91 | 91 | */ |
| 92 | - public function addMeta(array $data):SuccessResponseBuilder |
|
| 92 | + public function addMeta( array $data ):SuccessResponseBuilder |
|
| 93 | 93 | { |
| 94 | - $this->meta = array_merge($this->meta, $data); |
|
| 94 | + $this->meta = array_merge( $this->meta, $data ); |
|
| 95 | 95 | |
| 96 | 96 | return $this; |
| 97 | 97 | } |
@@ -104,11 +104,11 @@ discard block |
||
| 104 | 104 | */ |
| 105 | 105 | public function include($relations):SuccessResponseBuilder |
| 106 | 106 | { |
| 107 | - if (is_string($relations)) { |
|
| 108 | - $relations = explode(',', $relations); |
|
| 107 | + if ( is_string( $relations ) ) { |
|
| 108 | + $relations = explode( ',', $relations ); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - $this->relations = array_merge($this->relations, (array) $relations); |
|
| 111 | + $this->relations = array_merge( $this->relations, (array) $relations ); |
|
| 112 | 112 | |
| 113 | 113 | return $this; |
| 114 | 114 | } |
@@ -119,9 +119,9 @@ discard block |
||
| 119 | 119 | * @param \League\Fractal\Serializer\SerializerAbstract|string $serializer |
| 120 | 120 | * @return self |
| 121 | 121 | */ |
| 122 | - public function serializer($serializer):SuccessResponseBuilder |
|
| 122 | + public function serializer( $serializer ):SuccessResponseBuilder |
|
| 123 | 123 | { |
| 124 | - $this->manager->setSerializer($this->resolveSerializer($serializer)); |
|
| 124 | + $this->manager->setSerializer( $this->resolveSerializer( $serializer ) ); |
|
| 125 | 125 | |
| 126 | 126 | return $this; |
| 127 | 127 | } |
@@ -133,13 +133,13 @@ discard block |
||
| 133 | 133 | * @return self |
| 134 | 134 | * @throws \InvalidArgumentException |
| 135 | 135 | */ |
| 136 | - public function setStatus(int $statusCode):ResponseBuilder |
|
| 136 | + public function setStatus( int $statusCode ):ResponseBuilder |
|
| 137 | 137 | { |
| 138 | - if ($statusCode < 100 || $statusCode >= 400) { |
|
| 139 | - throw new InvalidArgumentException("{$statusCode} is not a valid success HTTP status code."); |
|
| 138 | + if ( $statusCode < 100 || $statusCode >= 400 ) { |
|
| 139 | + throw new InvalidArgumentException( "{$statusCode} is not a valid success HTTP status code." ); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - return parent::setStatus($statusCode); |
|
| 142 | + return parent::setStatus( $statusCode ); |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | /** |
@@ -151,25 +151,25 @@ discard block |
||
| 151 | 151 | * @param string|null $resourceKey |
| 152 | 152 | * @return self |
| 153 | 153 | */ |
| 154 | - public function transform($data = null, $transformer = null, string $resourceKey = null):SuccessResponseBuilder |
|
| 154 | + public function transform( $data = null, $transformer = null, string $resourceKey = null ):SuccessResponseBuilder |
|
| 155 | 155 | { |
| 156 | - $resource = $this->resourceFactory->make($data); |
|
| 156 | + $resource = $this->resourceFactory->make( $data ); |
|
| 157 | 157 | |
| 158 | - if (! is_null($resource->getData())) { |
|
| 159 | - $model = $this->resolveModel($resource->getData()); |
|
| 160 | - $transformer = $this->resolveTransformer($model, $transformer); |
|
| 161 | - $resourceKey = $this->resolveResourceKey($model, $resourceKey); |
|
| 158 | + if ( ! is_null( $resource->getData() ) ) { |
|
| 159 | + $model = $this->resolveModel( $resource->getData() ); |
|
| 160 | + $transformer = $this->resolveTransformer( $model, $transformer ); |
|
| 161 | + $resourceKey = $this->resolveResourceKey( $model, $resourceKey ); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | - if ($transformer instanceof Transformer) { |
|
| 165 | - $this->include($relations = $this->resolveNestedRelations($resource->getData())); |
|
| 164 | + if ( $transformer instanceof Transformer ) { |
|
| 165 | + $this->include( $relations = $this->resolveNestedRelations( $resource->getData() ) ); |
|
| 166 | 166 | |
| 167 | - if ($transformer->allRelationsAllowed()) { |
|
| 168 | - $transformer->setRelations($relations); |
|
| 167 | + if ( $transformer->allRelationsAllowed() ) { |
|
| 168 | + $transformer->setRelations( $relations ); |
|
| 169 | 169 | } |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | - $this->resource = $resource->setTransformer($transformer)->setResourceKey($resourceKey); |
|
| 172 | + $this->resource = $resource->setTransformer( $transformer )->setResourceKey( $resourceKey ); |
|
| 173 | 173 | |
| 174 | 174 | return $this; |
| 175 | 175 | } |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | */ |
| 182 | 182 | public function toArray():array |
| 183 | 183 | { |
| 184 | - return $this->serialize($this->getResource()); |
|
| 184 | + return $this->serialize( $this->getResource() ); |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | /** |
@@ -191,14 +191,14 @@ discard block |
||
| 191 | 191 | */ |
| 192 | 192 | public function getResource():ResourceInterface |
| 193 | 193 | { |
| 194 | - $this->manager->parseIncludes($this->relations); |
|
| 194 | + $this->manager->parseIncludes( $this->relations ); |
|
| 195 | 195 | $transformer = $this->resource->getTransformer(); |
| 196 | 196 | |
| 197 | - if ($transformer instanceof Transformer && $transformer->allRelationsAllowed()) { |
|
| 198 | - $this->resource->setTransformer($transformer->setRelations($this->manager->getRequestedIncludes())); |
|
| 197 | + if ( $transformer instanceof Transformer && $transformer->allRelationsAllowed() ) { |
|
| 198 | + $this->resource->setTransformer( $transformer->setRelations( $this->manager->getRequestedIncludes() ) ); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - return $this->resource->setMeta($this->meta); |
|
| 201 | + return $this->resource->setMeta( $this->meta ); |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | /** |
@@ -218,13 +218,13 @@ discard block |
||
| 218 | 218 | * @return \League\Fractal\Serializer\SerializerAbstract |
| 219 | 219 | * @throws \Flugg\Responder\Exceptions\InvalidSerializerException |
| 220 | 220 | */ |
| 221 | - protected function resolveSerializer($serializer):SerializerAbstract |
|
| 221 | + protected function resolveSerializer( $serializer ):SerializerAbstract |
|
| 222 | 222 | { |
| 223 | - if (is_string($serializer)) { |
|
| 223 | + if ( is_string( $serializer ) ) { |
|
| 224 | 224 | $serializer = new $serializer; |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | - if (! $serializer instanceof SerializerAbstract) { |
|
| 227 | + if ( ! $serializer instanceof SerializerAbstract ) { |
|
| 228 | 228 | throw new InvalidSerializerException(); |
| 229 | 229 | } |
| 230 | 230 | |
@@ -238,15 +238,15 @@ discard block |
||
| 238 | 238 | * @return \Illuminate\Database\Eloquent\Model |
| 239 | 239 | * @throws \InvalidArgumentException |
| 240 | 240 | */ |
| 241 | - protected function resolveModel($data):Model |
|
| 241 | + protected function resolveModel( $data ):Model |
|
| 242 | 242 | { |
| 243 | - if ($data instanceof Model) { |
|
| 243 | + if ( $data instanceof Model ) { |
|
| 244 | 244 | return $data; |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - $model = array_values($data)[0]; |
|
| 248 | - if (! $model instanceof Model) { |
|
| 249 | - throw new InvalidArgumentException('You can only transform data containing Eloquent models.'); |
|
| 247 | + $model = array_values( $data )[ 0 ]; |
|
| 248 | + if ( ! $model instanceof Model ) { |
|
| 249 | + throw new InvalidArgumentException( 'You can only transform data containing Eloquent models.' ); |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | return $model; |
@@ -259,15 +259,15 @@ discard block |
||
| 259 | 259 | * @param \Flugg\Responder\Transformer|callable|null $transformer |
| 260 | 260 | * @return \Flugg\Responder\Transformer|callable |
| 261 | 261 | */ |
| 262 | - protected function resolveTransformer(Model $model, $transformer = null) |
|
| 262 | + protected function resolveTransformer( Model $model, $transformer = null ) |
|
| 263 | 263 | { |
| 264 | - $transformer = $transformer ?: $this->resolveTransformerFromModel($model); |
|
| 264 | + $transformer = $transformer ?: $this->resolveTransformerFromModel( $model ); |
|
| 265 | 265 | |
| 266 | - if (is_string($transformer)) { |
|
| 266 | + if ( is_string( $transformer ) ) { |
|
| 267 | 267 | $transformer = new $transformer; |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | - return $this->parseTransformer($transformer, $model); |
|
| 270 | + return $this->parseTransformer( $transformer, $model ); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | /** |
@@ -277,10 +277,10 @@ discard block |
||
| 277 | 277 | * @param \Illuminate\Database\ELoquent\Model $model |
| 278 | 278 | * @return \Flugg\Responder\Transformer|callable |
| 279 | 279 | */ |
| 280 | - protected function resolveTransformerFromModel(Model $model) |
|
| 280 | + protected function resolveTransformerFromModel( Model $model ) |
|
| 281 | 281 | { |
| 282 | - if (! $model instanceof Transformable) { |
|
| 283 | - return function ($model) { |
|
| 282 | + if ( ! $model instanceof Transformable ) { |
|
| 283 | + return function ( $model ) { |
|
| 284 | 284 | return $model->toArray(); |
| 285 | 285 | }; |
| 286 | 286 | } |
@@ -296,14 +296,14 @@ discard block |
||
| 296 | 296 | * @return \Flugg\Responder\Transformer|callable |
| 297 | 297 | * @throws \InvalidTransformerException |
| 298 | 298 | */ |
| 299 | - protected function parseTransformer($transformer, Model $model) |
|
| 299 | + protected function parseTransformer( $transformer, Model $model ) |
|
| 300 | 300 | { |
| 301 | - if ($transformer instanceof Transformer) { |
|
| 302 | - $relations = $transformer->allRelationsAllowed() ? $this->resolveRelations($model) : $transformer->getRelations(); |
|
| 303 | - $transformer = $transformer->setRelations($relations); |
|
| 301 | + if ( $transformer instanceof Transformer ) { |
|
| 302 | + $relations = $transformer->allRelationsAllowed() ? $this->resolveRelations( $model ) : $transformer->getRelations(); |
|
| 303 | + $transformer = $transformer->setRelations( $relations ); |
|
| 304 | 304 | |
| 305 | - } elseif (! is_callable($transformer)) { |
|
| 306 | - throw new InvalidTransformerException($model); |
|
| 305 | + } elseif ( ! is_callable( $transformer ) ) { |
|
| 306 | + throw new InvalidTransformerException( $model ); |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | return $transformer; |
@@ -315,9 +315,9 @@ discard block |
||
| 315 | 315 | * @param \Illuminate\Database\Eloquent\Model $model |
| 316 | 316 | * @return array |
| 317 | 317 | */ |
| 318 | - protected function resolveRelations(Model $model):array |
|
| 318 | + protected function resolveRelations( Model $model ):array |
|
| 319 | 319 | { |
| 320 | - return array_keys($model->getRelations()); |
|
| 320 | + return array_keys( $model->getRelations() ); |
|
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | /** |
@@ -326,22 +326,22 @@ discard block |
||
| 326 | 326 | * @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $data |
| 327 | 327 | * @return array |
| 328 | 328 | */ |
| 329 | - protected function resolveNestedRelations($data):array |
|
| 329 | + protected function resolveNestedRelations( $data ):array |
|
| 330 | 330 | { |
| 331 | - if (is_null($data)) { |
|
| 332 | - return []; |
|
| 331 | + if ( is_null( $data ) ) { |
|
| 332 | + return [ ]; |
|
| 333 | 333 | } |
| 334 | 334 | |
| 335 | - $data = $data instanceof Model ? [$data] : $data; |
|
| 335 | + $data = $data instanceof Model ? [ $data ] : $data; |
|
| 336 | 336 | |
| 337 | - return collect($data)->flatMap(function ($model) { |
|
| 338 | - $relations = collect($model->getRelations()); |
|
| 337 | + return collect( $data )->flatMap( function ( $model ) { |
|
| 338 | + $relations = collect( $model->getRelations() ); |
|
| 339 | 339 | |
| 340 | - return $relations->keys()->merge($relations->flatMap(function ($relation, $key) { |
|
| 341 | - return collect($this->resolveNestedRelations($relation))->map(function ($nestedRelation) use ($key) { |
|
| 340 | + return $relations->keys()->merge( $relations->flatMap( function ( $relation, $key ) { |
|
| 341 | + return collect( $this->resolveNestedRelations( $relation ) )->map( function ( $nestedRelation ) use ($key) { |
|
| 342 | 342 | return $key . '.' . $nestedRelation; |
| 343 | 343 | }); |
| 344 | - })); |
|
| 344 | + }) ); |
|
| 345 | 345 | })->unique()->toArray(); |
| 346 | 346 | } |
| 347 | 347 | |
@@ -352,13 +352,13 @@ discard block |
||
| 352 | 352 | * @param string|null $resourceKey |
| 353 | 353 | * @return string |
| 354 | 354 | */ |
| 355 | - protected function resolveResourceKey(Model $model, string $resourceKey = null):string |
|
| 355 | + protected function resolveResourceKey( Model $model, string $resourceKey = null ):string |
|
| 356 | 356 | { |
| 357 | - if (! is_null($resourceKey)) { |
|
| 357 | + if ( ! is_null( $resourceKey ) ) { |
|
| 358 | 358 | return $resourceKey; |
| 359 | 359 | } |
| 360 | 360 | |
| 361 | - if (method_exists($model, 'getResourceKey')) { |
|
| 361 | + if ( method_exists( $model, 'getResourceKey' ) ) { |
|
| 362 | 362 | return $model->getResourceKey(); |
| 363 | 363 | } |
| 364 | 364 | |
@@ -371,8 +371,8 @@ discard block |
||
| 371 | 371 | * @param ResourceInterface $resource |
| 372 | 372 | * @return array |
| 373 | 373 | */ |
| 374 | - protected function serialize(ResourceInterface $resource):array |
|
| 374 | + protected function serialize( ResourceInterface $resource ):array |
|
| 375 | 375 | { |
| 376 | - return $this->manager->createData($resource)->toArray(); |
|
| 376 | + return $this->manager->createData( $resource )->toArray(); |
|
| 377 | 377 | } |
| 378 | 378 | } |