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