Test Failed
Push — T146030 ( 69731c...ad7786 )
by Leszek
05:52
created

EntityId::upcaseLocalId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
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
 * 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
	const PATTERN = '/^:?(\w+:)*[^:]+\z/';
22
23
	/**
24
	 * @param string $serialization
25
	 */
26
	public function __construct( $serialization ) {
27
		$this::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
	 * EntityId::joinSerialization can be used to restore the original serialization from the parts returned.
62
	 *
63
	 * @param string $serialization
64
	 * @return string[] Array containing the serialization split into 3 parts.
65
	 */
66
	public static function splitSerialization( $serialization ) {
67
		self::assertValidSerialization( $serialization );
68
69
		$parts = explode( ':', self::normalizeIdSerialization( $serialization ) );
70
		$localPart = array_pop( $parts );
71
		$repoName = array_shift( $parts ) ?: '';
72
		$prefixRemainder = implode( ':', $parts );
73
74
		return array( $repoName, $prefixRemainder, $localPart );
75
	}
76
77
	/**
78
	 * Builds an ID serialization from the parts returned by EntityId::splitSerialization.
79
	 *
80
	 * @param string[] $parts
81
	 * @return string
82
	 */
83
	public static function joinSerialization( array $parts ) {
84
		return implode( ':', array_filter( $parts ) );
85
	}
86
87
	/**
88
	 * Returns '' for local IDs and the foreign repository name for foreign IDs. For chained IDs (e.g. foo:bar:Q42) it
89
	 * will return only the first part.
90
	 *
91
	 * @return string
92
	 */
93
	public function getRepoName() {
94
		$parts = self::splitSerialization( $this->serialization );
95
96
		return $parts[0];
97
	}
98
99
	/**
100
	 * Returns the serialization without the first repository prefix.
101
	 *
102
	 * @return string
103
	 */
104
	public function getLocalPart() {
105
		$parts = self::splitSerialization( $this->serialization );
106
107
		return self::joinSerialization( array( $parts[1], $parts[2] ) );
108
	}
109
110
	/**
111
	 * Returns true iff EntityId::getRepoName returns a non-empty string.
112
	 *
113
	 * @return bool
114
	 */
115
	public function isForeign() {
116
		// not actually using EntityId::getRepoName for performance reasons
117
		return strpos( $this->serialization, ':' ) > 0;
118
	}
119
120
	/**
121
	 * @param string $id
122
	 * @return string
123
	 */
124
	private static function normalizeIdSerialization( $id ) {
125
		return ltrim( $id, ':' );
126
	}
127
128
	/**
129
	 * @param string $id
130
	 * @return string
131
	 */
132
	protected static function upcaseLocalId( $id ) {
133
		$parts = self::splitSerialization( $id );
134
135
		return self::joinSerialization( array(
136
			$parts[0],
137
			$parts[1],
138
			strtoupper( $parts[2] )
139
		) );
140
	}
141
142
	/**
143
	 * This is a human readable representation of the EntityId.
144
	 * This format is allowed to change and should therefore not
145
	 * be relied upon to be stable.
146
	 *
147
	 * @return string
148
	 */
149
	public function __toString() {
150
		return $this->serialization;
151
	}
152
153
	/**
154
	 * @see Comparable::equals
155
	 *
156
	 * @since 0.5
157
	 *
158
	 * @param mixed $target
159
	 *
160
	 * @return bool
161
	 */
162
	public function equals( $target ) {
163
		if ( $this === $target ) {
164
			return true;
165
		}
166
167
		return $target instanceof self
168
			&& $target->serialization === $this->serialization;
169
	}
170
171
}
172