-
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.
REST: Add property id filter to ValidatingRequestDeserializer
This also modifies the GetItemStatements use case and related classes to use the new validation. Bug: T345715 Change-Id: I6b9098cc12c17b4e92ceb04fbc59181081ba9718
- Loading branch information
1 parent
3133cb6
commit fee17c0
Showing
17 changed files
with
251 additions
and
133 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
repo/rest-api/src/Application/UseCases/DeserializedPropertyIdFilterRequest.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; | ||
|
||
use Wikibase\DataModel\Entity\PropertyId; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface DeserializedPropertyIdFilterRequest { | ||
public function getPropertyIdFilter(): ?PropertyId; | ||
} |
12 changes: 12 additions & 0 deletions
12
...t-api/src/Application/UseCases/GetItemStatements/DeserializedGetItemStatementsRequest.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\GetItemStatements; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCases\DeserializedItemIdRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCases\DeserializedPropertyIdFilterRequest; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface DeserializedGetItemStatementsRequest extends DeserializedItemIdRequest, DeserializedPropertyIdFilterRequest { | ||
} |
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
10 changes: 10 additions & 0 deletions
10
repo/rest-api/src/Application/UseCases/PropertyIdFilterRequest.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,10 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface PropertyIdFilterRequest { | ||
public function getPropertyIdFilter(): ?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
42 changes: 42 additions & 0 deletions
42
.../Application/UseCases/RequestValidation/PropertyIdFilterRequestValidatingDeserializer.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,42 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\RequestValidation; | ||
|
||
use Wikibase\DataModel\Entity\NumericPropertyId; | ||
use Wikibase\Repo\RestApi\Application\UseCases\PropertyIdFilterRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError; | ||
use Wikibase\Repo\RestApi\Application\Validation\PropertyIdValidator; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class PropertyIdFilterRequestValidatingDeserializer { | ||
public const DESERIALIZED_VALUE = 'property-id-filter'; | ||
private PropertyIdValidator $propertyIdValidator; | ||
|
||
public function __construct( PropertyIdValidator $validator ) { | ||
$this->propertyIdValidator = $validator; | ||
} | ||
|
||
/** | ||
* @throws UseCaseError | ||
*/ | ||
public function validateAndDeserialize( PropertyIdFilterRequest $request ): array { | ||
$filterPropertyId = $request->getPropertyIdFilter(); | ||
if ( $filterPropertyId === null ) { | ||
return [ self::DESERIALIZED_VALUE => null ]; | ||
} | ||
|
||
$validationError = $this->propertyIdValidator->validate( $filterPropertyId ); | ||
if ( $validationError ) { | ||
$context = $validationError->getContext(); | ||
throw new UseCaseError( | ||
UseCaseError::INVALID_PROPERTY_ID, | ||
"Not a valid property ID: {$context[PropertyIdValidator::CONTEXT_VALUE]}", | ||
[ UseCaseError::CONTEXT_PROPERTY_ID => $context[PropertyIdValidator::CONTEXT_VALUE] ] | ||
); | ||
} | ||
return [ self::DESERIALIZED_VALUE => new NumericPropertyId( $filterPropertyId ) ]; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.