Passed
Push — isLocal ( 981ca0...5d5e06 )
by no
15:25 queued 07:30
created

EntityId::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Wikibase\DataModel\Entity;
4
5
use Comparable;
6
use InvalidArgumentException;
7
use Serializable;
8
9
/**
10
 * @since 0.5
11
 * Abstract since 2.0
12
 *
13
 * @license GPL-2.0+
14
 */
15
abstract class EntityId implements Comparable, Serializable {
16
17
	protected $serialization;
18
19
	const PATTERN = '/^:?(\w+:)*[^:]+\z/';
20
21
	/**
22
	 * @since 6.2
23
	 *
24
	 * @param string $serialization
25
	 */
26
	public function __construct( $serialization ) {
27
		self::assertValidSerialization( $serialization );
28
		$this->serialization = self::normalizeIdSerialization( $serialization );
29
	}
30
31
	private static function assertValidSerialization( $serialization ) {
32
		if ( !is_string( $serialization ) ) {
33
			throw new InvalidArgumentException( '$serialization must be a string' );
34
		}
35
36
		if ( $serialization === '' ) {
37
			throw new InvalidArgumentException( '$serialization must not be an empty string' );
38
		}
39
40
		if ( !preg_match( self::PATTERN, $serialization ) ) {
41
			throw new InvalidArgumentException( '$serialization must match ' . self::PATTERN );
42
		}
43
	}
44
45
	/**
46
	 * @return string
47
	 */
48
	public abstract function getEntityType();
49
50
	/**
51
	 * @return string
52
	 */
53
	public function getSerialization() {
54
		return $this->serialization;
55
	}
56
57
	/**
58
	 * Returns an array with 3 elements: the foreign repository name as the first element, the local ID as the last
59
	 * element and everything that is in between as the second element.
60
	 *
61
	 * self::joinSerialization can be used to restore the original serialization from the parts
62
	 * returned.
63
	 *
64
	 * @since 6.2
65
	 *
66
	 * @param string $serialization
67
	 * @return string[] Array containing the serialization split into 3 parts.
68
	 */
69
	public static function splitSerialization( $serialization ) {
70
		self::assertValidSerialization( $serialization );
71
72
		$parts = explode( ':', self::normalizeIdSerialization( $serialization ) );
73
		$localPart = array_pop( $parts );
74
		$repoName = array_shift( $parts );
75
		$prefixRemainder = implode( ':', $parts );
76
77
		return [
78
			is_string( $repoName ) ? $repoName : '',
79
			$prefixRemainder,
80
			$localPart
81
		];
82
	}
83
84
	/**
85
	 * Builds an ID serialization from the parts returned by self::splitSerialization.
86
	 *
87
	 * @since 6.2
88
	 *
89
	 * @param string[] $parts
90
	 * @return string
91
	 *
92
	 * @throws InvalidArgumentException
93
	 */
94
	public static function joinSerialization( array $parts ) {
95
		if ( end( $parts ) === '' ) {
96
			throw new InvalidArgumentException( 'The last element of $parts must not be empty.' );
97
		}
98
99
		return implode(
100
			':',
101
			array_filter( $parts, function( $part ) {
102
				return $part !== '';
103
			} )
104
		);
105
	}
106
107
	/**
108
	 * Returns an empty string for unprefixed, or a repository name for prefixed IDs. For chained
109
	 * prefixes (e.g. foo:bar:Q42) it will return only the first prefix.
110
	 *
111
	 * @since 6.2
112
	 *
113
	 * @return string
114
	 */
115
	public function getRepositoryName() {
116
		$parts = self::splitSerialization( $this->serialization );
117
118
		return $parts[0];
119
	}
120
121
	/**
122
	 * Returns the serialization without the first repository prefix.
123
	 *
124
	 * @since 6.2
125
	 *
126
	 * @return string
127
	 */
128
	public function getLocalPart() {
129
		$parts = self::splitSerialization( $this->serialization );
130
131
		return self::joinSerialization( [ '', $parts[1], $parts[2] ] );
132
	}
133
134
	/**
135
	 * Returns true if the ID does have a prefix and is therefore guaranteed to be unresolved. This
136
	 * is the same as checking if getRepositoryName() returns a non-empty string. Note that the
137
	 * inverse is not necessarily true! An unprefixed ID may still need to be resolved, e.g. when
138
	 * the local repository does not support a specific entity type, but a known remote repository
139
	 * does.
140
	 *
141
	 * @since 7.0
142
	 *
143
	 * @return bool
144
	 */
145
	public function isUnresolved() {
146
		// Not actually using getRepositoryName() for performance reasons
147
		return strpos( $this->serialization, ':' ) > 0;
148
	}
149
150
	/**
151
	 * @param string $id
152
	 * @return string
153
	 */
154
	private static function normalizeIdSerialization( $id ) {
155
		return ltrim( $id, ':' );
156
	}
157
158
	/**
159
	 * This is a human readable representation of the EntityId.
160
	 * This format is allowed to change and should therefore not
161
	 * be relied upon to be stable.
162
	 *
163
	 * @return string
164
	 */
165
	public function __toString() {
166
		return $this->serialization;
167
	}
168
169
	/**
170
	 * @see Comparable::equals
171
	 *
172
	 * @since 0.5
173
	 *
174
	 * @param mixed $target
175
	 *
176
	 * @return bool
177
	 */
178
	public function equals( $target ) {
179
		if ( $this === $target ) {
180
			return true;
181
		}
182
183
		return $target instanceof self
184
			&& $target->serialization === $this->serialization;
185
	}
186
187
}
188