Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
32 / 32 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
LemmaChangeOpDeserializer | |
100.00% |
32 / 32 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
createEntityChangeOp | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
4 | |||
assertIsArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\Presentation\ChangeOp\Deserialization; |
4 | |
5 | use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpLemmaEdit; |
6 | use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpLemmaRemove; |
7 | use Wikibase\Lexeme\DataAccess\ChangeOp\Validation\LemmaTermValidator; |
8 | use Wikibase\Lexeme\DataAccess\ChangeOp\Validation\LexemeTermSerializationValidator; |
9 | use Wikibase\Lib\StringNormalizer; |
10 | use Wikibase\Repo\ChangeOp\ChangeOp; |
11 | use Wikibase\Repo\ChangeOp\ChangeOpDeserializer; |
12 | use Wikibase\Repo\ChangeOp\ChangeOps; |
13 | use Wikibase\Repo\ChangeOp\Deserialization\ChangeOpDeserializationException; |
14 | |
15 | /** |
16 | * Deserializer for lemma change request data. |
17 | * |
18 | * @see docs/change-op-serialization.wiki for a description of the serialization format. |
19 | * |
20 | * @license GPL-2.0-or-later |
21 | */ |
22 | class LemmaChangeOpDeserializer implements ChangeOpDeserializer { |
23 | |
24 | /** |
25 | * @var \Wikibase\Lexeme\DataAccess\ChangeOp\Validation\LemmaTermValidator |
26 | */ |
27 | private $lemmaTermValidator; |
28 | |
29 | /** |
30 | * @var StringNormalizer |
31 | */ |
32 | private $stringNormalizer; |
33 | |
34 | /** |
35 | * @var LexemeTermSerializationValidator |
36 | */ |
37 | private $termSerializationValidator; |
38 | |
39 | private const LEMMAS_PARAM = 'lemmas'; |
40 | |
41 | public function __construct( |
42 | LexemeTermSerializationValidator $termChangeOpSerializationValidator, |
43 | LemmaTermValidator $lemmaTermValidator, |
44 | StringNormalizer $stringNormalizer |
45 | ) { |
46 | $this->termSerializationValidator = $termChangeOpSerializationValidator; |
47 | $this->lemmaTermValidator = $lemmaTermValidator; |
48 | $this->stringNormalizer = $stringNormalizer; |
49 | } |
50 | |
51 | /** |
52 | * @see ChangeOpDeserializer::createEntityChangeOp |
53 | * |
54 | * @param array $changeRequest |
55 | * |
56 | * @throws ChangeOpDeserializationException |
57 | * |
58 | * @return ChangeOp |
59 | */ |
60 | public function createEntityChangeOp( array $changeRequest ) { |
61 | $this->assertIsArray( $changeRequest[self::LEMMAS_PARAM] ); |
62 | |
63 | $changeOps = new ChangeOps(); |
64 | |
65 | $validationContext = ValidationContext::create( self::LEMMAS_PARAM ); |
66 | foreach ( $changeRequest[self::LEMMAS_PARAM] as $languageCode => $serialization ) { |
67 | $languageContext = $validationContext->at( $languageCode ); |
68 | |
69 | $this->termSerializationValidator->validateStructure( |
70 | $serialization, |
71 | $languageContext |
72 | ); |
73 | |
74 | $lemmaTerm = array_key_exists( 'remove', $serialization ) ? '' : |
75 | $this->stringNormalizer->trimToNFC( $serialization['value'] ); |
76 | |
77 | if ( $lemmaTerm === '' ) { |
78 | $changeOps->add( new ChangeOpLemmaRemove( $serialization['language'] ) ); |
79 | continue; |
80 | } |
81 | |
82 | $this->termSerializationValidator->validateLanguage( |
83 | $languageCode, |
84 | $serialization, |
85 | $languageContext |
86 | ); |
87 | |
88 | // TODO: maybe move creating ChangeOpLemmaEdit instance to some kind of factory? |
89 | $changeOps->add( new ChangeOpLemmaEdit( |
90 | $serialization['language'], |
91 | $lemmaTerm, |
92 | $this->lemmaTermValidator |
93 | ) ); |
94 | } |
95 | |
96 | return $changeOps; |
97 | } |
98 | |
99 | private function assertIsArray( $lemmaSerialization ) { |
100 | if ( !is_array( $lemmaSerialization ) ) { |
101 | throw new ChangeOpDeserializationException( |
102 | 'List of lemmas must be an array', 'not-recognized-array' |
103 | ); |
104 | } |
105 | } |
106 | |
107 | } |