Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
LexicalCategoryChangeOpDeserializer
100.00% covered (success)
100.00%
19 / 19
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%
11 / 11
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\ChangeOpLexicalCategory;
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 lexical category 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 LexicalCategoryChangeOpDeserializer implements ChangeOpDeserializer {
22
23    private $lexicalCategoryValidator;
24    private $stringNormalizer;
25
26    public function __construct(
27        ValueValidator $lexicalCategoryValidator,
28        StringNormalizer $stringNormalizer
29    ) {
30        $this->lexicalCategoryValidator = $lexicalCategoryValidator;
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( 'lexicalCategory', $changeRequest )
44            || !is_string( $changeRequest['lexicalCategory'] )
45        ) {
46            throw new ChangeOpDeserializationException(
47                'lexicalCategory must be a string',
48                'invalid-lexical-category'
49            );
50        }
51
52        $value = $this->stringNormalizer->cleanupToNFC( $changeRequest['lexicalCategory'] );
53
54        return new ChangeOpLexicalCategory(
55            $this->validateItemId( $value ),
56            $this->lexicalCategoryValidator
57        );
58    }
59
60    /**
61     * @param string $idSerialization
62     *
63     * @return ItemId
64     * @throws ChangeOpDeserializationException
65     */
66    private function validateItemId( $idSerialization ) {
67        try {
68            return new ItemId( $idSerialization );
69        } catch ( InvalidArgumentException $e ) {
70            throw new ChangeOpDeserializationException(
71                'Item id can not be parsed',
72                'invalid-item-id'
73            );
74        }
75    }
76
77}