Skip to content

Commit 277bfe3

Browse files
committed
Merge pull request #19 from jarischaefer/development
merge
2 parents 8a4a9ae + 80379f8 commit 277bfe3

6 files changed

Lines changed: 21 additions & 178 deletions

File tree

src/Jarischaefer/HalApi/Controllers/HalApiResourceController.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ abstract class HalApiResourceController extends HalApiController implements HalA
4141
/**
4242
* The model's transformer.
4343
*
44-
* @var HalApiTransformer
44+
* @var HalApiTransformerContract
4545
*/
4646
protected $transformer;
4747
/**
@@ -69,10 +69,10 @@ abstract class HalApiResourceController extends HalApiController implements HalA
6969

7070
/**
7171
* @param HalApiControllerParameters $parameters
72-
* @param HalApiTransformer $transformer
72+
* @param HalApiTransformerContract $transformer
7373
* @param Builder $schemaBuilder
7474
*/
75-
public function __construct(HalApiControllerParameters $parameters, HalApiTransformer $transformer, Builder $schemaBuilder)
75+
public function __construct(HalApiControllerParameters $parameters, HalApiTransformerContract $transformer, Builder $schemaBuilder)
7676
{
7777
parent::__construct($parameters);
7878

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

87-
if (!is_subclass_of($this->transformer, HalApiTransformerContract::class)) {
88-
throw new RuntimeException('Transformer must be child of ' . HalApiTransformerContract::class);
89-
}
90-
9187
if (!is_subclass_of($this->model, Model::class)) {
9288
throw new RuntimeException('Model must be child of ' . Model::class);
9389
}
@@ -101,21 +97,21 @@ public function __construct(HalApiControllerParameters $parameters, HalApiTransf
10197
*/
10298
public static function getModelBindingCallback()
10399
{
104-
return function () {
100+
return function ($value) {
105101
switch (\Request::getMethod()) {
106102
case Request::METHOD_GET:
107103
throw new NotFoundHttpException;
108104
case Request::METHOD_POST:
109105
throw new NotFoundHttpException;
110106
case Request::METHOD_PUT:
111-
return null;
107+
return $value;
112108
case Request::METHOD_PATCH:
113109
throw new NotFoundHttpException;
114110
case Request::METHOD_DELETE:
115111
throw new NotFoundHttpException;
112+
default:
113+
return null;
116114
}
117-
118-
return null;
119115
};
120116
}
121117

@@ -233,10 +229,14 @@ public function store()
233229
/**
234230
* @inheritdoc
235231
*/
236-
public function update($model = null)
232+
public function update($model)
237233
{
238234
/** @var Model $model */
239-
$model = $model ?: new $this->model;
235+
if (!($model instanceof Model)) {
236+
$id = $model;
237+
$model = new $this->model;
238+
$model->{$model->getKeyName()} = $id;
239+
}
240240

241241
switch ($this->request->getMethod()) {
242242
case Request::METHOD_PUT:

src/Jarischaefer/HalApi/Controllers/HalApiResourceControllerContract.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Jarischaefer\HalApi\Exceptions\BadPutRequestException;
77
use Jarischaefer\HalApi\Exceptions\DatabaseConflictException;
88
use Jarischaefer\HalApi\Exceptions\DatabaseSaveException;
9-
use Jarischaefer\HalApi\Transformers\HalApiTransformer;
9+
use Jarischaefer\HalApi\Transformers\HalApiTransformerContract;
1010
use Symfony\Component\HttpFoundation\Response;
1111

1212
/**
@@ -38,11 +38,12 @@ public static function getModel();
3838
* exception.
3939
*
4040
* If you do not typehint your method (take a look at the update method in this class), the variable passed
41-
* will be null. Otherwise an instance of your model with the ->exists property set to false is passed.
41+
* will be the original route parameter.
42+
* Otherwise, an instance of your model with the ->exists property set to false is passed.
4243
*
43-
* public function update($user = null)
44+
* public function update($user)
4445
* {
45-
* var_dump($user) // null if not found in database
46+
* var_dump($user) // original route parameter (e.g. new resource's ID) if not found in database
4647
* }
4748
*
4849
* public function update(User $user)
@@ -60,7 +61,7 @@ public static function getModelBindingCallback();
6061
/**
6162
* Returns an instance of a transformer to be used for all transformations in this controller.
6263
*
63-
* @return HalApiTransformer
64+
* @return HalApiTransformerContract
6465
*/
6566
public function getTransformer();
6667

@@ -96,12 +97,12 @@ public function store();
9697
* Handles PUT and PATCH requests trying to create or update a model. Parameters are taken from the JSON request
9798
* body. PUT requests must contain all fillable attributes.
9899
*
99-
* @param null $model
100+
* @param Model|mixed $model
100101
* @return array
101102
* @throws BadPutRequestException
102103
* @throws DatabaseSaveException
103104
*/
104-
public function update($model = null);
105+
public function update($model);
105106

106107
/**
107108
* @param $model

src/Jarischaefer/HalApi/Providers/HalApiServiceProvider.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
use Jarischaefer\HalApi\Routing\HalApiUrlGenerator;
2121
use Jarischaefer\HalApi\Routing\LinkFactory;
2222
use Jarischaefer\HalApi\Routing\LinkFactoryImpl;
23-
use Jarischaefer\HalApi\Transformers\TransformerFactory;
24-
use Jarischaefer\HalApi\Transformers\TransformerFactoryImpl;
2523

2624
/**
2725
* Class HalApiServiceProvider
@@ -69,8 +67,6 @@ class HalApiServiceProvider extends ServiceProvider
6967
self::BASE_PATH . 'Routing' . DIRECTORY_SEPARATOR . 'LinkFactoryImpl.php',
7068

7169
self::BASE_PATH . 'Transformers' . DIRECTORY_SEPARATOR . 'HalApiTransformer.php',
72-
self::BASE_PATH . 'Transformers' . DIRECTORY_SEPARATOR . 'TransformerFactory.php',
73-
self::BASE_PATH . 'Transformers' . DIRECTORY_SEPARATOR . 'TransformerFactoryImpl.php',
7470
];
7571

7672
/**
@@ -117,7 +113,6 @@ public function register()
117113
return $databaseManager->connection()->getSchemaBuilder();
118114
});
119115
$this->app->singleton(CacheFactory::class, CacheFactoryImpl::class);
120-
$this->app->singleton(TransformerFactory::class, TransformerFactoryImpl::class);
121116
$this->app->singleton(RepresentationFactory::class, RepresentationFactoryImpl::class);
122117
$this->app->singleton(LinkFactory::class, function(Application $application) {
123118
return new LinkFactoryImpl($application->make(HalApiUrlGenerator::class));

src/Jarischaefer/HalApi/Tests/Transformers/TransformerFactoryImplTest.php

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/Jarischaefer/HalApi/Transformers/TransformerFactory.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Jarischaefer/HalApi/Transformers/TransformerFactoryImpl.php

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)