Completed
Push — T146030 ( 69731c )
by Leszek
06:28
created

EntityId::splitSerialization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Wikibase\DataModel\Entity;
4
5
use Comparable;
6
use Serializable;
7
8
/**
9
 * @since 0.5
10
 * Constructor non-public since 1.0
11
 * Abstract since 2.0
12
 *
13
 * @license GPL-2.0+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
abstract class EntityId implements Comparable, Serializable {
17
18
	protected $serialization;
19
20
	/**
21
	 * @param $serialization
22
	 */
23
	protected function __construct( $serialization ) {
24
		$this->serialization = $serialization;
25
	}
26
27
	/**
28
	 * @return string
29
	 */
30
	public abstract function getEntityType();
31
32
	/**
33
	 * @return string
34
	 */
35
	public function getSerialization() {
36
		return $this->serialization;
37
	}
38
39
	/**
40
	 * Returns an array with 3 elements: the foreign repository name as the first element, the local ID as the last
41
	 * element and everything that is in between as the second element.
42
	 *
43
	 * @param $serialization
44
	 * @return array
45
	 */
46
	public static function splitSerialization( $serialization ) {
47
		$parts = explode( ':', $serialization );
48
		$localPart = array_pop( $parts );
49
		$repoName = array_shift( $parts ) ?: '';
50
		$prefixRemainder = implode( ':', $parts );
51
52
		return array( $repoName, $prefixRemainder, $localPart );
53
	}
54
55
	/**
56
	 * Concatenates parts of an EntityId serialization with ':'.
57
	 *
58
	 * @param array $parts
59
	 * @return string
60
	 */
61
	public static function joinSerialization( $parts ) {
62
		return implode( ':', array_filter( $parts ) );
63
	}
64
65
	/**
66
	 * Returns the foreign repository name or empty string for local entities.
67
	 * @return string
68
	 */
69
	public function getRepoName() {
70
		$parts = self::splitSerialization( $this->serialization );
71
72
		return $parts[0];
73
	}
74
75
	/**
76
	 * @return string
77
	 */
78
	public function getLocalPart() {
79
		$parts = self::splitSerialization( $this->serialization );
80
81
		return self::joinSerialization( array( '', $parts[1], $parts[2] ) );
82
	}
83
84
	/**
85
	 * @return bool
86
	 */
87
	public function isForeign() {
88
		return strpos( $this->serialization, ':' ) > 0;
89
	}
90
91
	protected static function normalizeIdSerialization( $id ) {
92
		$parts = self::splitSerialization( ltrim( $id, ':' ) );
93
94
		return self::joinSerialization( array(
95
			$parts[0],
96
			$parts[1],
97
			strtoupper( $parts[2] )
98
		) );
99
	}
100
101
	/**
102
	 * This is a human readable representation of the EntityId.
103
	 * This format is allowed to change and should therefore not
104
	 * be relied upon to be stable.
105
	 *
106
	 * @return string
107
	 */
108
	public function __toString() {
109
		return $this->serialization;
110
	}
111
112
	/**
113
	 * @see Comparable::equals
114
	 *
115
	 * @since 0.5
116
	 *
117
	 * @param mixed $target
118
	 *
119
	 * @return bool
120
	 */
121
	public function equals( $target ) {
122
		if ( $this === $target ) {
123
			return true;
124
		}
125
126
		return $target instanceof self
127
			&& $target->serialization === $this->serialization;
128
	}
129
130
}
131