Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.83% covered (success)
95.83%
23 / 24
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
EditSenseElementsRequestParser
95.83% covered (success)
95.83%
23 / 24
50.00% covered (danger)
50.00%
1 / 2
5
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 parse
95.45% covered (success)
95.45%
21 / 22
0.00% covered (danger)
0.00%
0 / 1
4
1<?php
2
3namespace Wikibase\Lexeme\MediaWiki\Api;
4
5use LogicException;
6use Wikibase\Lexeme\MediaWiki\Api\Error\ParameterIsNotAJsonObject;
7use Wikibase\Lexeme\Presentation\ChangeOp\Deserialization\EditSenseChangeOpDeserializer;
8use Wikibase\Lexeme\Presentation\ChangeOp\Deserialization\SenseIdDeserializer;
9use Wikibase\Lexeme\Presentation\ChangeOp\Deserialization\ValidationContext;
10
11/**
12 * @license GPL-2.0-or-later
13 */
14class EditSenseElementsRequestParser {
15
16    public const PARAM_DATA = 'data';
17
18    public const PARAM_SENSE_ID = 'senseId';
19    public const PARAM_BASEREVID = 'baserevid';
20
21    /**
22     * @var SenseIdDeserializer
23     */
24    private $senseIdDeserializer;
25
26    /**
27     * @var EditSenseChangeOpDeserializer
28     */
29    private $editSenseChangeOpDeserializer;
30
31    public function __construct(
32        SenseIdDeserializer $senseIdDeserializer,
33        EditSenseChangeOpDeserializer $editSenseChangeOpDeserializer
34    ) {
35        $this->senseIdDeserializer = $senseIdDeserializer;
36        $this->editSenseChangeOpDeserializer = $editSenseChangeOpDeserializer;
37    }
38
39    /**
40     * @param array $params
41     * @return EditSenseElementsRequest
42     */
43    public function parse( array $params ) {
44        // guarded against missing fields by EditSenseElements::getAllowedParams()
45
46        //TODO: validate language. How?
47
48        $dataValidation = ValidationContext::create( self::PARAM_DATA );
49
50        $data = json_decode( $params[self::PARAM_DATA], true );
51        if ( !is_array( $data ) || !$data ) {
52            $dataValidation->addViolation(
53                new ParameterIsNotAJsonObject( self::PARAM_DATA, $params[self::PARAM_DATA] )
54            );
55            throw new LogicException( 'ApiUsageException not thrown' );
56        }
57
58        $senseId = $this->senseIdDeserializer->deserialize(
59            $params[self::PARAM_SENSE_ID],
60            ValidationContext::create( self::PARAM_SENSE_ID )
61        );
62
63        $this->editSenseChangeOpDeserializer->setContext(
64            $dataValidation
65        );
66
67        $baseRevId = null;
68        if ( isset( $params[ self::PARAM_BASEREVID ] ) ) {
69            $baseRevId = (int)$params[self::PARAM_BASEREVID];
70        }
71
72        return new EditSenseElementsRequest(
73            $senseId,
74            $this->editSenseChangeOpDeserializer->createEntityChangeOp( $data ),
75            $baseRevId
76        );
77    }
78
79}