diff --git a/.gitignore b/.gitignore index 31dc5a1..dfd6caa 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ -.idea/ /vendor composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json index 41fdcd8..c26fbd4 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "php": "^7.0", "illuminate/contracts": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.*", "illuminate/support": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.*", - "league/fractal": "^0.16.0" + "league/fractal": "^0.17.0" }, "require-dev": { "illuminate/database": "5.4.*", diff --git a/src/Contracts/ErrorSerializer.php b/src/Contracts/ErrorSerializer.php index e0f61d2..faaaec5 100644 --- a/src/Contracts/ErrorSerializer.php +++ b/src/Contracts/ErrorSerializer.php @@ -14,7 +14,7 @@ interface ErrorSerializer /** * Format the error data. * - * @param mixed|null $errorCode + * @param mixed|null $errorCode * @param string|null $message * @param array|null $data * @return array diff --git a/src/Contracts/Resources/ResourceKeyResolver.php b/src/Contracts/Resources/ResourceKeyResolver.php index cb22aa1..ec33557 100644 --- a/src/Contracts/Resources/ResourceKeyResolver.php +++ b/src/Contracts/Resources/ResourceKeyResolver.php @@ -24,7 +24,7 @@ public function bind($transformable, string $resourceKey); * Resolve a resource key from the given data. * * @param mixed $data - * @return string|null + * @return string */ public function resolve($data); } \ No newline at end of file diff --git a/src/Contracts/Responder.php b/src/Contracts/Responder.php index a282517..aa8d48d 100644 --- a/src/Contracts/Responder.php +++ b/src/Contracts/Responder.php @@ -27,7 +27,7 @@ public function success($data = null, $transformer = null, string $resourceKey = /** * Build an error response. * - * @param mixed|null $errorCode + * @param mixed|null $errorCode * @param string|null $message * @return \Flugg\Responder\Http\Responses\ErrorResponseBuilder */ diff --git a/src/ErrorMessageResolver.php b/src/ErrorMessageResolver.php index 7cd236e..677bc99 100644 --- a/src/ErrorMessageResolver.php +++ b/src/ErrorMessageResolver.php @@ -41,7 +41,7 @@ public function __construct(Translator $translator) /** * Register a message mapped to an error code. * - * @param mixed $errorCode + * @param mixed $errorCode * @param string $message * @return void */ diff --git a/src/Http/MakesResponses.php b/src/Http/MakesResponses.php index cd65715..5574810 100644 --- a/src/Http/MakesResponses.php +++ b/src/Http/MakesResponses.php @@ -31,7 +31,7 @@ public function success($data = null, $transformer = null, string $resourceKey = /** * Build an error response. * - * @param mixed|null $errorCode + * @param mixed|null $errorCode * @param string|null $message * @return \Flugg\Responder\Http\Responses\ErrorResponseBuilder */ diff --git a/src/Http/Middleware/ConvertToSnakeCase.php b/src/Http/Middleware/ConvertToSnakeCase.php index 8c21133..452398e 100644 --- a/src/Http/Middleware/ConvertToSnakeCase.php +++ b/src/Http/Middleware/ConvertToSnakeCase.php @@ -5,13 +5,11 @@ use Illuminate\Foundation\Http\Middleware\TransformsRequest; /** - * A middleware class responsible for converting parameter keys to snake case. + * A middleware class responsible for converting incoming parameter keys to snake case. * * @package flugger/laravel-responder * @author Alexander Tømmerås * @license The MIT License - * - * @see \Flugg\Responder\Responder */ class ConvertToSnakeCase extends TransformsRequest { diff --git a/src/Http/Responses/Decorators/EscapeHtmlDecorator.php b/src/Http/Responses/Decorators/EscapeHtmlDecorator.php index 3f5c1ec..bfc67af 100644 --- a/src/Http/Responses/Decorators/EscapeHtmlDecorator.php +++ b/src/Http/Responses/Decorators/EscapeHtmlDecorator.php @@ -17,14 +17,14 @@ class EscapeHtmlDecorator extends ResponseDecorator * Generate a JSON response. * * @param array $data - * @param int $status + * @param int $status * @param array $headers * @return \Illuminate\Http\JsonResponse */ public function make(array $data, int $status, array $headers = []): JsonResponse { array_walk_recursive($data, function (&$value) { - if(is_string($value)) { + if (is_string($value)) { $value = e($value); } }); diff --git a/src/Http/Responses/ErrorResponseBuilder.php b/src/Http/Responses/ErrorResponseBuilder.php index 646189a..ab6e10d 100644 --- a/src/Http/Responses/ErrorResponseBuilder.php +++ b/src/Http/Responses/ErrorResponseBuilder.php @@ -75,7 +75,7 @@ public function __construct(ResponseFactory $responseFactory, ErrorFactory $erro /** * Set the error code and message. * - * @param mixed|null $errorCode + * @param mixed|null $errorCode * @param string|null $message * @return $this */ diff --git a/src/Http/Responses/SuccessResponseBuilder.php b/src/Http/Responses/SuccessResponseBuilder.php index 591df79..8e0e6f0 100644 --- a/src/Http/Responses/SuccessResponseBuilder.php +++ b/src/Http/Responses/SuccessResponseBuilder.php @@ -89,7 +89,7 @@ public function __call($name, $arguments) /** * Get the serialized response output. * - * @return array + * @return mixed */ protected function getOutput(): array { diff --git a/src/Pagination/CursorPaginator.php b/src/Pagination/CursorPaginator.php index b0ad784..61e4561 100644 --- a/src/Pagination/CursorPaginator.php +++ b/src/Pagination/CursorPaginator.php @@ -125,7 +125,7 @@ public function get(): Collection */ public function set($data): CursorPaginator { - $this->items = $data instanceof Collection ? $data : Collection::make($data);; + $this->items = $data instanceof Collection ? $data : collect($data); return $this; } diff --git a/src/Resources/ResourceFactory.php b/src/Resources/ResourceFactory.php index 9508330..2dbb248 100644 --- a/src/Resources/ResourceFactory.php +++ b/src/Resources/ResourceFactory.php @@ -9,6 +9,7 @@ use League\Fractal\Resource\Collection as CollectionResource; use League\Fractal\Resource\Item as ItemResource; use League\Fractal\Resource\NullResource; +use League\Fractal\Resource\Primitive; use League\Fractal\Resource\ResourceInterface; use Traversable; @@ -81,7 +82,7 @@ public function make($data = null, $transformer = null, string $resourceKey = nu /** * Make resource from the given resource. * - * @param \League\Fractal\Resource\ResourceInterface $resource + * @param \League\Fractal\Resource\ResourceInterface $resource * @param \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer * @param string|null $resourceKey * @return \League\Fractal\Resource\ResourceInterface @@ -108,6 +109,8 @@ protected function instatiateResource($data, $transformer = null, string $resour return new NullResource(null, null, $resourceKey); } elseif ($this->shouldCreateCollection($data)) { return new CollectionResource($data, $transformer, $resourceKey); + } elseif (is_scalar($data)) { + return new Primitive($data, $transformer, $resourceKey); } return new ItemResource($data, $transformer, $resourceKey); @@ -122,7 +125,7 @@ protected function instatiateResource($data, $transformer = null, string $resour protected function shouldCreateCollection($data): bool { if (is_array($data)) { - return ! is_scalar(Arr::first($data)); + return ! Arr::isAssoc($data) && ! is_scalar(Arr::first($data)); } return $data instanceof Traversable; diff --git a/src/Resources/ResourceKeyResolver.php b/src/Resources/ResourceKeyResolver.php index c617863..fba3098 100644 --- a/src/Resources/ResourceKeyResolver.php +++ b/src/Resources/ResourceKeyResolver.php @@ -40,11 +40,11 @@ public function bind($transformable, string $resourceKey) * Resolve a resource key from the given data. * * @param mixed $data - * @return string|null + * @return string */ public function resolve($data) { - $transformable = $this->resolveTransformable($data); + $transformable = $this->resolveTransformableItem($data); if (is_object($transformable) && key_exists(get_class($transformable), $this->bindings)) { return $this->bindings[get_class($transformable)]; @@ -54,38 +54,38 @@ public function resolve($data) return $this->resolveFromModel($transformable); } - return null; + return 'data'; } /** - * Resolve a transformable from the given data. + * Resolve a resource key from the given model. * - * @param mixed $data - * @return mixed + * @param \Illuminate\Database\Eloquent\Model $model + * @return string */ - protected function resolveTransformable($data) + public function resolveFromModel(Model $model) { - if (is_array($data) || $data instanceof Traversable) { - foreach ($data as $item) { - return $item; - } + if (method_exists($model, 'getResourceKey')) { + return $model->getResourceKey(); } - return $data; + return $model->getTable(); } /** - * Resolve a resource key from the given model. + * Resolve a transformable from the given data. * - * @param \Illuminate\Database\Eloquent\Model $model - * @return string + * @param mixed $data + * @return mixed */ - protected function resolveFromModel(Model $model) + protected function resolveTransformableItem($data) { - if (method_exists($model, 'getResourceKey')) { - return $model->getResourceKey(); + if (is_array($data) || $data instanceof Traversable) { + foreach ($data as $item) { + return $item; + } } - return $model->getTable(); + return $data; } } \ No newline at end of file diff --git a/src/Responder.php b/src/Responder.php index 192c9e2..750a950 100644 --- a/src/Responder.php +++ b/src/Responder.php @@ -57,7 +57,7 @@ public function success($data = null, $transformer = null, string $resourceKey = /** * Build an error response. * - * @param mixed|null $errorCode + * @param mixed|null $errorCode * @param string|null $message * @return \Flugg\Responder\Http\Responses\ErrorResponseBuilder */ diff --git a/src/Serializers/ErrorSerializer.php b/src/Serializers/ErrorSerializer.php index 36c2dfa..1550898 100644 --- a/src/Serializers/ErrorSerializer.php +++ b/src/Serializers/ErrorSerializer.php @@ -16,7 +16,7 @@ class ErrorSerializer implements ErrorSerializerContract /** * Format the error data. * - * @param mixed|null $errorCode + * @param mixed|null $errorCode * @param string|null $message * @param array|null $data * @return array diff --git a/src/Serializers/SuccessSerializer.php b/src/Serializers/SuccessSerializer.php index 7699b11..797d41c 100644 --- a/src/Serializers/SuccessSerializer.php +++ b/src/Serializers/SuccessSerializer.php @@ -4,7 +4,6 @@ use League\Fractal\Pagination\CursorInterface; use League\Fractal\Pagination\PaginatorInterface; -use League\Fractal\Resource\ResourceInterface; use League\Fractal\Serializer\ArraySerializer; /**