Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
GlossesChangeOpDeserializer
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setContext
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createEntityChangeOp
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Wikibase\Lexeme\Presentation\ChangeOp\Deserialization;
4
5use Wikibase\DataModel\Deserializers\TermDeserializer;
6use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpGloss;
7use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpGlossList;
8use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpRemoveSenseGloss;
9use Wikibase\Lexeme\DataAccess\ChangeOp\Validation\LexemeTermSerializationValidator;
10use Wikibase\Lib\StringNormalizer;
11use Wikibase\Repo\ChangeOp\ChangeOpDeserializer;
12
13/**
14 * @license GPL-2.0-or-later
15 */
16class GlossesChangeOpDeserializer implements ChangeOpDeserializer {
17
18    private const PARAM_LANGUAGE = 'language';
19    private const PARAM_VALUE = 'value';
20
21    /**
22     * @var TermDeserializer
23     */
24    private $glossDeserializer;
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 $glossDeserializer,
43        StringNormalizer $stringNormalizer,
44        LexemeTermSerializationValidator $validator
45    ) {
46        $this->glossDeserializer = $glossDeserializer;
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[] $glosses
57     * @return ChangeOpGlossList
58     */
59    public function createEntityChangeOp( array $glosses ) {
60        $changeOps = [];
61
62        foreach ( $glosses as $language => $gloss ) {
63            $languageContext = $this->validationContext->at( $language );
64            $this->termSerializationValidator->validateStructure( $gloss, $languageContext );
65
66            if ( array_key_exists( 'remove', $gloss ) ) {
67                $changeOps[] = new ChangeOpRemoveSenseGloss( $gloss[self::PARAM_LANGUAGE] );
68            } else {
69                $this->termSerializationValidator->validateLanguage(
70                    $language, $gloss, $languageContext );
71                $trimmedGloss = [
72                    self::PARAM_LANGUAGE => $gloss[self::PARAM_LANGUAGE],
73                    self::PARAM_VALUE => $this->stringNormalizer->trimToNFC( $gloss[self::PARAM_VALUE] ),
74                ];
75
76                $changeOps[] = new ChangeOpGloss(
77                    $this->glossDeserializer->deserialize( $trimmedGloss )
78                );
79            }
80        }
81
82        return new ChangeOpGlossList( $changeOps );
83    }
84
85}