Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
81.25% |
13 / 16 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
LexemeSubEntityId | |
81.25% |
13 / 16 |
|
57.14% |
4 / 7 |
10.66 | |
0.00% |
0 / 1 |
getSerialization | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__serialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__unserialize | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
getLexemeId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getIdSuffix | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
formatSerialization | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
extractLexemeIdAndSubEntityId | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 |
1 | <?php |
2 | |
3 | declare( strict_types = 1 ); |
4 | |
5 | namespace Wikibase\Lexeme\Domain\Model; |
6 | |
7 | use InvalidArgumentException; |
8 | use LogicException; |
9 | use Wikibase\DataModel\Entity\EntityId; |
10 | use Wikibase\DataModel\Entity\SerializableEntityId; |
11 | |
12 | /** |
13 | * An entity ID of a sub-entity of a {@link Lexeme}, |
14 | * which contains the {@link LexemeId} of the parent lexeme |
15 | * and exposes it via {@link getLexemeId()}. |
16 | * |
17 | * @license GPL-2.0-or-later |
18 | */ |
19 | abstract class LexemeSubEntityId extends SerializableEntityId { |
20 | |
21 | public const SUBENTITY_ID_SEPARATOR = '-'; |
22 | |
23 | public function getSerialization(): string { |
24 | return $this->serialization; |
25 | } |
26 | |
27 | public function __serialize(): array { |
28 | return [ 'serialization' => $this->serialization ]; |
29 | } |
30 | |
31 | public function __unserialize( array $data ): void { |
32 | $this->__construct( $data['serialization'] ?? '' ); |
33 | if ( $this->serialization !== $data['serialization'] ) { |
34 | throw new InvalidArgumentException( '$data contained invalid serialization' ); |
35 | } |
36 | } |
37 | |
38 | public function getLexemeId(): LexemeId { |
39 | return new LexemeId( $this->extractLexemeIdAndSubEntityId()[0] ); |
40 | } |
41 | |
42 | /** |
43 | * Returns the sub-entity id suffix, e.g. 'F1' for L1-F1, or 'S1' for L1-S1. |
44 | * Returns empty string for dummy ids and the like. |
45 | */ |
46 | public function getIdSuffix(): string { |
47 | if ( $this->serialization !== null ) { |
48 | return $this->extractLexemeIdAndSubEntityId()[1]; |
49 | } |
50 | |
51 | return ''; |
52 | } |
53 | |
54 | /** |
55 | * Format a serialization of a sub entity id, e.g. 'L1-F3' |
56 | * |
57 | * @param EntityId $containerEntityId Id of the entity in which the sub entity resides, e.g. L1 |
58 | * @param string $idPrefix The prefix of the sub entity, e.g. 'F' |
59 | * @param int $id The id of the sub entity, e.g. '3' |
60 | * |
61 | * @return string |
62 | */ |
63 | public static function formatSerialization( |
64 | EntityId $containerEntityId, |
65 | string $idPrefix, |
66 | int $id |
67 | ): string { |
68 | return $containerEntityId->getSerialization() . |
69 | self::SUBENTITY_ID_SEPARATOR . |
70 | $idPrefix . $id; |
71 | } |
72 | |
73 | /** |
74 | * This method should not be used for code that is expected to work with dummy ids. |
75 | * |
76 | * @return string[] two strings containing the lexeme id serialization and the sub-entity suffix, |
77 | * e.g. ['L1', 'F1'] for form id L1-F1. |
78 | */ |
79 | private function extractLexemeIdAndSubEntityId(): array { |
80 | $parts = explode( self::SUBENTITY_ID_SEPARATOR, $this->serialization, 2 ); |
81 | |
82 | if ( count( $parts ) !== 2 ) { |
83 | throw new LogicException( 'Malformed sub-entity id' ); |
84 | } |
85 | |
86 | return $parts; |
87 | } |
88 | |
89 | } |