1 | <?php |
||
14 | abstract class EntityId implements Serializable { |
||
15 | |||
16 | protected $serialization; |
||
17 | |||
18 | /** |
||
19 | * @since 7.3 |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $repositoryName; |
||
24 | |||
25 | /** |
||
26 | * @since 7.3 |
||
27 | * |
||
28 | 31 | * @var string |
|
29 | 31 | */ |
|
30 | protected $localPart; |
||
31 | |||
32 | public const PATTERN = '/^:?(\w+:)*[^:]+\z/'; |
||
33 | |||
34 | /** |
||
35 | * @since 6.2 |
||
36 | * |
||
37 | * @param string $serialization |
||
38 | * |
||
39 | 5 | * @throws InvalidArgumentException |
|
40 | 5 | */ |
|
41 | public function __construct( $serialization ) { |
||
47 | |||
48 | private static function assertValidSerialization( $serialization ) { |
||
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | abstract public function getEntityType(); |
||
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | public function getSerialization() { |
||
73 | |||
74 | /** |
||
75 | * Returns an array with 3 elements: the foreign repository name as the first element, the local ID as the last |
||
76 | * element and everything that is in between as the second element. |
||
77 | * |
||
78 | * EntityId::joinSerialization can be used to restore the original serialization from the parts returned. |
||
79 | * |
||
80 | * @since 6.2 |
||
81 | * |
||
82 | * @param string $serialization |
||
83 | * |
||
84 | * @throws InvalidArgumentException |
||
85 | * @return string[] Array containing the serialization split into 3 parts. |
||
86 | */ |
||
87 | public static function splitSerialization( $serialization ) { |
||
92 | |||
93 | /** |
||
94 | * Splits the given ID serialization into an array with the following three elements: |
||
95 | * - the repository name as the first element (empty string for local repository) |
||
96 | * - any parts of the ID serialization but the repository name and the local ID (if any, empty string |
||
97 | * if nothing else present) |
||
98 | * - the local ID |
||
99 | * Note: this method does not perform any validation of the given input. Calling code should take |
||
100 | * care of this! |
||
101 | * |
||
102 | * @param string $serialization |
||
103 | * |
||
104 | * @return string[] |
||
105 | */ |
||
106 | private static function extractSerializationParts( $serialization ) { |
||
118 | |||
119 | /** |
||
120 | * Builds an ID serialization from the parts returned by EntityId::splitSerialization. |
||
121 | * |
||
122 | * @since 6.2 |
||
123 | * |
||
124 | * @param string[] $parts |
||
125 | * |
||
126 | * @throws InvalidArgumentException |
||
127 | * @return string |
||
128 | */ |
||
129 | public static function joinSerialization( array $parts ) { |
||
141 | |||
142 | /** |
||
143 | * Returns '' for local IDs and the foreign repository name for foreign IDs. For chained IDs (e.g. foo:bar:Q42) it |
||
144 | * will return only the first part. |
||
145 | * |
||
146 | * @since 6.2 |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getRepositoryName() { |
||
153 | |||
154 | /** |
||
155 | * Returns the serialization without the first repository prefix. |
||
156 | * |
||
157 | * @since 6.2 |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function getLocalPart() { |
||
164 | |||
165 | /** |
||
166 | * Returns true iff EntityId::getRepositoryName returns a non-empty string. |
||
167 | * |
||
168 | * @since 6.2 |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function isForeign() { |
||
175 | |||
176 | /** |
||
177 | * @param string $id |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | private static function normalizeIdSerialization( $id ) { |
||
184 | |||
185 | /** |
||
186 | * This is a human readable representation of the EntityId. |
||
187 | * This format is allowed to change and should therefore not |
||
188 | * be relied upon to be stable. |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | public function __toString() { |
||
195 | |||
196 | /** |
||
197 | * |
||
198 | * @since 0.5 |
||
199 | * |
||
200 | * @param mixed $target |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | public function equals( $target ) { |
||
212 | |||
213 | /** |
||
214 | * Returns a pair (repository name, local part of ID) from the given ID serialization. |
||
215 | * Note: this does not perform any validation of the given input. Calling code should take |
||
216 | * care of this! |
||
217 | * |
||
218 | * @since 7.3 |
||
219 | * |
||
220 | * @param string $serialization |
||
221 | * |
||
222 | * @return string[] Array of form [ string $repositoryName, string $localPart ] |
||
223 | */ |
||
224 | protected static function extractRepositoryNameAndLocalPart( $serialization ) { |
||
227 | |||
228 | } |
||
229 |