Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pactode committed May 23, 2019
1 parent 3de6a32 commit d62cba4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/Responder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Support\Responsable;
use Signifly\Responder\Support\ModelResolver;
use Signifly\Responder\Contracts\ModelResolver;
use Signifly\Responder\Responses\ModelResponse;
use Signifly\Responder\Responses\DefaultResponse;
use Signifly\Responder\Contracts\ResourceResolver;
Expand All @@ -16,10 +16,10 @@

class Responder implements Contract
{
/** @var \Signifly\Responder\Support\ModelResolver */
/** @var \Signifly\Responder\Contracts\ModelResolver */
protected $modelResolver;

/** @var \Signifly\Responder\Support\ResourceResolver */
/** @var \Signifly\Responder\Contracts\ResourceResolver */
protected $resourceResolver;

public function __construct(
Expand Down Expand Up @@ -62,7 +62,7 @@ public function respond($data): Responsable
*/
protected function respondForCollection(Collection $data)
{
$modelClass = $model ?? $this->modelResolver->resolve($data, 'collection');
$modelClass = $this->modelResolver->resolve($data, 'collection');
$resourceClass = $this->resourceResolver->resolve($modelClass);

return new CollectionResponse($data, $resourceClass);
Expand Down Expand Up @@ -90,7 +90,7 @@ protected function respondForModel(Model $model)
*/
protected function respondForPaginator(LengthAwarePaginator $data)
{
$modelClass = $model ?? $this->modelResolver->resolve($data, 'paginator');
$modelClass = $this->modelResolver->resolve($data, 'paginator');
$resourceClass = $this->resourceResolver->resolve($modelClass);

return new PaginatorResponse($data, $resourceClass);
Expand Down
16 changes: 0 additions & 16 deletions src/Responses/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,4 @@

abstract class Response implements Responsable
{
/**
* Get the http resource class for a given model.
*
* @param string $model
* @return string
*/
protected function getResourceClassFor(string $model)
{
$resourceClass = ResourceResolver::forModel($model);

if (config('responder.force_resources') && ! class_exists($resourceClass)) {
throw new Exception(sprintf('Could not find a resource for %s', $model));
}

return $resourceClass;
}
}
37 changes: 32 additions & 5 deletions src/Support/ModelResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Signifly\Responder\Contracts\ModelResolver as Contract;

class ModelResolver implements Contract
{
/**
* Resolve the model from data for a given type.
*
* @param mixed $data
* @param string $type
* @return string|null
*/
public function resolve($data, string $type): ?string
{
$methodName = 'resolveFor'.Str::studly($type);
Expand All @@ -23,14 +32,26 @@ public function resolve($data, string $type): ?string
);
}

/**
* Guard against invalid item.
*
* @param mixed $item
* @return void
*/
protected function guardAgainstInvalidItem($item): void
{
if (! $item instanceof Model) {
throw new Exception('The collection should consists of models.');
}
}

protected function resolveForArray($data): ?string
/**
* Resolve for an array.
*
* @param array $data
* @return string|null
*/
protected function resolveForArray(array $data): ?string
{
if (empty($data)) {
return null;
Expand All @@ -41,7 +62,13 @@ protected function resolveForArray($data): ?string
return $this->resolveItem($item);
}

protected function resolveForCollection($data): ?string
/**
* Resolve for a collection.
*
* @param \Illuminate\Support\Collection $data
* @return string|null
*/
protected function resolveForCollection(Collection $data): ?string
{
if ($data->isEmpty()) {
return null;
Expand All @@ -55,10 +82,10 @@ protected function resolveForCollection($data): ?string
/**
* Resolve for paginator.
*
* @param [type] $data
* @return string
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $data
* @return string|null
*/
protected function resolveForPaginator($data): ?string
protected function resolveForPaginator(LengthAwarePaginator $data): ?string
{
if ($data->isEmpty()) {
return null;
Expand Down
7 changes: 7 additions & 0 deletions src/Support/ResourceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

namespace Signifly\Responder\Support;

use Signifly\Responder\Exceptions\ResourceNotFoundException;
use Signifly\Responder\Contracts\ResourceResolver as Contract;

class ResourceResolver implements Contract
{
/**
* Resolve a resource from a model class.
*
* @param string $model
* @return string|null
*/
public function resolve(string $model): ?string
{
if (empty($model)) {
Expand Down

0 comments on commit d62cba4

Please sign in to comment.