Skip to content

Commit

Permalink
Clean up docblocks and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
flugg committed Jan 28, 2018
1 parent 44e6cd5 commit a2c7314
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 39 deletions.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,3 +1,2 @@
.idea/
/vendor
composer.lock
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -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.*",
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/ErrorSerializer.php
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Resources/ResourceKeyResolver.php
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/Contracts/Responder.php
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ErrorMessageResolver.php
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Http/MakesResponses.php
Expand Up @@ -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
*/
Expand Down
4 changes: 1 addition & 3 deletions src/Http/Middleware/ConvertToSnakeCase.php
Expand Up @@ -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 <flugged@gmail.com>
* @license The MIT License
*
* @see \Flugg\Responder\Responder
*/
class ConvertToSnakeCase extends TransformsRequest
{
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Responses/Decorators/EscapeHtmlDecorator.php
Expand Up @@ -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);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Responses/ErrorResponseBuilder.php
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Responses/SuccessResponseBuilder.php
Expand Up @@ -89,7 +89,7 @@ public function __call($name, $arguments)
/**
* Get the serialized response output.
*
* @return array
* @return mixed
*/
protected function getOutput(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Pagination/CursorPaginator.php
Expand Up @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Resources/ResourceFactory.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
Expand Down
38 changes: 19 additions & 19 deletions src/Resources/ResourceKeyResolver.php
Expand Up @@ -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)];
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Responder.php
Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Serializers/ErrorSerializer.php
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/Serializers/SuccessSerializer.php
Expand Up @@ -4,7 +4,6 @@

use League\Fractal\Pagination\CursorInterface;
use League\Fractal\Pagination\PaginatorInterface;
use League\Fractal\Resource\ResourceInterface;
use League\Fractal\Serializer\ArraySerializer;

/**
Expand Down

0 comments on commit a2c7314

Please sign in to comment.