Skip to content

Commit

Permalink
Merge "REST: Use term type specific validators"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Jun 18, 2024
2 parents 8a26b91 + c7acfe6 commit 1b8e276
Show file tree
Hide file tree
Showing 32 changed files with 197 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Wikibase\Repo\RestApi\Application\Serialization\Exceptions\InvalidFieldException;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;
use Wikibase\Repo\RestApi\Application\Validation\AliasesInLanguageValidator;
use Wikibase\Repo\RestApi\Application\Validation\LanguageCodeValidator;
use Wikibase\Repo\RestApi\Application\Validation\AliasLanguageCodeValidator;

/**
* @license GPL-2.0-or-later
Expand All @@ -18,12 +18,12 @@ class PatchedAliasesValidator {

private AliasesDeserializer $aliasesDeserializer;
private AliasesInLanguageValidator $aliasesInLanguageValidator;
private LanguageCodeValidator $languageCodeValidator;
private AliasLanguageCodeValidator $languageCodeValidator;

public function __construct(
AliasesDeserializer $aliasesDeserializer,
AliasesInLanguageValidator $aliasesInLanguageValidator,
LanguageCodeValidator $languageCodeValidator
AliasLanguageCodeValidator $languageCodeValidator
) {
$this->aliasesDeserializer = $aliasesDeserializer;
$this->aliasesInLanguageValidator = $aliasesInLanguageValidator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Wikibase\Repo\RestApi\Application\Serialization\Exceptions\InvalidFieldException;
use Wikibase\Repo\RestApi\Application\UseCases\UseCaseError;
use Wikibase\Repo\RestApi\Application\Validation\AliasesInLanguageValidator;
use Wikibase\Repo\RestApi\Application\Validation\LanguageCodeValidator;
use Wikibase\Repo\RestApi\Application\Validation\AliasLanguageCodeValidator;

/**
* @license GPL-2.0-or-later
Expand All @@ -18,12 +18,12 @@ class PatchedAliasesValidator {

private AliasesDeserializer $aliasesDeserializer;
private AliasesInLanguageValidator $aliasesInLanguageValidator;
private LanguageCodeValidator $languageCodeValidator;
private AliasLanguageCodeValidator $languageCodeValidator;

public function __construct(
AliasesDeserializer $aliasesDeserializer,
AliasesInLanguageValidator $aliasesInLanguageValidator,
LanguageCodeValidator $languageCodeValidator
AliasLanguageCodeValidator $languageCodeValidator
) {
$this->aliasesDeserializer = $aliasesDeserializer;
$this->aliasesInLanguageValidator = $aliasesInLanguageValidator;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\Validation;

/**
* @license GPL-2.0-or-later
*/
interface AliasLanguageCodeValidator extends LanguageCodeValidator {
}
4 changes: 2 additions & 2 deletions repo/rest-api/src/Application/Validation/AliasesValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ class AliasesValidator {
public const CONTEXT_LANGUAGE = 'aliases-validator-context-language';

private AliasesInLanguageValidator $aliasesInLanguageValidator;
private LanguageCodeValidator $languageCodeValidator;
private AliasLanguageCodeValidator $languageCodeValidator;
private AliasesDeserializer $aliasesDeserializer;

private ?AliasGroupList $deserializedAliases = null;

public function __construct(
AliasesInLanguageValidator $aliasesInLanguageValidator,
LanguageCodeValidator $languageCodeValidator,
AliasLanguageCodeValidator $languageCodeValidator,
AliasesDeserializer $aliasesDeserializer
) {
$this->aliasesInLanguageValidator = $aliasesInLanguageValidator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,5 @@
/**
* @license GPL-2.0-or-later
*/
interface DescriptionLanguageCodeValidator {

public const CODE_INVALID_LANGUAGE = 'description-language-code-validator-code-invalid-language-code';

public const CONTEXT_LANGUAGE = 'description-language-code-validator-context-language';

public function validate( string $language ): ?ValidationError;

interface DescriptionLanguageCodeValidator extends LanguageCodeValidator {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class DescriptionsSyntaxValidator {
public const CONTEXT_LANGUAGE = 'descriptions-syntax-validator-context-language';

private DescriptionsDeserializer $deserializer;
private LanguageCodeValidator $languageCodeValidator;
private DescriptionLanguageCodeValidator $languageCodeValidator;
private PartiallyValidatedDescriptions $deserializedDescriptions;

public function __construct( DescriptionsDeserializer $deserializer, LanguageCodeValidator $languageCodeValidator ) {
public function __construct( DescriptionsDeserializer $deserializer, DescriptionLanguageCodeValidator $languageCodeValidator ) {
$this->deserializer = $deserializer;
$this->languageCodeValidator = $languageCodeValidator;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Application\Validation;

/**
* @license GPL-2.0-or-later
*/
interface LabelLanguageCodeValidator extends LanguageCodeValidator {
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class LabelsSyntaxValidator {
public const CONTEXT_LANGUAGE = 'labels-syntax-validator-context-language';

private LabelsDeserializer $deserializer;
private LanguageCodeValidator $languageCodeValidator;
private LabelLanguageCodeValidator $languageCodeValidator;
private PartiallyValidatedLabels $deserializedLabels;

public function __construct( LabelsDeserializer $deserializer, LanguageCodeValidator $languageCodeValidator ) {
public function __construct( LabelsDeserializer $deserializer, LabelLanguageCodeValidator $languageCodeValidator ) {
$this->deserializer = $deserializer;
$this->languageCodeValidator = $languageCodeValidator;
}
Expand Down
17 changes: 2 additions & 15 deletions repo/rest-api/src/Application/Validation/LanguageCodeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,13 @@
/**
* @license GPL-2.0-or-later
*/
class LanguageCodeValidator {
interface LanguageCodeValidator {

public const CODE_INVALID_LANGUAGE_CODE = 'language-code-validator-code-invalid-language-code';

public const CONTEXT_LANGUAGE_CODE = 'language-code-validator-context-language-code';
public const CONTEXT_PATH = 'language-code-validator-context-path';

private array $validLanguageCodes;

public function __construct( array $validLanguageCodes ) {
$this->validLanguageCodes = $validLanguageCodes;
}

public function validate( string $languageCode ): ?ValidationError {
return in_array( $languageCode, $this->validLanguageCodes )
? null
: new ValidationError(
self::CODE_INVALID_LANGUAGE_CODE,
[ self::CONTEXT_LANGUAGE_CODE => $languageCode ]
);
}
public function validate( string $languageCode ): ?ValidationError;

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare( strict_types=1 );

namespace Wikibase\Repo\RestApi\Infrastructure;

use ValueValidators\ValueValidator;
use Wikibase\Repo\RestApi\Application\Validation\AliasLanguageCodeValidator;
use Wikibase\Repo\RestApi\Application\Validation\DescriptionLanguageCodeValidator;
use Wikibase\Repo\RestApi\Application\Validation\LabelLanguageCodeValidator;
use Wikibase\Repo\RestApi\Application\Validation\ValidationError;

/**
* @license GPL-2.0-or-later
*/
class ValueValidatorLanguageCodeValidator
implements LabelLanguageCodeValidator, DescriptionLanguageCodeValidator, AliasLanguageCodeValidator {

private ValueValidator $validator;

public function __construct( ValueValidator $validator ) {
$this->validator = $validator;
}

public function validate( string $language ): ?ValidationError {
$result = $this->validator->validate( $language );
if ( !$result->isValid() ) {
return new ValidationError(
self::CODE_INVALID_LANGUAGE_CODE,
[ self::CONTEXT_LANGUAGE_CODE => $language ]
);
}

return null;
}

}
Loading

0 comments on commit 1b8e276

Please sign in to comment.