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
EditFormElementsRequestParser
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\EditFormChangeOpDeserializer;
8use Wikibase\Lexeme\Presentation\ChangeOp\Deserialization\FormIdDeserializer;
9use Wikibase\Lexeme\Presentation\ChangeOp\Deserialization\ValidationContext;
10
11/**
12 * @license GPL-2.0-or-later
13 */
14class EditFormElementsRequestParser {
15
16    public const PARAM_DATA = 'data';
17
18    public const PARAM_FORM_ID = 'formId';
19
20    public const PARAM_BASEREVID = 'baserevid';
21
22    /**
23     * @var FormIdDeserializer
24     */
25    private $formIdDeserializer;
26
27    /**
28     * @var EditFormChangeOpDeserializer
29     */
30    private $editFormChangeOpDeserializer;
31
32    public function __construct(
33        FormIdDeserializer $formIdDeserializer,
34        EditFormChangeOpDeserializer $editFormChangeOpDeserializer
35    ) {
36        $this->formIdDeserializer = $formIdDeserializer;
37        $this->editFormChangeOpDeserializer = $editFormChangeOpDeserializer;
38    }
39
40    /**
41     * @param array $params
42     * @return EditFormElementsRequest
43     */
44    public function parse( array $params ) {
45        // guarded against missing fields by EditFormElements::getAllowedParams()
46
47        //TODO: validate language. How?
48        //TODO: validate if all grammatical features exist
49
50        $dataValidation = ValidationContext::create( self::PARAM_DATA );
51
52        $data = json_decode( $params[self::PARAM_DATA], true );
53        if ( !is_array( $data ) || !$data ) {
54            $dataValidation->addViolation(
55                new ParameterIsNotAJsonObject( self::PARAM_DATA, $params[self::PARAM_DATA] )
56            );
57            throw new LogicException( 'ApiUsageException not thrown' );
58        }
59
60        $formId = $this->formIdDeserializer->deserialize(
61            $params[self::PARAM_FORM_ID],
62            ValidationContext::create( self::PARAM_FORM_ID )
63        );
64
65        $this->editFormChangeOpDeserializer->setContext(
66            $dataValidation
67        );
68
69        $baseRevId = null;
70        if ( isset( $params[ self::PARAM_BASEREVID ] ) ) {
71            $baseRevId = (int)$params[self::PARAM_BASEREVID];
72        }
73
74        return new EditFormElementsRequest(
75            $formId,
76            $this->editFormChangeOpDeserializer->createEntityChangeOp( $data ),
77            $baseRevId
78        );
79    }
80
81}