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 | $serializationParts = self::splitSerialization( $idSerialization ); |
|
26 | $localId = strtoupper( $serializationParts[2] ); |
||
27 | 33 | $this->assertValidIdFormat( $localId ); |
|
28 | 33 | parent::__construct( self::joinSerialization( |
|
29 | 2 | [ $serializationParts[0], $serializationParts[1], $localId ] ) |
|
30 | ); |
||
31 | } |
||
32 | 31 | ||
33 | 15 | private function assertValidIdFormat( $idSerialization ) { |
|
34 | if ( !is_string( $idSerialization ) ) { |
||
35 | 16 | 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 | 1 | } |
|
41 | 1 | ||
42 | if ( strlen( $idSerialization ) > 10 |
||
43 | && substr( $idSerialization, 1 ) > Int32EntityId::MAX |
||
44 | ) { |
||
45 | throw new InvalidArgumentException( '$idSerialization can not exceed ' |
||
46 | . Int32EntityId::MAX ); |
||
47 | 1 | } |
|
48 | 1 | } |
|
49 | |||
50 | /** |
||
51 | * @see Int32EntityId::getNumericId |
||
52 | * |
||
53 | * @return int Guaranteed to be a distinct integer in the range [1..2147483647]. |
||
54 | */ |
||
55 | public function getNumericId() { |
||
56 | 1 | $serializationParts = self::splitSerialization( $this->serialization ); |
|
57 | 1 | return (int)substr( $serializationParts[2], 1 ); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getEntityType() { |
||
66 | 1 | ||
67 | 1 | /** |
|
68 | * @see Serializable::serialize |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function serialize() { |
||
75 | |||
76 | /** |
||
77 | * @see Serializable::unserialize |
||
78 | * |
||
79 | * @param string $serialized |
||
80 | */ |
||
81 | 9 | public function unserialize( $serialized ) { |
|
82 | 9 | $array = json_decode( $serialized ); |
|
83 | 1 | $this->serialization = is_array( $array ) ? $array[1] : $serialized; |
|
84 | } |
||
85 | |||
86 | 8 | /** |
|
87 | * Construct an ItemId given the numeric part of its serialization. |
||
88 | * |
||
89 | * CAUTION: new usages of this method are discouraged. Typically you |
||
90 | * should avoid dealing with just the numeric part, and use the whole |
||
91 | * serialization. Not doing so in new code requires special justification. |
||
92 | * |
||
93 | * @param int|float|string $numericId |
||
94 | * |
||
95 | * @return self |
||
96 | * @throws InvalidArgumentException |
||
97 | */ |
||
98 | public static function newFromNumber( $numericId ) { |
||
105 | |||
106 | /** |
||
107 | * CAUTION: Use the full string serialization whenever you can and avoid using numeric IDs. |
||
108 | * |
||
109 | * @param string $repositoryName |
||
110 | * @param int|float|string $numericId |
||
111 | * |
||
112 | * @return self |
||
113 | * @throws InvalidArgumentException |
||
114 | */ |
||
115 | public static function newFromRepositoryAndNumber( $repositoryName, $numericId ) { |
||
122 | |||
123 | } |
||
124 |