Skip to content

Commit

Permalink
Merge pull request #19 from jarischaefer/development
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
jarischaefer committed Mar 26, 2016
2 parents 8a4a9ae + 80379f8 commit 277bfe3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 178 deletions.
26 changes: 13 additions & 13 deletions src/Jarischaefer/HalApi/Controllers/HalApiResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class HalApiResourceController extends HalApiController implements HalA
/**
* The model's transformer.
*
* @var HalApiTransformer
* @var HalApiTransformerContract
*/
protected $transformer;
/**
Expand Down Expand Up @@ -69,10 +69,10 @@ abstract class HalApiResourceController extends HalApiController implements HalA

/**
* @param HalApiControllerParameters $parameters
* @param HalApiTransformer $transformer
* @param HalApiTransformerContract $transformer
* @param Builder $schemaBuilder
*/
public function __construct(HalApiControllerParameters $parameters, HalApiTransformer $transformer, Builder $schemaBuilder)
public function __construct(HalApiControllerParameters $parameters, HalApiTransformerContract $transformer, Builder $schemaBuilder)
{
parent::__construct($parameters);

Expand All @@ -84,10 +84,6 @@ public function __construct(HalApiControllerParameters $parameters, HalApiTransf
$this->schemaBuilder = $schemaBuilder;
$this->model = static::getModel();

if (!is_subclass_of($this->transformer, HalApiTransformerContract::class)) {
throw new RuntimeException('Transformer must be child of ' . HalApiTransformerContract::class);
}

if (!is_subclass_of($this->model, Model::class)) {
throw new RuntimeException('Model must be child of ' . Model::class);
}
Expand All @@ -101,21 +97,21 @@ public function __construct(HalApiControllerParameters $parameters, HalApiTransf
*/
public static function getModelBindingCallback()
{
return function () {
return function ($value) {
switch (\Request::getMethod()) {
case Request::METHOD_GET:
throw new NotFoundHttpException;
case Request::METHOD_POST:
throw new NotFoundHttpException;
case Request::METHOD_PUT:
return null;
return $value;
case Request::METHOD_PATCH:
throw new NotFoundHttpException;
case Request::METHOD_DELETE:
throw new NotFoundHttpException;
default:
return null;
}

return null;
};
}

Expand Down Expand Up @@ -233,10 +229,14 @@ public function store()
/**
* @inheritdoc
*/
public function update($model = null)
public function update($model)
{
/** @var Model $model */
$model = $model ?: new $this->model;
if (!($model instanceof Model)) {
$id = $model;
$model = new $this->model;
$model->{$model->getKeyName()} = $id;
}

switch ($this->request->getMethod()) {
case Request::METHOD_PUT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Jarischaefer\HalApi\Exceptions\BadPutRequestException;
use Jarischaefer\HalApi\Exceptions\DatabaseConflictException;
use Jarischaefer\HalApi\Exceptions\DatabaseSaveException;
use Jarischaefer\HalApi\Transformers\HalApiTransformer;
use Jarischaefer\HalApi\Transformers\HalApiTransformerContract;
use Symfony\Component\HttpFoundation\Response;

/**
Expand Down Expand Up @@ -38,11 +38,12 @@ public static function getModel();
* exception.
*
* If you do not typehint your method (take a look at the update method in this class), the variable passed
* will be null. Otherwise an instance of your model with the ->exists property set to false is passed.
* will be the original route parameter.
* Otherwise, an instance of your model with the ->exists property set to false is passed.
*
* public function update($user = null)
* public function update($user)
* {
* var_dump($user) // null if not found in database
* var_dump($user) // original route parameter (e.g. new resource's ID) if not found in database
* }
*
* public function update(User $user)
Expand All @@ -60,7 +61,7 @@ public static function getModelBindingCallback();
/**
* Returns an instance of a transformer to be used for all transformations in this controller.
*
* @return HalApiTransformer
* @return HalApiTransformerContract
*/
public function getTransformer();

Expand Down Expand Up @@ -96,12 +97,12 @@ public function store();
* Handles PUT and PATCH requests trying to create or update a model. Parameters are taken from the JSON request
* body. PUT requests must contain all fillable attributes.
*
* @param null $model
* @param Model|mixed $model
* @return array
* @throws BadPutRequestException
* @throws DatabaseSaveException
*/
public function update($model = null);
public function update($model);

/**
* @param $model
Expand Down
5 changes: 0 additions & 5 deletions src/Jarischaefer/HalApi/Providers/HalApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
use Jarischaefer\HalApi\Routing\HalApiUrlGenerator;
use Jarischaefer\HalApi\Routing\LinkFactory;
use Jarischaefer\HalApi\Routing\LinkFactoryImpl;
use Jarischaefer\HalApi\Transformers\TransformerFactory;
use Jarischaefer\HalApi\Transformers\TransformerFactoryImpl;

/**
* Class HalApiServiceProvider
Expand Down Expand Up @@ -69,8 +67,6 @@ class HalApiServiceProvider extends ServiceProvider
self::BASE_PATH . 'Routing' . DIRECTORY_SEPARATOR . 'LinkFactoryImpl.php',

self::BASE_PATH . 'Transformers' . DIRECTORY_SEPARATOR . 'HalApiTransformer.php',
self::BASE_PATH . 'Transformers' . DIRECTORY_SEPARATOR . 'TransformerFactory.php',
self::BASE_PATH . 'Transformers' . DIRECTORY_SEPARATOR . 'TransformerFactoryImpl.php',
];

/**
Expand Down Expand Up @@ -117,7 +113,6 @@ public function register()
return $databaseManager->connection()->getSchemaBuilder();
});
$this->app->singleton(CacheFactory::class, CacheFactoryImpl::class);
$this->app->singleton(TransformerFactory::class, TransformerFactoryImpl::class);
$this->app->singleton(RepresentationFactory::class, RepresentationFactoryImpl::class);
$this->app->singleton(LinkFactory::class, function(Application $application) {
return new LinkFactoryImpl($application->make(HalApiUrlGenerator::class));
Expand Down

This file was deleted.

21 changes: 0 additions & 21 deletions src/Jarischaefer/HalApi/Transformers/TransformerFactory.php

This file was deleted.

68 changes: 0 additions & 68 deletions src/Jarischaefer/HalApi/Transformers/TransformerFactoryImpl.php

This file was deleted.

0 comments on commit 277bfe3

Please sign in to comment.