Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
SenseIdDeserializer | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
deserialize | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\Presentation\ChangeOp\Deserialization; |
4 | |
5 | use Wikibase\DataModel\Entity\EntityIdParser; |
6 | use Wikibase\DataModel\Entity\EntityIdParsingException; |
7 | use Wikibase\Lexeme\Domain\Model\Sense; |
8 | use Wikibase\Lexeme\Domain\Model\SenseId; |
9 | use Wikibase\Lexeme\MediaWiki\Api\Error\ParameterIsNotSenseId; |
10 | |
11 | /** |
12 | * A throwing ValidationContext guards us from actual null return values, |
13 | * w/o it the result is too fuzzy to regard it clean |
14 | * |
15 | * @license GPL-2.0-or-later |
16 | */ |
17 | class SenseIdDeserializer { |
18 | |
19 | /** |
20 | * @var EntityIdParser |
21 | */ |
22 | private $entityIdParser; |
23 | |
24 | public function __construct( EntityIdParser $idParser ) { |
25 | $this->entityIdParser = $idParser; |
26 | } |
27 | |
28 | /** |
29 | * @param string $id |
30 | * @param ValidationContext $validationContext |
31 | * @return SenseId|null |
32 | */ |
33 | public function deserialize( $id, ValidationContext $validationContext ) { |
34 | try { |
35 | $senseId = $this->entityIdParser->parse( $id ); |
36 | } catch ( EntityIdParsingException $e ) { |
37 | $validationContext->addViolation( new ParameterIsNotSenseId( $id ) ); |
38 | return null; |
39 | } |
40 | |
41 | if ( $senseId->getEntityType() !== Sense::ENTITY_TYPE ) { |
42 | $validationContext->addViolation( new ParameterIsNotSenseId( $id ) ); |
43 | return null; |
44 | } |
45 | |
46 | /** @var SenseId $senseId */ |
47 | '@phan-var SenseId $senseId'; |
48 | return $senseId; |
49 | } |
50 | |
51 | } |