@@ -16,16 +16,34 @@ abstract class EntityId implements Comparable, Serializable {
1616
1717 protected $ serialization ;
1818
19+ /**
20+ * @since 7.3
21+ *
22+ * @var string
23+ */
24+ protected $ repositoryName ;
25+
26+ /**
27+ * @since 7.3
28+ *
29+ * @var string
30+ */
31+ protected $ localPart ;
32+
1933 const PATTERN = '/^:?(\w+:)*[^:]+\z/ ' ;
2034
2135 /**
2236 * @since 6.2
2337 *
2438 * @param string $serialization
39+ *
40+ * @throws InvalidArgumentException
2541 */
2642 public function __construct ( $ serialization ) {
2743 self ::assertValidSerialization ( $ serialization );
2844 $ this ->serialization = self ::normalizeIdSerialization ( $ serialization );
45+
46+ list ( $ this ->repositoryName , $ this ->localPart ) = self ::extractRepositoryNameAndLocalPart ( $ serialization );
2947 }
3048
3149 private static function assertValidSerialization ( $ serialization ) {
@@ -63,12 +81,31 @@ public function getSerialization() {
6381 * @since 6.2
6482 *
6583 * @param string $serialization
84+ *
85+ * @throws InvalidArgumentException
6686 * @return string[] Array containing the serialization split into 3 parts.
6787 */
6888 public static function splitSerialization ( $ serialization ) {
6989 self ::assertValidSerialization ( $ serialization );
7090
71- $ parts = explode ( ': ' , self ::normalizeIdSerialization ( $ serialization ) );
91+ return self ::extractSerializationParts ( self ::normalizeIdSerialization ( $ serialization ) );
92+ }
93+
94+ /**
95+ * Splits the given ID serialization into an array with the following three elements:
96+ * - the repository name as the first element (empty string for local repository)
97+ * - any parts of the ID serialization but the repository name and the local ID (if any, empty string
98+ * if nothing else present)
99+ * - the local ID
100+ * Note: this method does not perform any validation of the given input. Calling code should take
101+ * care of this!
102+ *
103+ * @param string $serialization
104+ *
105+ * @return string[]
106+ */
107+ private static function extractSerializationParts ( $ serialization ) {
108+ $ parts = explode ( ': ' , $ serialization );
72109 $ localPart = array_pop ( $ parts );
73110 $ repoName = array_shift ( $ parts );
74111 $ prefixRemainder = implode ( ': ' , $ parts );
@@ -86,9 +123,9 @@ public static function splitSerialization( $serialization ) {
86123 * @since 6.2
87124 *
88125 * @param string[] $parts
89- * @return string
90126 *
91127 * @throws InvalidArgumentException
128+ * @return string
92129 */
93130 public static function joinSerialization ( array $ parts ) {
94131 if ( end ( $ parts ) === '' ) {
@@ -112,9 +149,7 @@ public static function joinSerialization( array $parts ) {
112149 * @return string
113150 */
114151 public function getRepositoryName () {
115- $ parts = self ::splitSerialization ( $ this ->serialization );
116-
117- return $ parts [0 ];
152+ return $ this ->repositoryName ;
118153 }
119154
120155 /**
@@ -125,9 +160,7 @@ public function getRepositoryName() {
125160 * @return string
126161 */
127162 public function getLocalPart () {
128- $ parts = self ::splitSerialization ( $ this ->serialization );
129-
130- return self ::joinSerialization ( [ '' , $ parts [1 ], $ parts [2 ] ] );
163+ return $ this ->localPart ;
131164 }
132165
133166 /**
@@ -144,6 +177,7 @@ public function isForeign() {
144177
145178 /**
146179 * @param string $id
180+ *
147181 * @return string
148182 */
149183 private static function normalizeIdSerialization ( $ id ) {
@@ -179,4 +213,20 @@ public function equals( $target ) {
179213 && $ target ->serialization === $ this ->serialization ;
180214 }
181215
216+ /**
217+ * Returns a pair (repository name, local part of ID) from the given ID serialization.
218+ * Note: this does not perform any validation of the given input. Calling code should take
219+ * care of this!
220+ *
221+ * @since 7.3
222+ *
223+ * @param string $serialization
224+ *
225+ * @return string[] Array of form [ string $repositoryName, string $localPart ]
226+ */
227+ protected static function extractRepositoryNameAndLocalPart ( $ serialization ) {
228+ $ parts = explode ( ': ' , $ serialization , 2 );
229+ return isset ( $ parts [1 ] ) ? $ parts : [ '' , $ parts [0 ] ];
230+ }
231+
182232}
0 commit comments