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