1 | <?php |
||
12 | class PropertyId extends EntityId implements Int32EntityId { |
||
13 | |||
14 | /** |
||
15 | * @since 0.5 |
||
16 | */ |
||
17 | const PATTERN = '/^P[1-9]\d{0,9}\z/i'; |
||
18 | |||
19 | /** |
||
20 | * @param string $idSerialization |
||
21 | * |
||
22 | * @throws InvalidArgumentException |
||
23 | */ |
||
24 | public function __construct( $idSerialization ) { |
||
25 | $serializationParts = self::splitSerialization( $idSerialization ); |
||
26 | $localId = strtoupper( $serializationParts[2] ); |
||
27 | $this->assertValidIdFormat( $localId ); |
||
28 | parent::__construct( self::joinSerialization( |
||
29 | [ $serializationParts[0], $serializationParts[1], $localId ] ) |
||
30 | ); |
||
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 distinct integer in the range [1..2147483647]. |
||
54 | */ |
||
55 | public function getNumericId() { |
||
56 | $serializationParts = self::splitSerialization( $this->serialization ); |
||
57 | return (int)substr( $serializationParts[2], 1 ); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getEntityType() { |
||
66 | |||
67 | /** |
||
68 | * @see Serializable::serialize |
||
69 | * |
||
70 | * @since 7.0 serialization format changed in an incompatible way |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | public function serialize() { |
||
77 | |||
78 | /** |
||
79 | * @see Serializable::unserialize |
||
80 | * |
||
81 | * @param string $serialized |
||
82 | */ |
||
83 | public function unserialize( $serialized ) { |
||
87 | |||
88 | /** |
||
89 | * Construct a PropertyId given the numeric part of its serialization. |
||
90 | * |
||
91 | * CAUTION: new usages of this method are discouraged. Typically you |
||
92 | * should avoid dealing with just the numeric part, and use the whole |
||
93 | * serialization. Not doing so in new code requires special justification. |
||
94 | * |
||
95 | * @param int|float|string $numericId |
||
96 | * |
||
97 | * @return self |
||
98 | * @throws InvalidArgumentException |
||
99 | */ |
||
100 | public static function newFromNumber( $numericId ) { |
||
107 | |||
108 | /** |
||
109 | * CAUTION: Use the full string serialization whenever you can and avoid using numeric IDs. |
||
110 | * |
||
111 | * @since 7.0 |
||
112 | * |
||
113 | * @param string $repositoryName |
||
114 | * @param int|float|string $numericId |
||
115 | * |
||
116 | * @return self |
||
117 | * @throws InvalidArgumentException |
||
118 | */ |
||
119 | public static function newFromRepositoryAndNumber( $repositoryName, $numericId ) { |
||
126 | |||
127 | } |
||
128 |