Completed
Pull Request — master (#678)
by Jakob
03:19
created

EntityId::joinSerialization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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
 * Constructor non-public since 1.0
12
 * Abstract since 2.0
13
 *
14
 * @license GPL-2.0+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
abstract class EntityId implements Comparable, Serializable {
18
19
	protected $serialization;
20
21
	/**
22
	 * @param string $serialization
23
	 */
24
	public function __construct( $serialization ) {
25
		$this->serialization = self::normalizeIdSerialization( $serialization );
26
		$this->assertValidSerialization( $this->serialization );
27
	}
28 31
29 31
	private function assertValidSerialization( $serialization ) {
30
		if ( empty( $serialization ) ) {
31
			throw new InvalidArgumentException( '$serialization must not be an empty string' );
32
		}
33
	}
34
35
	/**
36
	 * @return string
37
	 */
38
	public abstract function getEntityType();
39 5
40 5
	/**
41
	 * @return string
42
	 */
43
	public function getSerialization() {
44
		return $this->serialization;
45
	}
46
47
	/**
48
	 * Returns an array with 3 elements: the foreign repository name as the first element, the local ID as the last
49
	 * element and everything that is in between as the second element.
50
	 *
51
	 * EntityId::joinSerialization can be used to restore the original serialization from the parts returned.
52 5
	 *
53 5
	 * @param string $serialization
54 5
	 * @return string[] Array containing the serialization split into 3 parts.
55
	 */
56
	public static function splitSerialization( $serialization ) {
57
		$parts = explode( ':', self::normalizeIdSerialization( $serialization ) );
58 5
		$localPart = array_pop( $parts );
59
		$repoName = array_shift( $parts ) ?: '';
60
		$prefixRemainder = implode( ':', $parts );
61
62
		return array( $repoName, $prefixRemainder, $localPart );
63
	}
64
65
	/**
66
	 * Builds an ID serialization from the parts returned by EntityId::splitSerialization.
67
	 *
68
	 * @param string[] $parts
69
	 * @return string
70
	 */
71
	public static function joinSerialization( array $parts ) {
72
		return implode( ':', array_filter( $parts ) );
73
	}
74
75
	/**
76
	 * Returns '' for local IDs and the foreign repository name for foreign IDs. For chained IDs (e.g. foo:bar:Q42) it
77
	 * will return only the first part.
78
	 *
79
	 * @return string
80
	 */
81
	public function getRepoName() {
82
		$parts = self::splitSerialization( $this->serialization );
83
84
		return $parts[0];
85
	}
86
87
	/**
88
	 * Returns the serialization without the first repository prefix.
89
	 *
90
	 * @return string
91
	 */
92
	public function getLocalPart() {
93
		$parts = self::splitSerialization( $this->serialization );
94
95
		return self::joinSerialization( array( $parts[1], $parts[2] ) );
96
	}
97
98
	/**
99
	 * Returns true iff EntityId::getRepoName returns a non-empty string.
100
	 *
101
	 * @return bool
102
	 */
103
	public function isForeign() {
104
		// not actually using EntityId::getRepoName for performance reasons
105
		return strpos( $this->serialization, ':' ) > 0;
106
	}
107
108
	/**
109
	 * @param string $id
110
	 * @return string
111
	 */
112
	private static function normalizeIdSerialization( $id ) {
113
		return ltrim( $id, ':' );
114
	}
115
116
	/**
117
	 * @param string $id
118
	 * @return string
119
	 */
120
	protected static function upcaseLocalId( $id ) {
121
		$parts = self::splitSerialization( $id );
122
123
		return self::joinSerialization( array(
124
			$parts[0],
125
			$parts[1],
126
			strtoupper( $parts[2] )
127
		) );
128
	}
129
130
	/**
131
	 * This is a human readable representation of the EntityId.
132
	 * This format is allowed to change and should therefore not
133
	 * be relied upon to be stable.
134
	 *
135
	 * @return string
136
	 */
137
	public function __toString() {
138
		return $this->serialization;
139
	}
140
141
	/**
142
	 * @see Comparable::equals
143
	 *
144
	 * @since 0.5
145
	 *
146
	 * @param mixed $target
147
	 *
148
	 * @return bool
149
	 */
150
	public function equals( $target ) {
151
		if ( $this === $target ) {
152
			return true;
153
		}
154
155
		return $target instanceof self
156
			&& $target->serialization === $this->serialization;
157
	}
158
159
}
160