Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
RepresentationsChangeOpDeserializer | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
setContext | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createEntityChangeOp | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\Presentation\ChangeOp\Deserialization; |
4 | |
5 | use Wikibase\DataModel\Deserializers\TermDeserializer; |
6 | use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpRemoveFormRepresentation; |
7 | use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpRepresentation; |
8 | use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpRepresentationList; |
9 | use Wikibase\Lexeme\DataAccess\ChangeOp\Validation\LexemeTermSerializationValidator; |
10 | use Wikibase\Lib\StringNormalizer; |
11 | use Wikibase\Repo\ChangeOp\ChangeOpDeserializer; |
12 | |
13 | /** |
14 | * @license GPL-2.0-or-later |
15 | */ |
16 | class RepresentationsChangeOpDeserializer implements ChangeOpDeserializer { |
17 | |
18 | private const PARAM_LANGUAGE = 'language'; |
19 | private const PARAM_VALUE = 'value'; |
20 | |
21 | /** |
22 | * @var TermDeserializer |
23 | */ |
24 | private $representationDeserializer; |
25 | |
26 | /** |
27 | * @var StringNormalizer |
28 | */ |
29 | private $stringNormalizer; |
30 | |
31 | /** |
32 | * @var ValidationContext |
33 | */ |
34 | private $validationContext; |
35 | |
36 | /** |
37 | * @var LexemeTermSerializationValidator |
38 | */ |
39 | private $termSerializationValidator; |
40 | |
41 | public function __construct( |
42 | TermDeserializer $representationDeserializer, |
43 | StringNormalizer $stringNormalizer, |
44 | LexemeTermSerializationValidator $validator |
45 | ) { |
46 | $this->representationDeserializer = $representationDeserializer; |
47 | $this->stringNormalizer = $stringNormalizer; |
48 | $this->termSerializationValidator = $validator; |
49 | } |
50 | |
51 | public function setContext( ValidationContext $context ) { |
52 | $this->validationContext = $context; |
53 | } |
54 | |
55 | /** |
56 | * @param array[] $representations |
57 | * @return ChangeOpRepresentationList |
58 | */ |
59 | public function createEntityChangeOp( array $representations ) { |
60 | $changeOps = []; |
61 | |
62 | foreach ( $representations as $language => $representation ) { |
63 | $languageContext = $this->validationContext->at( $language ); |
64 | $this->termSerializationValidator->validateStructure( $representation, $languageContext ); |
65 | |
66 | if ( array_key_exists( 'remove', $representation ) ) { |
67 | $changeOps[] = new ChangeOpRemoveFormRepresentation( $representation[self::PARAM_LANGUAGE] ); |
68 | } else { |
69 | $this->termSerializationValidator->validateLanguage( |
70 | $language, $representation, $languageContext ); |
71 | $trimmedRepresentation = [ |
72 | self::PARAM_LANGUAGE => $representation[self::PARAM_LANGUAGE], |
73 | self::PARAM_VALUE => $this->stringNormalizer->trimToNFC( $representation[self::PARAM_VALUE] ), |
74 | ]; |
75 | |
76 | $changeOps[] = new ChangeOpRepresentation( |
77 | $this->representationDeserializer->deserialize( $trimmedRepresentation ) |
78 | ); |
79 | } |
80 | } |
81 | |
82 | return new ChangeOpRepresentationList( $changeOps ); |
83 | } |
84 | |
85 | } |