-
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: Create SetPropertyLabel happy path"
- Loading branch information
Showing
23 changed files
with
829 additions
and
11 deletions.
There are no files selected for viewing
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
12 changes: 12 additions & 0 deletions
12
...est-api/src/Application/UseCaseRequestValidation/DeserializedPropertyLabelEditRequest.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\UseCaseRequestValidation; | ||
|
||
use Wikibase\DataModel\Term\Term; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface DeserializedPropertyLabelEditRequest extends DeserializedPropertyIdRequest { | ||
public function getPropertyLabel(): Term; | ||
} |
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/UseCaseRequestValidation/PropertyLabelEditRequest.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\UseCaseRequestValidation; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface PropertyLabelEditRequest extends PropertyIdRequest, LanguageCodeRequest { | ||
public function getLabel(): string; | ||
} |
17 changes: 17 additions & 0 deletions
17
...c/Application/UseCaseRequestValidation/PropertyLabelEditRequestValidatingDeserializer.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,17 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCaseRequestValidation; | ||
|
||
use Wikibase\DataModel\Term\Term; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class PropertyLabelEditRequestValidatingDeserializer { | ||
|
||
public function validateAndDeserialize( PropertyLabelEditRequest $request ): Term { | ||
// TODO: validation | ||
return new Term( $request->getLanguageCode(), $request->getLabel() ); | ||
} | ||
|
||
} |
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
12 changes: 12 additions & 0 deletions
12
...est-api/src/Application/UseCases/SetPropertyLabel/DeserializedSetPropertyLabelRequest.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\SetPropertyLabel; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DeserializedEditMetadataRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\DeserializedPropertyLabelEditRequest; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface DeserializedSetPropertyLabelRequest extends DeserializedPropertyLabelEditRequest, DeserializedEditMetadataRequest { | ||
} |
56 changes: 56 additions & 0 deletions
56
repo/rest-api/src/Application/UseCases/SetPropertyLabel/SetPropertyLabel.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,56 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\SetPropertyLabel; | ||
|
||
use Wikibase\Repo\RestApi\Domain\Model\EditMetadata; | ||
use Wikibase\Repo\RestApi\Domain\Model\LabelEditSummary; | ||
use Wikibase\Repo\RestApi\Domain\Services\PropertyRetriever; | ||
use Wikibase\Repo\RestApi\Domain\Services\PropertyUpdater; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class SetPropertyLabel { | ||
|
||
private PropertyRetriever $propertyRetriever; | ||
private PropertyUpdater $propertyUpdater; | ||
private SetPropertyLabelValidator $validator; | ||
|
||
public function __construct( | ||
SetPropertyLabelValidator $validator, | ||
PropertyRetriever $propertyRetriever, | ||
PropertyUpdater $propertyUpdater | ||
) { | ||
$this->validator = $validator; | ||
$this->propertyRetriever = $propertyRetriever; | ||
$this->propertyUpdater = $propertyUpdater; | ||
} | ||
|
||
public function execute( SetPropertyLabelRequest $request ): SetPropertyLabelResponse { | ||
$deserializedRequest = $this->validator->validateAndDeserialize( $request ); | ||
$propertyId = $deserializedRequest->getPropertyId(); | ||
$label = $deserializedRequest->getPropertyLabel(); | ||
$editMetadata = $deserializedRequest->getEditMetadata(); | ||
|
||
$property = $this->propertyRetriever->getProperty( $propertyId ); | ||
$labelExists = $property->getLabels()->hasTermForLanguage( $label->getLanguageCode() ); | ||
$property->getLabels()->setTerm( $label ); | ||
|
||
$editSummary = $labelExists | ||
? LabelEditSummary::newReplaceSummary( $editMetadata->getComment(), $label ) | ||
: LabelEditSummary::newAddSummary( $editMetadata->getComment(), $label ); | ||
|
||
$newRevision = $this->propertyUpdater->update( | ||
$property, // @phan-suppress-current-line PhanTypeMismatchArgumentNullable | ||
new EditMetadata( $editMetadata->getTags(), $editMetadata->isBot(), $editSummary ) | ||
); | ||
|
||
return new SetPropertyLabelResponse( | ||
$newRevision->getProperty()->getLabels()[$label->getLanguageCode()], | ||
$newRevision->getLastModified(), | ||
$newRevision->getRevisionId(), | ||
$labelExists | ||
); | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
repo/rest-api/src/Application/UseCases/SetPropertyLabel/SetPropertyLabelRequest.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,68 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\SetPropertyLabel; | ||
|
||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\EditMetadataRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\PropertyLabelEditRequest; | ||
use Wikibase\Repo\RestApi\Application\UseCaseRequestValidation\UseCaseRequest; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class SetPropertyLabelRequest implements UseCaseRequest, PropertyLabelEditRequest, EditMetadataRequest { | ||
|
||
private string $propertyId; | ||
private string $languageCode; | ||
private string $label; | ||
private array $editTags; | ||
private bool $isBot; | ||
private ?string $comment; | ||
private ?string $username; | ||
|
||
public function __construct( | ||
string $propertyId, | ||
string $languageCode, | ||
string $label, | ||
array $editTags, | ||
bool $isBot, | ||
?string $comment, | ||
?string $username | ||
) { | ||
$this->propertyId = $propertyId; | ||
$this->languageCode = $languageCode; | ||
$this->label = trim( $label ); | ||
$this->editTags = $editTags; | ||
$this->isBot = $isBot; | ||
$this->comment = $comment; | ||
$this->username = $username; | ||
} | ||
|
||
public function getPropertyId(): string { | ||
return $this->propertyId; | ||
} | ||
|
||
public function getLanguageCode(): string { | ||
return $this->languageCode; | ||
} | ||
|
||
public function getLabel(): string { | ||
return $this->label; | ||
} | ||
|
||
public function getEditTags(): array { | ||
return $this->editTags; | ||
} | ||
|
||
public function isBot(): bool { | ||
return $this->isBot; | ||
} | ||
|
||
public function getComment(): ?string { | ||
return $this->comment; | ||
} | ||
|
||
public function getUsername(): ?string { | ||
return $this->username; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
repo/rest-api/src/Application/UseCases/SetPropertyLabel/SetPropertyLabelResponse.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,40 @@ | ||
<?php declare( strict_types=1 ); | ||
|
||
namespace Wikibase\Repo\RestApi\Application\UseCases\SetPropertyLabel; | ||
|
||
use Wikibase\Repo\RestApi\Domain\ReadModel\Label; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
class SetPropertyLabelResponse { | ||
|
||
private Label $label; | ||
private string $lastModified; | ||
private int $revisionId; | ||
private bool $replaced; | ||
|
||
public function __construct( Label $label, string $lastModified, int $revisionId, bool $replaced ) { | ||
$this->label = $label; | ||
$this->lastModified = $lastModified; | ||
$this->revisionId = $revisionId; | ||
$this->replaced = $replaced; | ||
} | ||
|
||
public function getLabel(): Label { | ||
return $this->label; | ||
} | ||
|
||
public function getLastModified(): string { | ||
return $this->lastModified; | ||
} | ||
|
||
public function getRevisionId(): int { | ||
return $this->revisionId; | ||
} | ||
|
||
public function wasReplaced(): bool { | ||
return $this->replaced; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
repo/rest-api/src/Application/UseCases/SetPropertyLabel/SetPropertyLabelValidator.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\SetPropertyLabel; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
*/ | ||
interface SetPropertyLabelValidator { | ||
|
||
public function validateAndDeserialize( SetPropertyLabelRequest $request ): DeserializedSetPropertyLabelRequest; | ||
|
||
} |
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.