Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
EditSenseChangeOpDeserializer
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
8
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%
22 / 22
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3namespace Wikibase\Lexeme\Presentation\ChangeOp\Deserialization;
4
5use Wikibase\Lexeme\DataAccess\ChangeOp\ChangeOpSenseEdit;
6use Wikibase\Lexeme\MediaWiki\Api\Error\InvalidSenseClaims;
7use Wikibase\Lexeme\MediaWiki\Api\Error\JsonFieldHasWrongType;
8use Wikibase\Repo\ChangeOp\ChangeOp;
9use Wikibase\Repo\ChangeOp\ChangeOpDeserializer;
10use Wikibase\Repo\ChangeOp\Deserialization\ChangeOpDeserializationException;
11use Wikibase\Repo\ChangeOp\Deserialization\ClaimsChangeOpDeserializer;
12
13/**
14 * Deserialize a change request on a single sense
15 *
16 * @license GPL-2.0-or-later
17 */
18class EditSenseChangeOpDeserializer implements ChangeOpDeserializer {
19
20    private const PARAM_GLOSSES = 'glosses';
21
22    private const PARAM_STATEMENTS = 'claims';
23
24    /**
25     * @var GlossesChangeOpDeserializer
26     */
27    private $glossesChangeOpDeserializer;
28
29    /**
30     * @var ValidationContext
31     */
32    private $validationContext;
33
34    /**
35     * @var ClaimsChangeOpDeserializer
36     */
37    private $statementsChangeOpDeserializer;
38
39    public function __construct(
40        GlossesChangeOpDeserializer $glossesChangeOpDeserializer,
41        ClaimsChangeOpDeserializer $statementsChangeOpDeserializer
42    ) {
43        $this->glossesChangeOpDeserializer = $glossesChangeOpDeserializer;
44        $this->statementsChangeOpDeserializer = $statementsChangeOpDeserializer;
45    }
46
47    public function setContext( ValidationContext $context ) {
48        $this->validationContext = $context;
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        $changeOps = [];
62
63        if ( array_key_exists( self::PARAM_GLOSSES, $changeRequest ) ) {
64            $glosses = $changeRequest[self::PARAM_GLOSSES];
65
66            $glossesContext = $this->validationContext->at( self::PARAM_GLOSSES );
67
68            if ( !is_array( $glosses ) ) {
69                $glossesContext->addViolation(
70                    new JsonFieldHasWrongType( 'array', gettype( $glosses ) )
71                );
72            } else {
73                $this->glossesChangeOpDeserializer->setContext( $glossesContext );
74                $changeOps[] =
75                    $this->glossesChangeOpDeserializer->createEntityChangeOp( $glosses );
76            }
77        }
78
79        if ( array_key_exists( self::PARAM_STATEMENTS, $changeRequest ) ) {
80            $statementsContext = $this->validationContext->at( self::PARAM_STATEMENTS );
81            $statementsRequest = $changeRequest[self::PARAM_STATEMENTS];
82
83            if ( !is_array( $statementsRequest ) ) {
84                $statementsContext->addViolation(
85                    new JsonFieldHasWrongType( 'array', gettype( $statementsRequest ) )
86                );
87            } else {
88                try {
89                    $changeOps[] = $this->statementsChangeOpDeserializer->createEntityChangeOp( $changeRequest );
90                } catch ( ChangeOpDeserializationException $exception ) {
91                    $statementsContext->addViolation( new InvalidSenseClaims() );
92                }
93            }
94        }
95
96        return new ChangeOpSenseEdit( $changeOps );
97    }
98
99}