Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
SenseListChangeOpDeserializer
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
3 / 3
10
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
 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%
32 / 32
100.00% covered (success)
100.00%
1 / 1
8
1<?php
2
3namespace Wikibase\Lexeme\Presentation\ChangeOp\Deserialization;
4
5use Wikibase\DataModel\Services\Statement\GuidGenerator;
6use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpRemoveSense;
7use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpSenseAdd;
8use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpsSensesEdit;
9use Wikibase\Lexeme\MediaWiki\Api\Error\JsonFieldHasWrongType;
10use Wikibase\Lexeme\MediaWiki\Api\Error\JsonFieldIsRequired;
11use Wikibase\Repo\ChangeOp\ChangeOp;
12use Wikibase\Repo\ChangeOp\ChangeOpDeserializer;
13use Wikibase\Repo\ChangeOp\ChangeOps;
14use Wikibase\Repo\ChangeOp\Deserialization\ChangeOpDeserializationException;
15
16/**
17 * Deserialize change requests on multiple senses
18 *
19 * @see docs/change-op-serialization.wiki for a description of the serialization format.
20 *
21 * @license GPL-2.0-or-later
22 */
23class SenseListChangeOpDeserializer implements ChangeOpDeserializer {
24
25    private const PARAM_SENSE_ID = 'id';
26
27    /**
28     * @var SenseChangeOpDeserializer
29     */
30    private $senseChangeOpDeserializer;
31
32    /**
33     * @var SenseIdDeserializer
34     */
35    private $senseIdDeserializer;
36
37    /**
38     * @var ValidationContext
39     */
40    private $validationContext;
41
42    public function __construct(
43        SenseIdDeserializer $senseIdDeserializer,
44        SenseChangeOpDeserializer $senseChangeOpDeserializer
45    ) {
46        $this->senseChangeOpDeserializer = $senseChangeOpDeserializer;
47        $this->senseIdDeserializer = $senseIdDeserializer;
48    }
49
50    public function setContext( ValidationContext $context ) {
51        $this->validationContext = $context;
52    }
53
54    /**
55     * @see ChangeOpDeserializer::createEntityChangeOp
56     *
57     * @param array $changeRequest
58     *
59     * @throws ChangeOpDeserializationException
60     *
61     * @return ChangeOp
62     */
63    public function createEntityChangeOp( array $changeRequest ) {
64        $lexemeChangeOps = new ChangeOps();
65        $changeOpsForSense = [];
66
67        if ( !is_array( $changeRequest['senses'] ) ) {
68            $this->validationContext->addViolation(
69                new JsonFieldHasWrongType( 'array', gettype( $changeRequest['senses'] ) )
70            );
71        }
72
73        foreach ( $changeRequest['senses'] as $index => $serializedSense ) {
74            $senseValidationContext = $this->validationContext->at( $index );
75            $this->senseChangeOpDeserializer->setContext( $senseValidationContext );
76
77            if ( !is_array( $serializedSense ) ) {
78                $senseValidationContext->addViolation(
79                    new JsonFieldHasWrongType( 'array', gettype( $serializedSense ) )
80                );
81            }
82
83            if ( array_key_exists( 'remove', $serializedSense ) ) {
84                if ( !array_key_exists( self::PARAM_SENSE_ID, $serializedSense ) ) {
85                    $senseValidationContext->addViolation(
86                        new JsonFieldIsRequired( self::PARAM_SENSE_ID )
87                    );
88                }
89
90                $senseId = $this->senseIdDeserializer->deserialize(
91                    $serializedSense[self::PARAM_SENSE_ID],
92                    $senseValidationContext->at( self::PARAM_SENSE_ID )
93                );
94
95                $lexemeChangeOps->add( new ChangeOpRemoveSense( $senseId ) );
96            } elseif ( array_key_exists( 'add', $serializedSense ) ) {
97                $lexemeChangeOps->add( new ChangeOpSenseAdd(
98                    $this->senseChangeOpDeserializer->createEntityChangeOp( $serializedSense ),
99                    new GuidGenerator()
100                ) );
101            } elseif ( array_key_exists( self::PARAM_SENSE_ID, $serializedSense ) ) {
102                $changeOpsForSense[$serializedSense[self::PARAM_SENSE_ID]] =
103                    $this->senseChangeOpDeserializer->createEntityChangeOp( $serializedSense );
104            }
105        }
106
107        return new ChangeOps( [ $lexemeChangeOps, new ChangeOpsSensesEdit( $changeOpsForSense ) ] );
108    }
109
110}