Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
MediaInfoId
93.75% covered (success)
93.75%
15 / 16
83.33% covered (warning)
83.33%
5 / 6
11.03
0.00% covered (danger)
0.00%
0 / 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%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 getNumericId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getEntityType
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
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Wikibase\MediaInfo\DataModel;
4
5use InvalidArgumentException;
6use Wikibase\DataModel\Entity\EntityId;
7use Wikibase\DataModel\Entity\Int32EntityId;
8use Wikibase\DataModel\Entity\SerializableEntityId;
9
10/**
11 * Identifier for media info entities, containing a numeric id prefixed by 'M'.
12 *
13 * @since 0.1
14 *
15 * @license GPL-2.0-or-later
16 * @author Bene* < benestar.wikimedia@gmail.com >
17 * @author Thiemo Kreuz
18 */
19class MediaInfoId extends SerializableEntityId implements Int32EntityId {
20
21    public const PATTERN = '/^M[1-9]\d{0,9}\z/i';
22
23    /**
24     * @param string $idSerialization
25     *
26     * @throws InvalidArgumentException
27     */
28    public function __construct( $idSerialization ) {
29        $this->assertValidIdFormat( $idSerialization );
30        parent::__construct( strtoupper( $idSerialization ) );
31    }
32
33    private function assertValidIdFormat( $idSerialization ) {
34        if ( !is_string( $idSerialization ) ) {
35            throw new InvalidArgumentException( '$idSerialization must be a string' );
36        }
37
38        if ( !preg_match( self::PATTERN, $idSerialization ) ) {
39            throw new InvalidArgumentException( '$idSerialization must match ' . self::PATTERN );
40        }
41
42        if ( strlen( $idSerialization ) > 10
43            && substr( $idSerialization, 1 ) > Int32EntityId::MAX
44        ) {
45            throw new InvalidArgumentException( '$idSerialization can not exceed '
46                . Int32EntityId::MAX );
47        }
48    }
49
50    /**
51     * @see Int32EntityId::getNumericId
52     *
53     * @return int Guaranteed to be a unique integer in the range [1..2147483647].
54     */
55    public function getNumericId() {
56        return (int)substr( $this->serialization, 1 );
57    }
58
59    /**
60     * @see EntityId::getEntityType
61     *
62     * @return string
63     */
64    public function getEntityType() {
65        return 'mediainfo';
66    }
67
68    /**
69     * @see Serializable::serialize
70     *
71     * @return array
72     */
73    public function __serialize(): array {
74        return [ 'serialization' => $this->serialization ];
75    }
76
77    /**
78     * @see Serializable::unserialize
79     *
80     * @param array $value
81     */
82    public function __unserialize( array $value ): void {
83        $this->__construct( $value['serialization'] ?? '' );
84        if ( $this->serialization !== $value['serialization'] ) {
85            throw new InvalidArgumentException( '$value contained invalid serialization' );
86        }
87    }
88
89}