-
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.
- Loading branch information
1 parent
22e2d8b
commit cd26c1a
Showing
29 changed files
with
2,092 additions
and
55 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/Contracts/Http/Controllers/Hooks/UpdateImplementation.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,45 @@ | ||
<?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\Controllers\Hooks; | ||
|
||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
use Illuminate\Http\Request; | ||
use LaravelJsonApi\Contracts\Query\QueryParameters; | ||
|
||
interface UpdateImplementation extends SaveImplementation | ||
{ | ||
/** | ||
* @param object $model | ||
* @param Request $request | ||
* @param QueryParameters $query | ||
* @return void | ||
* @throws HttpResponseException | ||
*/ | ||
public function updating(object $model, Request $request, QueryParameters $query): void; | ||
|
||
/** | ||
* @param object $model | ||
* @param Request $request | ||
* @param QueryParameters $query | ||
* @return void | ||
* @throws HttpResponseException | ||
*/ | ||
public function updated(object $model, 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,46 @@ | ||
<?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\Update; | ||
|
||
interface UpdateValidator | ||
{ | ||
/** | ||
* Extract validation data from the update operation. | ||
* | ||
* @param object $model | ||
* @param Update $operation | ||
* @return array | ||
*/ | ||
public function extract(object $model, Update $operation): array; | ||
|
||
/** | ||
* Make a validator for the store operation. | ||
* | ||
* @param Request|null $request | ||
* @param object $model | ||
* @param Update $operation | ||
* @return Validator | ||
*/ | ||
public function make(?Request $request, object $model, Update $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,68 @@ | ||
<?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\Concerns; | ||
|
||
use RuntimeException; | ||
|
||
trait Identifiable | ||
{ | ||
/** | ||
* @var object|null | ||
*/ | ||
private ?object $model = null; | ||
|
||
/** | ||
* Return a new instance with the model set, if known. | ||
* | ||
* @param object|null $model | ||
* @return static | ||
*/ | ||
public function withModel(?object $model): static | ||
{ | ||
$copy = clone $this; | ||
$copy->model = $model; | ||
|
||
return $copy; | ||
} | ||
|
||
/** | ||
* Get the model for the query. | ||
* | ||
* @return object|null | ||
*/ | ||
public function model(): ?object | ||
{ | ||
return $this->model; | ||
} | ||
|
||
/** | ||
* Get the model for the query. | ||
* | ||
* @return object | ||
*/ | ||
public function modelOrFail(): object | ||
{ | ||
if ($this->model !== null) { | ||
return $this->model; | ||
} | ||
|
||
throw new RuntimeException('Expecting a model to be set on the query.'); | ||
} | ||
} |
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,54 @@ | ||
<?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; | ||
|
||
use LaravelJsonApi\Core\Document\Input\Values\ResourceId; | ||
|
||
interface IsIdentifiable | ||
{ | ||
/** | ||
* Get the resource id for the command. | ||
* | ||
* @return ResourceId | ||
*/ | ||
public function id(): ResourceId; | ||
|
||
/** | ||
* Get the model for the command, if there is one. | ||
* | ||
* @return object|null | ||
*/ | ||
public function model(): ?object; | ||
|
||
/** | ||
* Get the model for the command, or fail if there isn't one. | ||
* | ||
* @return object | ||
*/ | ||
public function modelOrFail(): object; | ||
|
||
/** | ||
* Return a new instance with the model set. | ||
* | ||
* @param object|null $model | ||
* @return static | ||
*/ | ||
public function withModel(?object $model): static; | ||
} |
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,67 @@ | ||
<?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\Store\Store; | ||
use LaravelJsonApi\Core\Bus\Commands\Command; | ||
use LaravelJsonApi\Core\Bus\Commands\IsIdentifiable; | ||
use LaravelJsonApi\Core\Bus\Commands\Result; | ||
use LaravelJsonApi\Core\Document\Error; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class LookupModelIfMissing | ||
{ | ||
/** | ||
* LookupModelIfMissing constructor | ||
* | ||
* @param Store $store | ||
*/ | ||
public function __construct(private readonly Store $store) | ||
{ | ||
} | ||
|
||
/** | ||
* Handle an identifiable command. | ||
* | ||
* @param IsIdentifiable&Command $command | ||
* @param Closure $next | ||
* @return Result | ||
*/ | ||
public function handle(Command&IsIdentifiable $command, Closure $next): Result | ||
{ | ||
if ($command->model() === null) { | ||
$model = $this->store->find( | ||
$command->type(), | ||
$command->id(), | ||
); | ||
|
||
if ($model === null) { | ||
return Result::failed( | ||
Error::make()->setStatus(Response::HTTP_NOT_FOUND) | ||
); | ||
} | ||
|
||
$command = $command->withModel($model); | ||
} | ||
|
||
return $next($command); | ||
} | ||
} |
Oops, something went wrong.