-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add update relationship command
- Loading branch information
1 parent
99a681d
commit 16f8179
Showing
22 changed files
with
2,007 additions
and
9 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/Contracts/Http/Hooks/UpdateRelationshipImplementation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/* | ||
* Copyright 2023 Cloud Creativity Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelJsonApi\Contracts\Http\Hooks; | ||
|
||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
use Illuminate\Http\Request; | ||
use LaravelJsonApi\Contracts\Query\QueryParameters; | ||
|
||
interface UpdateRelationshipImplementation | ||
{ | ||
/** | ||
* @param object $model | ||
* @param string $fieldName | ||
* @param Request $request | ||
* @param QueryParameters $query | ||
* @return void | ||
* @throws HttpResponseException | ||
*/ | ||
public function updatingRelationship( | ||
object $model, | ||
string $fieldName, | ||
Request $request, | ||
QueryParameters $query, | ||
): void; | ||
|
||
/** | ||
* @param object $model | ||
* @param string $fieldName | ||
* @param mixed $related | ||
* @param Request $request | ||
* @param QueryParameters $query | ||
* @return void | ||
* @throws HttpResponseException | ||
*/ | ||
public function updatedRelationship( | ||
object $model, | ||
string $fieldName, | ||
mixed $related, | ||
Request $request, | ||
QueryParameters $query, | ||
): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/* | ||
* Copyright 2023 Cloud Creativity Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelJsonApi\Contracts\Validation; | ||
|
||
use Illuminate\Contracts\Validation\Validator; | ||
use Illuminate\Http\Request; | ||
use LaravelJsonApi\Core\Extensions\Atomic\Operations\UpdateToMany; | ||
use LaravelJsonApi\Core\Extensions\Atomic\Operations\UpdateToOne; | ||
|
||
interface RelationshipValidator | ||
{ | ||
/** | ||
* Extract validation data from the update relationship operation. | ||
* | ||
* @param object $model | ||
* @param UpdateToOne|UpdateToMany $operation | ||
* @return array | ||
*/ | ||
public function extract(object $model, UpdateToOne|UpdateToMany $operation): array; | ||
|
||
/** | ||
* Make a validator for the update relationship operation. | ||
* | ||
* @param Request|null $request | ||
* @param object $model | ||
* @param UpdateToOne|UpdateToMany $operation | ||
* @return Validator | ||
*/ | ||
public function make(?Request $request, object $model, UpdateToOne|UpdateToMany $operation): Validator; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/* | ||
* Copyright 2023 Cloud Creativity Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelJsonApi\Core\Bus\Commands\Command; | ||
|
||
interface IsRelatable extends IsIdentifiable | ||
{ | ||
/** | ||
* Get the JSON:API field name for the relationship the command is targeting. | ||
* | ||
* @return string | ||
*/ | ||
public function fieldName(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/Core/Bus/Commands/Middleware/ValidateRelationshipCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/* | ||
* Copyright 2023 Cloud Creativity Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelJsonApi\Core\Bus\Commands\Middleware; | ||
|
||
use Closure; | ||
use LaravelJsonApi\Contracts\Schema\Container as SchemaContainer; | ||
use LaravelJsonApi\Contracts\Validation\Container as ValidatorContainer; | ||
use LaravelJsonApi\Contracts\Validation\RelationshipValidator; | ||
use LaravelJsonApi\Contracts\Validation\ResourceErrorFactory; | ||
use LaravelJsonApi\Core\Bus\Commands\Result; | ||
use LaravelJsonApi\Core\Bus\Commands\UpdateRelationship\HandlesUpdateRelationshipCommands; | ||
use LaravelJsonApi\Core\Bus\Commands\UpdateRelationship\UpdateRelationshipCommand; | ||
use LaravelJsonApi\Core\Document\Input\Values\ResourceType; | ||
|
||
class ValidateRelationshipCommand implements HandlesUpdateRelationshipCommands | ||
{ | ||
/** | ||
* ValidateRelationshipCommand constructor | ||
* | ||
* @param ValidatorContainer $validatorContainer | ||
* @param SchemaContainer $schemaContainer | ||
* @param ResourceErrorFactory $errorFactory | ||
*/ | ||
public function __construct( | ||
private readonly ValidatorContainer $validatorContainer, | ||
private readonly SchemaContainer $schemaContainer, | ||
private readonly ResourceErrorFactory $errorFactory, | ||
) { | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function handle(UpdateRelationshipCommand $command, Closure $next): Result | ||
{ | ||
if ($command->mustValidate()) { | ||
$validator = $this | ||
->validatorFor($command->type()) | ||
->make($command->request(), $command->modelOrFail(), $command->operation()); | ||
|
||
if ($validator->fails()) { | ||
return Result::failed( | ||
$this->errorFactory->make( | ||
$this->schemaContainer->schemaFor($command->type()), | ||
$validator, | ||
), | ||
); | ||
} | ||
|
||
$command = $command->withValidated( | ||
$validator->validated(), | ||
); | ||
} | ||
|
||
if ($command->isNotValidated()) { | ||
$data = $this | ||
->validatorFor($command->type()) | ||
->extract($command->modelOrFail(), $command->operation()); | ||
|
||
$command = $command->withValidated($data); | ||
} | ||
|
||
return $next($command); | ||
} | ||
|
||
/** | ||
* Make an update relationship validator. | ||
* | ||
* @param ResourceType $type | ||
* @return RelationshipValidator | ||
*/ | ||
private function validatorFor(ResourceType $type): RelationshipValidator | ||
{ | ||
return $this->validatorContainer | ||
->validatorsFor($type) | ||
->relation(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Core/Bus/Commands/UpdateRelationship/HandlesUpdateRelationshipCommands.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/* | ||
* Copyright 2023 Cloud Creativity Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaravelJsonApi\Core\Bus\Commands\UpdateRelationship; | ||
|
||
use Closure; | ||
use LaravelJsonApi\Core\Bus\Commands\Result; | ||
|
||
interface HandlesUpdateRelationshipCommands | ||
{ | ||
/** | ||
* @param UpdateRelationshipCommand $command | ||
* @param Closure $next | ||
* @return Result | ||
*/ | ||
public function handle(UpdateRelationshipCommand $command, Closure $next): Result; | ||
} |
Oops, something went wrong.