Test Setup Failed
Push — master ( 8f1eea...54d071 )
by adam
02:14
created

StatementGuid::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

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
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Wikibase\DataModel\Statement;
4
5
use Comparable;
6
use InvalidArgumentException;
7
use Wikibase\DataModel\Entity\EntityId;
8
9
/**
10
 * Immutable value object for a statement id. A statement id consists of the entity id serialization
11
 * of the entity it belongs to (e.g. "Q1") and a randomly generated global unique identifier (GUID),
12
 * separated by a dollar sign.
13
 *
14
 * @since 3.0
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Addshore
18
 */
19
class StatementGuid implements Comparable {
20
21
	/**
22
	 * The separator for the prefix and suffix of the GUID.
23
	 */
24
	public const SEPARATOR = '$';
25
26
	/**
27
	 * @var EntityId
28
	 */
29
	private $entityId;
30
31 11
	/**
32 11
	 * @var string
33 5
	 */
34
	private $guidPart;
35 6
36
	/**
37
	 * @var string
38
	 */
39 6
	private $serialization;
40 6
41 6
	/**
42
	 * @param EntityId $entityId
43
	 * @param string $guid
44
	 *
45
	 * @throws InvalidArgumentException
46 3
	 */
47 3
	public function __construct( EntityId $entityId, $guid ) {
48
		if ( !is_string( $guid ) ) {
49
			throw new InvalidArgumentException( '$guid must be a string' );
50
		}
51
52
		$this->serialization = $entityId->getSerialization() . self::SEPARATOR . $guid;
53 3
		$this->entityId = $entityId;
54 3
		$this->guidPart = $guid;
55
	}
56
57
	/**
58
	 * @return EntityId
59
	 */
60
	public function getEntityId() {
61
		return $this->entityId;
62 6
	}
63 6
64
	/**
65
	 * @since 9.4
66
	 *
67
	 * @return string
68 6
	 */
69
	public function getGuidPart() {
70
		return $this->guidPart;
71
	}
72
73
	/**
74
	 * @return string
75
	 * @deprecated The value returned by this method might differ in case from the original, unparsed statement GUID
76
	 * (the entity ID part might have been lowercase originally, but is always normalized in the return value here),
77
	 * which means that the value should not be compared to other statement GUID serializations,
78
	 * e.g. to look up a statement in a StatementList.
79
	 */
80
	public function getSerialization() {
81
		return $this->serialization;
82
	}
83
84
	/**
85
	 * @param mixed $target
86
	 *
87
	 * @return bool
88
	 */
89
	public function equals( $target ) {
90
		if ( $this === $target ) {
91
			return true;
92
		}
93
94
		return $target instanceof self
95
			&& $target->serialization === $this->serialization;
96
	}
97
98
	public function __toString() {
99
		return $this->serialization;
100
	}
101
102
}
103