Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
27 / 30
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AddFormRequestParser
90.00% covered (success)
90.00%
27 / 30
33.33% covered (danger)
33.33%
1 / 3
8.06
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.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
4
 parseLexemeId
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
3.14
1<?php
2
3namespace Wikibase\Lexeme\MediaWiki\Api;
4
5use LogicException;
6use Wikibase\DataModel\Entity\EntityIdParser;
7use Wikibase\DataModel\Entity\EntityIdParsingException;
8use Wikibase\Lexeme\Domain\Model\LexemeId;
9use Wikibase\Lexeme\MediaWiki\Api\Error\ParameterIsNotAJsonObject;
10use Wikibase\Lexeme\MediaWiki\Api\Error\ParameterIsNotLexemeId;
11use Wikibase\Lexeme\Presentation\ChangeOp\Deserialization\EditFormChangeOpDeserializer;
12use Wikibase\Lexeme\Presentation\ChangeOp\Deserialization\ValidationContext;
13
14/**
15 * @license GPL-2.0-or-later
16 */
17class AddFormRequestParser {
18
19    public const PARAM_DATA = 'data';
20    public const PARAM_LEXEME_ID = 'lexemeId';
21    public const PARAM_BASEREVID = 'baserevid';
22
23    /**
24     * @var EntityIdParser
25     */
26    private $entityIdParser;
27
28    /**
29     * @var EditFormChangeOpDeserializer
30     */
31    private $editFormChangeOpDeserializer;
32
33    public function __construct(
34        EntityIdParser $entityIdParser,
35        EditFormChangeOpDeserializer $editFormChangeOpDeserializer
36    ) {
37        $this->entityIdParser = $entityIdParser;
38        $this->editFormChangeOpDeserializer = $editFormChangeOpDeserializer;
39    }
40
41    public function parse( array $params ): AddFormRequest {
42        // guarded against missing fields by AddForm::getAllowedParams()
43
44        //TODO: validate language. How?
45        //TODO: validate if all grammatical features exist
46
47        $dataValidation = ValidationContext::create( self::PARAM_DATA );
48
49        $data = json_decode( $params[self::PARAM_DATA], true );
50        if ( !is_array( $data ) || !$data ) {
51            $dataValidation->addViolation(
52                new ParameterIsNotAJsonObject( self::PARAM_DATA, $params[self::PARAM_DATA] )
53            );
54            throw new LogicException( 'ApiUsageException not thrown' );
55        }
56
57        $lexemeId = $this->parseLexemeId(
58            $params[self::PARAM_LEXEME_ID],
59            ValidationContext::create( self::PARAM_LEXEME_ID )
60        );
61
62        $baseRevId = null;
63        if ( isset( $params[ self::PARAM_BASEREVID ] ) ) {
64            $baseRevId = (int)$params[self::PARAM_BASEREVID];
65        }
66
67        $this->editFormChangeOpDeserializer->setContext( $dataValidation );
68
69        return new AddFormRequest(
70            $lexemeId,
71            $this->editFormChangeOpDeserializer->createEntityChangeOp( $data ),
72            $baseRevId
73        );
74    }
75
76    /**
77     * @param string $id
78     * @param ValidationContext $validationContext
79     * @return LexemeId|null
80     */
81    private function parseLexemeId( $id, ValidationContext $validationContext ) {
82        try {
83            $lexemeId = $this->entityIdParser->parse( $id );
84        } catch ( EntityIdParsingException $e ) {
85            $validationContext->addViolation( new ParameterIsNotLexemeId( $id ) );
86            return null;
87        }
88
89        if ( $lexemeId->getEntityType() !== 'lexeme' ) {
90            $validationContext->addViolation( new ParameterIsNotLexemeId( $id ) );
91            return null;
92        }
93
94        /**
95         * @var LexemeId $lexemeId
96         */
97        '@phan-var LexemeId $lexemeId';
98
99        return $lexemeId;
100    }
101
102}