Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
LexemeTermSerializationValidator | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
10 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validateStructure | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
7 | |||
validateLanguage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace Wikibase\Lexeme\DataAccess\ChangeOp\Validation; |
6 | |
7 | use Wikibase\Lexeme\MediaWiki\Api\Error\JsonFieldHasWrongType; |
8 | use Wikibase\Lexeme\MediaWiki\Api\Error\JsonFieldIsRequired; |
9 | use Wikibase\Lexeme\MediaWiki\Api\Error\LanguageInconsistent; |
10 | use Wikibase\Lexeme\MediaWiki\Api\Error\LexemeTermTextCanNotBeEmpty; |
11 | use Wikibase\Lexeme\Presentation\ChangeOp\Deserialization\ValidationContext; |
12 | |
13 | /** |
14 | * @license GPL-2.0-or-later |
15 | */ |
16 | class LexemeTermSerializationValidator { |
17 | |
18 | /** |
19 | * @var LexemeTermLanguageValidator |
20 | */ |
21 | private $languageValidator; |
22 | |
23 | public function __construct( LexemeTermLanguageValidator $languageValidator ) { |
24 | $this->languageValidator = $languageValidator; |
25 | } |
26 | |
27 | /** |
28 | * Validate the structure of the given $serialization. |
29 | * |
30 | * If the term is not being removed, |
31 | * callers should also call {@link LexemeTermSerializationValidator::validateLanguage()} |
32 | * afterwards. |
33 | * |
34 | * @param array $serialization (checking that it is an array is part of the validation) |
35 | * @param ValidationContext $context |
36 | */ |
37 | public function validateStructure( $serialization, ValidationContext $context ) { |
38 | if ( !is_array( $serialization ) ) { |
39 | $context->addViolation( new JsonFieldHasWrongType( 'array', gettype( $serialization ) ) ); |
40 | return; |
41 | } |
42 | |
43 | if ( !array_key_exists( 'language', $serialization ) ) { |
44 | $context->addViolation( new JsonFieldIsRequired( 'language' ) ); |
45 | return; |
46 | } |
47 | |
48 | if ( !array_key_exists( 'remove', $serialization ) ) { |
49 | if ( !array_key_exists( 'value', $serialization ) ) { |
50 | $context->addViolation( new JsonFieldIsRequired( 'value' ) ); |
51 | return; |
52 | } |
53 | |
54 | if ( !is_string( $serialization['value'] ) ) { |
55 | $context->addViolation( |
56 | new JsonFieldHasWrongType( 'string', gettype( $serialization['value'] ) ) |
57 | ); |
58 | } |
59 | |
60 | if ( $serialization['value'] === '' ) { |
61 | $context->addViolation( new LexemeTermTextCanNotBeEmpty() ); |
62 | } |
63 | } |
64 | } |
65 | |
66 | /** |
67 | * Check that the language inside the $serialization is valid |
68 | * and consistent with the given $language. |
69 | * |
70 | * The $serialization must already have been |
71 | * {@link LexemeTermSerializationValidator::validateStructure() validated for structural correctness}. |
72 | * |
73 | * @param string $language (checking that it is a string is part of the validation) |
74 | */ |
75 | public function validateLanguage( $language, array $serialization, ValidationContext $context ) { |
76 | $this->languageValidator->validate( $language, $context, $serialization['value'] ?? null ); |
77 | |
78 | if ( $language !== $serialization['language'] ) { |
79 | $context->addViolation( new LanguageInconsistent( $language, $serialization['language'] ) ); |
80 | } |
81 | } |
82 | |
83 | } |