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

EntityId::upcaseLocalId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 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
	 * @return string
22
	 */
23
	public abstract function getEntityType();
24
25
	/**
26
	 * @return string
27
	 */
28 31
	public function getSerialization() {
29 31
		return $this->serialization;
30
	}
31
32
	/**
33
	 * Returns the foreign repository name or empty string for local entities.
34
	 * @return string
35
	 */
36
	public function getRepoName() {
37
		if ( $this->isForeign() ) {
38
			return strtok( $this->serialization, ':' );
39 5
		}
40 5
41
		return '';
42
	}
43
44
	/**
45
	 * @return bool
46
	 */
47
	public function isForeign() {
48
		return strpos( $this->serialization, ':' ) > 0;
49
	}
50
51
	protected function sanitizeIdSerialization( $id ) {
52 5
		return $this->upcaseLocalId( ltrim( $id, ':' ) );
53 5
54 5
	}
55
56
	private function upcaseLocalId( $id ) {
57
		$parts = explode( ':', $id );
58 5
		$parts[count( $parts ) - 1] = strtoupper( end( $parts ) );
59
60
		return implode( ':', $parts );
61
	}
62
63
	/**
64
	 * This is a human readable representation of the EntityId.
65
	 * This format is allowed to change and should therefore not
66
	 * be relied upon to be stable.
67
	 *
68
	 * @return string
69
	 */
70
	public function __toString() {
71
		return $this->serialization;
72
	}
73
74
	/**
75
	 * @see Comparable::equals
76
	 *
77
	 * @since 0.5
78
	 *
79
	 * @param mixed $target
80
	 *
81
	 * @return bool
82
	 */
83
	public function equals( $target ) {
84
		if ( $this === $target ) {
85
			return true;
86
		}
87
88
		return $target instanceof self
89
			&& $target->serialization === $this->serialization;
90
	}
91
92
}
93