Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
LexemeId
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
6 / 6
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 assertValidIdFormat
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 __serialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __unserialize
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getEntityType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getNumericId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare( strict_types = 1 );
4
5namespace Wikibase\Lexeme\Domain\Model;
6
7use InvalidArgumentException;
8use Wikibase\DataModel\Entity\Int32EntityId;
9use Wikibase\DataModel\Entity\SerializableEntityId;
10use Wikimedia\Assert\Assert;
11
12/**
13 * Immutable ID of a Lexeme in the lexicographical data model.
14 *
15 * @see https://www.mediawiki.org/wiki/Extension:WikibaseLexeme/Data_Model#Lexeme
16 *
17 * @license GPL-2.0-or-later
18 */
19class LexemeId extends SerializableEntityId implements Int32EntityId {
20
21    public const PATTERN = '/^L[1-9]\d{0,9}\z/i';
22
23    /**
24     * @throws InvalidArgumentException
25     */
26    public function __construct( string $serialization ) {
27        $this->assertValidIdFormat( $serialization );
28        parent::__construct( strtoupper( $serialization ) );
29    }
30
31    /**
32     * @throws InvalidArgumentException
33     */
34    private function assertValidIdFormat( string $serialization ): void {
35        Assert::parameterType( 'string', $serialization, '$serialization' );
36        Assert::parameter(
37            preg_match( self::PATTERN, $serialization ),
38            '$serialization',
39            'must match ' . self::PATTERN
40        );
41        Assert::parameter(
42            strlen( $serialization ) <= 10 || substr( $serialization, 1 ) <= Int32EntityId::MAX,
43            '$serialization',
44            'must not exceed ' . Int32EntityId::MAX
45        );
46    }
47
48    public function __serialize(): array {
49        return [ 'serialization' => $this->serialization ];
50    }
51
52    public function __unserialize( array $data ): void {
53        $this->__construct( $data['serialization'] ?? '' );
54        if ( $this->serialization !== $data['serialization'] ) {
55            throw new InvalidArgumentException( '$data contained invalid serialization' );
56        }
57    }
58
59    public function getEntityType(): string {
60        return 'lexeme';
61    }
62
63    public function getNumericId(): int {
64        return (int)substr( $this->serialization, 1 );
65    }
66
67}