diff --git a/src/Jarischaefer/HalApi/Controllers/HalApiResourceController.php b/src/Jarischaefer/HalApi/Controllers/HalApiResourceController.php index db6d611..66c2a56 100644 --- a/src/Jarischaefer/HalApi/Controllers/HalApiResourceController.php +++ b/src/Jarischaefer/HalApi/Controllers/HalApiResourceController.php @@ -41,7 +41,7 @@ abstract class HalApiResourceController extends HalApiController implements HalA /** * The model's transformer. * - * @var HalApiTransformer + * @var HalApiTransformerContract */ protected $transformer; /** @@ -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); @@ -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); } @@ -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; }; } @@ -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: diff --git a/src/Jarischaefer/HalApi/Controllers/HalApiResourceControllerContract.php b/src/Jarischaefer/HalApi/Controllers/HalApiResourceControllerContract.php index 9f8cb3c..ec58dd7 100644 --- a/src/Jarischaefer/HalApi/Controllers/HalApiResourceControllerContract.php +++ b/src/Jarischaefer/HalApi/Controllers/HalApiResourceControllerContract.php @@ -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; /** @@ -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) @@ -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(); @@ -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 diff --git a/src/Jarischaefer/HalApi/Providers/HalApiServiceProvider.php b/src/Jarischaefer/HalApi/Providers/HalApiServiceProvider.php index b5c8ca0..b8412b9 100644 --- a/src/Jarischaefer/HalApi/Providers/HalApiServiceProvider.php +++ b/src/Jarischaefer/HalApi/Providers/HalApiServiceProvider.php @@ -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 @@ -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', ]; /** @@ -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)); diff --git a/src/Jarischaefer/HalApi/Tests/Transformers/TransformerFactoryImplTest.php b/src/Jarischaefer/HalApi/Tests/Transformers/TransformerFactoryImplTest.php deleted file mode 100644 index 57e6345..0000000 --- a/src/Jarischaefer/HalApi/Tests/Transformers/TransformerFactoryImplTest.php +++ /dev/null @@ -1,64 +0,0 @@ -app->make(UrlGenerator::class); - $representationFactory = $this->app->make(RepresentationFactory::class); - /** @var LinkFactory $linkFactory */ - $linkFactory = Mockery::mock(LinkFactory::class); - /** @var Application $applicationMock */ - $applicationMock = Mockery::mock($this->app); - - $routeHelper = $this->createRouteHelper(); - - $applicationMock->shouldReceive('make') - ->withArgs(['url']) - ->andReturn($urlGenerator); - $expected = new TestingTransformer($linkFactory, $representationFactory, $routeHelper, $self, $parent); - $applicationMock->shouldReceive('make') - ->withArgs([TestingTransformer::class, [$linkFactory, $representationFactory, $routeHelper, $self, $parent, 123]]) - ->andReturn($expected); - - $factory = new TransformerFactoryImpl($applicationMock, $linkFactory, $representationFactory, $routeHelper); - $created = $factory->create(TestingTransformer::class, $self, $parent, [123]); - - $this->assertEquals($expected, $created); - } - -} diff --git a/src/Jarischaefer/HalApi/Transformers/TransformerFactory.php b/src/Jarischaefer/HalApi/Transformers/TransformerFactory.php deleted file mode 100644 index 2d43245..0000000 --- a/src/Jarischaefer/HalApi/Transformers/TransformerFactory.php +++ /dev/null @@ -1,21 +0,0 @@ -application = $application; - $this->linkFactory = $linkFactory; - $this->representationFactory = $representationFactory; - $this->routeHelper = $routeHelper; - } - - /** - * @inheritdoc - */ - public function create($class, Route $self, Route $parent, array $arguments = []) - { - if (!is_subclass_of($class, HalApiTransformerContract::class)) { - throw new InvalidArgumentException('Class must implement ' . HalApiTransformerContract::class); - } - - $parameters = array_merge([ - $this->linkFactory, - $this->representationFactory, - $this->routeHelper, - $self, - $parent, - ], $arguments); - - return $this->application->make($class, $parameters); - } - -}