SiteLink::getSiteId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Wikibase\DataModel;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Entity\ItemIdSet;
8
9
/**
10
 * Immutable value object representing a link to a page on another site.
11
 *
12
 * A set of badges, represented as ItemId objects, acts as flags
13
 * describing attributes of the linked to page.
14
 *
15
 * @since 0.4
16
 *
17
 * @license GPL-2.0-or-later
18
 * @author Jeroen De Dauw < [email protected] >
19
 * @author Michał Łazowik
20
 * @author Thiemo Kreuz
21
 */
22
class SiteLink {
23
24
	/**
25
	 * @var string
26
	 */
27
	private $siteId;
28
29
	/**
30
	 * @var string
31
	 */
32
	private $pageName;
33
34
	/**
35
	 * @var ItemIdSet
36
	 */
37
	private $badges;
38
39
	/**
40
	 * @param string $siteId
41
	 * @param string $pageName
42
	 * @param ItemIdSet|ItemId[]|null $badges
43
	 *
44
	 * @throws InvalidArgumentException
45
	 */
46
	public function __construct( $siteId, $pageName, $badges = null ) {
47
		if ( !is_string( $siteId ) || $siteId === '' ) {
48
			throw new InvalidArgumentException( '$siteId must be a non-empty string' );
49
		}
50
51
		if ( !is_string( $pageName ) || $pageName === '' ) {
52
			throw new InvalidArgumentException( '$pageName must be a non-empty string' );
53
		}
54
55
		$this->siteId = $siteId;
56 34
		$this->pageName = $pageName;
57 34
		$this->setBadges( $badges );
58 5
	}
59
60
	/**
61 29
	 * @param ItemIdSet|ItemId[]|null $badges
62 5
	 *
63
	 * @throws InvalidArgumentException
64
	 */
65 24
	private function setBadges( $badges ) {
66 24
		if ( $badges === null ) {
67 24
			$badges = new ItemIdSet();
68 16
		} elseif ( is_array( $badges ) ) {
69
			$badges = new ItemIdSet( $badges );
70
		} elseif ( !( $badges instanceof ItemIdSet ) ) {
71
			throw new InvalidArgumentException(
72
				'$badges must be an instance of ItemIdSet, an array of instances of ItemId, or null'
73
			);
74
		}
75 24
76 24
		$this->badges = $badges;
77 12
	}
78 24
79 7
	/**
80 8
	 * @since 0.4
81 4
	 *
82
	 * @return string
83 4
	 */
84
	public function getSiteId() {
85
		return $this->siteId;
86 16
	}
87 16
88
	/**
89
	 * @since 0.4
90
	 *
91
	 * @return string
92
	 */
93
	public function getPageName() {
94 3
		return $this->pageName;
95 3
	}
96
97
	/**
98
	 * Badges are not order dependent.
99
	 *
100
	 * @since 0.5
101
	 *
102
	 * @return ItemId[]
103 3
	 */
104 3
	public function getBadges() {
105
		return array_values( iterator_to_array( $this->badges ) );
106
	}
107
108
	/**
109
	 *
110
	 * @since 0.7.4
111
	 *
112
	 * @param mixed $target
113
	 *
114 5
	 * @return bool
115 5
	 */
116
	public function equals( $target ) {
117
		if ( $this === $target ) {
118
			return true;
119
		}
120
121
		return $target instanceof self
122
			&& $this->siteId === $target->siteId
123
			&& $this->pageName === $target->pageName
124
			&& $this->badges->equals( $target->badges );
125
	}
126
127
}
128