Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
LanguageChangeOpDeserializer
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 createEntityChangeOp
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 validateItemId
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Wikibase\Lexeme\Presentation\ChangeOp\Deserialization;
4
5use InvalidArgumentException;
6use ValueValidators\ValueValidator;
7use Wikibase\DataModel\Entity\ItemId;
8use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpLanguage;
9use Wikibase\Lib\StringNormalizer;
10use Wikibase\Repo\ChangeOp\ChangeOp;
11use Wikibase\Repo\ChangeOp\ChangeOpDeserializer;
12use Wikibase\Repo\ChangeOp\Deserialization\ChangeOpDeserializationException;
13
14/**
15 * Deserializer for language change request data.
16 *
17 * @see docs/change-op-serialization.wiki for a description of the serialization format.
18 *
19 * @license GPL-2.0-or-later
20 */
21class LanguageChangeOpDeserializer implements ChangeOpDeserializer {
22
23    private ValueValidator $languageValidator;
24    private StringNormalizer $stringNormalizer;
25
26    public function __construct(
27        ValueValidator $languageValidator,
28        StringNormalizer $stringNormalizer
29    ) {
30        $this->languageValidator = $languageValidator;
31        $this->stringNormalizer = $stringNormalizer;
32    }
33
34    /**
35     * @see ChangeOpDeserializer::createEntityChangeOp
36     *
37     * @param array $changeRequest
38     *
39     * @throws ChangeOpDeserializationException
40     * @return ChangeOp
41     */
42    public function createEntityChangeOp( array $changeRequest ) {
43        if ( !array_key_exists( 'language', $changeRequest )
44            || !is_string( $changeRequest['language'] )
45        ) {
46            throw new ChangeOpDeserializationException(
47                'language must be a string',
48                'invalid-language'
49            );
50        }
51
52        $value = $this->stringNormalizer->cleanupToNFC( $changeRequest['language'] );
53
54        $itemId = $this->validateItemId( $value );
55        // TODO: maybe move creating ChangeOpLanguage instance to some kind of factory?
56        return new ChangeOpLanguage( $itemId, $this->languageValidator );
57    }
58
59    /**
60     * @param string $idSerialization
61     *
62     * @return ItemId
63     * @throws ChangeOpDeserializationException
64     */
65    private function validateItemId( $idSerialization ) {
66        try {
67            return new ItemId( $idSerialization );
68        } catch ( InvalidArgumentException $e ) {
69            throw new ChangeOpDeserializationException(
70                'Item id can not be parsed',
71                'invalid-item-id'
72            );
73        }
74    }
75
76}