-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge "REST: Use new validation mechanism in GetItemDescription"
- Loading branch information
Showing
9 changed files
with
46 additions
and
130 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
...api/src/Application/UseCases/GetItemDescription/DeserializedGetItemDescriptionRequest.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,12 @@ | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCases\DeserializedItemIdRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCases\DeserializedLanguageCodeRequest; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface DeserializedGetItemDescriptionRequest extends DeserializedItemIdRequest, DeserializedLanguageCodeRequest { | ||
} |
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
51 changes: 10 additions & 41 deletions
51
repo/rest-api/src/Application/UseCases/GetItemDescription/GetItemDescriptionValidator.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 |
---|---|---|
@@ -1,59 +1,28 @@ | ||
<?php declare( strict_types=1 ); | ||
<?php declare( strict_types = 1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\GetItemDescription; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCases\RequestValidation\DeserializedRequestAdapter; | ||
use Wikibase\Repo\RestApi\Application\UseCases\RequestValidation\ValidatingRequestDeserializer; | ||
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError; | ||
use Wikibase\Repo\RestApi\Application\Validation\ItemIdValidator; | ||
use Wikibase\Repo\RestApi\Application\Validation\LanguageCodeValidator; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class GetItemDescriptionValidator { | ||
|
||
private ItemIdValidator $itemIdValidator; | ||
private LanguageCodeValidator $languageCodeValidator; | ||
private ValidatingRequestDeserializer $requestDeserializer; | ||
|
||
public function __construct( | ||
ItemIdValidator $itemIdValidator, | ||
LanguageCodeValidator $languageCodeValidator | ||
) { | ||
$this->itemIdValidator = $itemIdValidator; | ||
$this->languageCodeValidator = $languageCodeValidator; | ||
public function __construct( ValidatingRequestDeserializer $requestDeserializer ) { | ||
$this->requestDeserializer = $requestDeserializer; | ||
} | ||
|
||
/** | ||
* @throws UseCaseError | ||
*/ | ||
public function assertValidRequest( GetItemDescriptionRequest $request ): void { | ||
$this->validateItemId( $request->getItemId() ); | ||
$this->validateLanguageCode( $request->getLanguageCode() ); | ||
public function validateAndDeserialize( GetItemDescriptionRequest $request ): DeserializedGetItemDescriptionRequest { | ||
return new class( $this->requestDeserializer->validateAndDeserialize( $request ) ) | ||
extends DeserializedRequestAdapter implements DeserializedGetItemDescriptionRequest { | ||
}; | ||
} | ||
|
||
/** | ||
* @throws UseCaseError | ||
*/ | ||
private function validateItemId( string $itemId ): void { | ||
$validationError = $this->itemIdValidator->validate( $itemId ); | ||
if ( $validationError ) { | ||
throw new UseCaseError( | ||
UseCaseError::INVALID_ITEM_ID, | ||
"Not a valid item ID: {$validationError->getContext()[ItemIdValidator::CONTEXT_VALUE]}" | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @throws UseCaseError | ||
*/ | ||
private function validateLanguageCode( string $languageCode ): void { | ||
$validationError = $this->languageCodeValidator->validate( $languageCode ); | ||
if ( $validationError ) { | ||
throw new UseCaseError( | ||
UseCaseError::INVALID_LANGUAGE_CODE, | ||
"Not a valid language code: {$validationError->getContext()[LanguageCodeValidator::CONTEXT_LANGUAGE_CODE_VALUE]}" | ||
); | ||
} | ||
} | ||
|
||
} |
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
60 changes: 0 additions & 60 deletions
60
...tests/phpunit/Application/UseCases/GetItemDescription/GetItemDescriptionValidatorTest.php
This file was deleted.
Oops, something went wrong.
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