Completed
Push — suchids ( 874a83 )
by Jeroen De
03:40
created

EntityId::isForeign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
	protected $repositoryName;
20
21
	/**
22
	 * @return string
23
	 */
24
	public abstract function getEntityType();
25
26
	/**
27
	 * @return string
28
	 */
29
	public function getSerialization() {
30
		if ( $this->isForeign() ) {
31
			return $this->repositoryName . ':' . $this->serialization;
32
		}
33
34
		return $this->serialization;
35
	}
36
37
	/**
38
	 * Returns the serialization without the first repository prefix.
39
	 *
40
	 * @return string
41
	 */
42
	public function getLocalPart() {
43
		return $this->serialization;
44
	}
45
46
	/**
47
	 * Returns '' for local IDs and the foreign repository name for foreign IDs. For chained IDs (e.g. foo:bar:Q42) it
48
	 * will return only the first part.
49
	 *
50
	 * @return string
51
	 */
52
	public function getRepoName() {
53
		return $this->repositoryName;
54
	}
55
56
	/**
57
	 * @return bool
58
	 */
59
	public function isForeign() {
60
		return $this->repositoryName !== '';
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->getSerialization();
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
			&& $target->repositoryName === $this->repositoryName;
91
	}
92
93
}
94