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