1 | <?php |
||
12 | class ItemId extends EntityId implements Int32EntityId { |
||
13 | |||
14 | /** |
||
15 | * @since 0.5 |
||
16 | */ |
||
17 | const PATTERN = '/^Q[1-9]\d{0,9}\z/i'; |
||
18 | |||
19 | /** |
||
20 | * @param string $idSerialization |
||
21 | * |
||
22 | 33 | * @throws InvalidArgumentException |
|
23 | 33 | */ |
|
24 | 16 | public function __construct( $idSerialization ) { |
|
25 | 16 | $parts = self::splitSerialization( $idSerialization ); |
|
26 | $this->assertValidIdFormat( $parts[2] ); |
||
27 | 33 | parent::__construct( self::joinSerialization( |
|
28 | 33 | [ $parts[0], $parts[1], strtoupper( $parts[2] ) ] |
|
29 | 2 | ) ); |
|
30 | } |
||
31 | |||
32 | 31 | private function assertValidIdFormat( $idSerialization ) { |
|
33 | 15 | if ( !preg_match( self::PATTERN, $idSerialization ) ) { |
|
34 | throw new InvalidArgumentException( '$idSerialization must match ' . self::PATTERN ); |
||
35 | 16 | } |
|
36 | |||
37 | if ( strlen( $idSerialization ) > 10 |
||
38 | && substr( $idSerialization, 1 ) > Int32EntityId::MAX |
||
39 | ) { |
||
40 | 1 | throw new InvalidArgumentException( '$idSerialization can not exceed ' |
|
41 | 1 | . Int32EntityId::MAX ); |
|
42 | } |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @see Int32EntityId::getNumericId |
||
47 | 1 | * |
|
48 | 1 | * @return int Guaranteed to be a distinct integer in the range [1..2147483647]. |
|
49 | */ |
||
50 | public function getNumericId() { |
||
51 | $serializationParts = self::splitSerialization( $this->serialization ); |
||
52 | return (int)substr( $serializationParts[2], 1 ); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | 1 | * @return string |
|
57 | 1 | */ |
|
58 | public function getEntityType() { |
||
59 | return 'item'; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @see Serializable::serialize |
||
64 | * |
||
65 | 1 | * @since 7.0 serialization format changed in an incompatible way |
|
66 | 1 | * |
|
67 | 1 | * @return string |
|
68 | */ |
||
69 | public function serialize() { |
||
70 | return $this->serialization; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @see Serializable::unserialize |
||
75 | * |
||
76 | * @param string $serialized |
||
77 | */ |
||
78 | public function unserialize( $serialized ) { |
||
79 | $array = json_decode( $serialized ); |
||
80 | $this->serialization = is_array( $array ) ? $array[1] : $serialized; |
||
81 | 9 | list( $this->repositoryName, $this->localPart ) = |
|
82 | 9 | self::extractRepositoryNameAndLocalPart( $this->serialization ); |
|
83 | 1 | } |
|
84 | |||
85 | /** |
||
86 | 8 | * Construct an ItemId given the numeric part of its serialization. |
|
87 | * |
||
88 | * CAUTION: new usages of this method are discouraged. Typically you |
||
89 | * should avoid dealing with just the numeric part, and use the whole |
||
90 | * serialization. Not doing so in new code requires special justification. |
||
91 | * |
||
92 | * @param int|float|string $numericId |
||
93 | * |
||
94 | * @return self |
||
95 | * @throws InvalidArgumentException |
||
96 | */ |
||
97 | public static function newFromNumber( $numericId ) { |
||
104 | |||
105 | /** |
||
106 | * CAUTION: Use the full string serialization whenever you can and avoid using numeric IDs. |
||
107 | * |
||
108 | * @since 7.0 |
||
109 | * |
||
110 | * @param string $repositoryName |
||
111 | * @param int|float|string $numericId |
||
112 | * |
||
113 | * @return self |
||
114 | * @throws InvalidArgumentException |
||
115 | */ |
||
116 | public static function newFromRepositoryAndNumber( $repositoryName, $numericId ) { |
||
123 | |||
124 | } |
||
125 |